-
Notifications
You must be signed in to change notification settings - Fork 16
/
Domains.ts
117 lines (98 loc) · 4.29 KB
/
Domains.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {Assert} from "js-vextensions";
// sync:rs
// ==========
const domainConstants = {
prodDomain: "debatemap.app",
recognizedWebServerHosts: [
"localhost:5100", "localhost:5200", // load-balancer service (exposes web-server, plus other services, eg. /grafana)
"localhost:5101", // local webpack (alternative to web-server pod, when doing local development)
"localhost:5131", // monitor, local webpack (alternative to frontend-serving from `/monitor`, when doing local development)
"localhost:5150", "localhost:5250", // pyroscope
// direct to server
"9m2x1z.nodes.c1.or1.k8s.ovh.us",
"debatemap.societylibrary.org",
// through cloudflare
"debatemap.app",
"debates.app",
"debating.app",
],
// since in client code, these are always false (vars are kept to match server code)
onServerAndDev: false,
onServerAndProd: false,
};
export type ServerPod = "web-server" | "app-server" | "monitor" | "grafana" | "pyroscope";
export type GetServerURL_Options = {
claimedClientURL: string|n,
restrictToRecognizedHosts: boolean,
forceLocalhost?: boolean,
forceHTTPS?: boolean,
};
export function GetServerURL(serverPod: ServerPod, subpath: string, opts: GetServerURL_Options) {
Assert(subpath.startsWith("/"));
// process claimed-client-url
const claimedClientURL = opts.claimedClientURL ? new URL(opts.claimedClientURL) : null;
const shouldTrustClaimedClientURL = claimedClientURL != null
? !opts.restrictToRecognizedHosts || domainConstants.recognizedWebServerHosts.includes(claimedClientURL.host) || domainConstants.onServerAndDev
: false;
const claimedClientURL_trusted = shouldTrustClaimedClientURL ? claimedClientURL : null;
const claimedClientURL_appServerPort = claimedClientURL_trusted?.searchParams.get("appServerPort") ?? "5100"; // 5100 is the standard local-k8s entry-point
let serverURL: URL;
// section 1: set protocol and hostname
// ==========
//if (claimedClientURL_trusted != null) {
if (claimedClientURL_trusted != null) {
const portStr = claimedClientURL_trusted.port ? `:${claimedClientURL_trusted.port}` : "";
serverURL = new URL(`${claimedClientURL_trusted.protocol}//${claimedClientURL_trusted.hostname}${portStr}`);
} else {
// if we don't have a claimed-client-url that we can trust, then just guess at the correct origin
//Assert(webServerHosts.includes(referrerURL.host), `Client sent invalid referrer host (${referrerURL.host}).`);
const guessedToBeLocal = opts.forceLocalhost || domainConstants.onServerAndDev;
if (guessedToBeLocal) {
serverURL = new URL(`http://localhost:5100`);
} else {
serverURL = new URL(`https://${domainConstants.prodDomain}`);
}
}
const backendIsRemote = !opts.forceLocalhost && (
claimedClientURL_trusted?.searchParams.get("db") == "prod"
|| serverURL.hostname == domainConstants.prodDomain
);
// section 2: set subdomain/port
// ==========
// for simply deciding between localhost:5100 and localhost:5101, we don't need the claimed-client-url to be "trusted"
if (serverPod == "web-server" && claimedClientURL?.port == "5101") {
serverURL.port = "5101";
} else if (serverPod == "pyroscope") {
serverURL.hostname = "localhost";
serverURL.port = backendIsRemote ? "5250" : "5150";
}
const permittedAppServerPortOverrides = ["5110"];
if (serverPod == "app-server" && permittedAppServerPortOverrides.includes(claimedClientURL_appServerPort)) {
serverURL.port = claimedClientURL_appServerPort;
}
// section 3: set path
// ==========
Assert(subpath.startsWith("/"), "Subpath must start with a forward-slash.");
let subpathFinal = subpath;
if (serverPod == "app-server") {
subpathFinal = `/app-server${subpathFinal}`;
} else if (serverPod == "monitor") {
subpathFinal = `/monitor${subpathFinal}`;
} else if (serverPod == "grafana") {
subpathFinal = `/grafana${subpathFinal}`;
}
serverURL.pathname = subpathFinal;
// section 4: special-case handling
// ==========
// if this app-server is PROD, but we have a "localhost" host, user must be using the "?db=prod" flag
/*if (ON_SERVER_AND_PROD && (claimedClientURL?.host == "localhost:5100" || claimedClientURL?.host == "localhost:5101")) {
if (subpath == "/auth/google/callback") {
subpath = "/auth/google/callback_returnToLocalhost";
serverURL.pathname = subpath;
}
}*/
if (opts.forceHTTPS) {
serverURL.protocol = "https:";
}
return serverURL.toString();
}