CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/compute/cloud/common/dns.tsx
Views: 687
1
import { DNS_COST_PER_HOUR, checkValidDomain } from "@cocalc/util/compute/dns";
2
import { useTypedRedux } from "@cocalc/frontend/app-framework";
3
import { useEffect, useState } from "react";
4
import { Button, Checkbox, Input, Typography } from "antd";
5
import { A, Icon } from "@cocalc/frontend/components";
6
import { currency } from "@cocalc/util/misc";
7
8
export default function DNS({ setConfig, configuration, loading }) {
9
const compute_servers_dns = useTypedRedux("customize", "compute_servers_dns");
10
const [showDns, setShowDns] = useState<boolean>(!!configuration.dns);
11
const [dnsError, setDnsError] = useState<string>("");
12
const [dns, setDns] = useState<string | undefined>(configuration.dns);
13
useEffect(() => {
14
if (!dns) return;
15
try {
16
checkValidDomain(dns);
17
setDnsError("");
18
} catch (err) {
19
setDnsError(`${err}`);
20
}
21
}, [dns]);
22
23
if (!compute_servers_dns) {
24
return null;
25
}
26
27
return (
28
<div>
29
<Checkbox
30
disabled={loading}
31
checked={showDns}
32
onChange={() => {
33
setShowDns(!showDns);
34
if (showDns) {
35
// disable on backend.
36
setConfig({ dns: "" });
37
}
38
}}
39
>
40
DNS: Custom Subdomain with SSL ({currency(DNS_COST_PER_HOUR)}/hour when
41
running or stopped)
42
</Checkbox>
43
{showDns && (
44
<A
45
style={{ float: "right" }}
46
href={`https://${configuration.dns}.${compute_servers_dns}`}
47
>
48
<Icon name="external-link" /> https://{dns ?? "*"}.
49
{compute_servers_dns}
50
</A>
51
)}
52
{showDns && (
53
<div style={{ marginTop: "5px" }}>
54
<Input
55
disabled={loading}
56
style={{ margin: "15px 0" }}
57
maxLength={63}
58
showCount
59
allowClear
60
value={dns}
61
onChange={(e) => {
62
const dns = e.target.value.trim();
63
setDns(dns);
64
if (!dns) {
65
setConfig({ dns: "" });
66
}
67
}}
68
/>
69
70
<Button
71
disabled={configuration.dns == dns || dnsError || loading}
72
onClick={() => {
73
const s = (dns ?? "").toLowerCase();
74
setConfig({ dns: s });
75
setDns(s);
76
}}
77
>
78
{!dns || configuration.dns != dns
79
? "Enable Custom Domain"
80
: "Custom Domain Enabled"}
81
</Button>
82
<div style={{ color: "#666", margin: "5px 0" }}>
83
<Typography.Paragraph
84
style={{ color: "#666" }}
85
ellipsis={{
86
expandable: true,
87
rows: 2,
88
symbol: "more",
89
}}
90
>
91
A custom DNS A record with{" "}
92
<A href="https://developers.cloudflare.com/dns/manage-dns-records/reference/proxied-dns-records/">
93
https and http proxying will be created at CloudFlare
94
</A>{" "}
95
as long as your VM is not deprovisioned. Whenever your VM starts
96
running it is allocated an external ip address, and CoCalc updates
97
the DNS entry to point at that ip address. A web server with
98
self-signed certificate will appear to have a proper certificate
99
to website visitors. You can enable or disable custom DNS at any
100
time.
101
</Typography.Paragraph>
102
</div>
103
{dnsError && dns && (
104
<div
105
style={{
106
background: "red",
107
color: "white",
108
padding: "5px",
109
margin: "10px 0",
110
}}
111
>
112
<div>{dnsError}</div>
113
Please enter a valid subdomain name. Subdomains can consist of
114
letters (a-z, A-Z), numbers (0-9), and hyphens (-). They cannot
115
start or end with a hyphen.
116
</div>
117
)}
118
</div>
119
)}
120
</div>
121
);
122
}
123
124