forked from survev/survev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ts
More file actions
425 lines (362 loc) · 12.5 KB
/
Copy pathsetup.ts
File metadata and controls
425 lines (362 loc) · 12.5 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import enquirer from "enquirer";
import hjson from "hjson";
import { randomBytes } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { configFileName } from "./config.ts";
import type { PartialConfig } from "./configType.ts";
import { util } from "./shared/utils/util.ts";
const prompt = enquirer.prompt;
async function importKeys(config: PartialConfig) {
config.secrets ??= {};
const apiKey = await prompt<{ value: string }>({
message: "Enter API key:",
name: "value",
type: "text",
required: true,
});
config.secrets.SURVEV_API_KEY = apiKey.value;
}
async function setupGameServer(config: PartialConfig) {
config.gameServer ??= {};
const regionId = await prompt<{ value: string }>({
message: "Which region is this game server hosting?",
name: "value",
type: "text",
required: true,
});
config.gameServer.thisRegion = regionId.value;
config.regions ??= {};
if (!config.regions[regionId.value]) {
const https = await prompt<{ value: boolean }>({
message: "Does this region support https?",
name: "value",
type: "confirm",
initial: true,
});
const address = await prompt<{ value: string }>({
message: "Enter region address",
name: "value",
type: "text",
});
const l10n = await prompt<{ value: string }>({
message: "Enter region translation key (eg: index-north-america, index-south-america)",
name: "value",
type: "text",
});
config.regions[regionId.value] = {
https: https.value,
address: address.value,
l10n: l10n.value,
};
}
const apiAddress = await prompt<{ value: string }>({
message: "Enter the API server address",
name: "value",
type: "text",
required: true,
initial: `http://${config.apiServer?.host ?? "127.0.0.1"}:${config.apiServer?.port ?? 8000}`,
validate(value) {
return URL.parse(value) !== null;
},
});
config.gameServer.apiServerUrl = apiAddress.value;
await setupProxyIPHeader(config, "gameServer");
await setupSSL(config, "gameServer");
}
async function setupDatabase(config: PartialConfig, initial = true) {
const dbEnabled = await prompt<{ value: boolean }>({
message: "Would you like to setup database support (required for accounts, IP bans, leaderboards etc)",
name: "value",
type: "confirm",
initial,
});
config.database = {
...config.database,
enabled: dbEnabled.value,
};
if (dbEnabled.value) {
await setupAccounts(config);
const setupBot = await prompt<{ value: boolean }>({
message: "Would you like to setup the moderation bot?",
name: "value",
type: "confirm",
initial: false,
});
if (setupBot.value) {
await setupBotConfig(config);
}
}
}
async function setupAccounts(config: PartialConfig) {
const redirectURI = await prompt<{ value: string }>({
message: "Enter the full base URL of the website for oauth2 redirects (eg: https://survev.io)",
name: "value",
type: "text",
initial: `http://${config.apiServer?.host ?? "127.0.0.1"}:${config.apiServer?.port ?? 8000}`,
required: true,
});
config.oauthRedirectURI = redirectURI.value;
const addGoogle = await prompt<{ value: boolean }>({
message: "Would you like to add google login support",
name: "value",
type: "confirm",
initial: false,
});
config.secrets ??= {};
if (addGoogle.value) {
const clientId = await prompt<{ value: string }>({
message: "Enter google client ID",
name: "value",
type: "text",
required: true,
});
config.secrets.GOOGLE_SECRET_ID = clientId.value;
const clientSecret = await prompt<{ value: string }>({
message: "Enter google secret ID",
name: "value",
type: "text",
required: true,
});
config.secrets.GOOGLE_SECRET_ID = clientSecret.value;
}
const addDiscord = await prompt<{ value: boolean }>({
message: "Would you like to add discord login support",
name: "value",
type: "confirm",
initial: false,
});
if (addDiscord.value) {
const clientId = await prompt<{ value: string }>({
message: "Enter discord client ID",
name: "value",
type: "text",
required: true,
});
config.secrets.DISCORD_CLIENT_ID = clientId.value;
const clientSecret = await prompt<{ value: string }>({
message: "Enter discord secret secret",
name: "value",
type: "text",
required: true,
});
config.secrets.DISCORD_SECRET_ID = clientSecret.value;
}
}
async function setupAPIServer(config: PartialConfig) {
const shouldImportKeys = await prompt<{ value: "import" | "random" }>({
message: "Would you like to import the API and loadout secret keys or use random ones?",
name: "value",
type: "select",
choices: ["import", "random"],
});
if (shouldImportKeys.value === "import") {
await importKeys(config);
}
await setupProxyIPHeader(config, "apiServer");
await setupSSL(config, "apiServer");
await setupDatabase(config);
}
async function setupRegions(config: PartialConfig) {
config.regions ??= {};
let addRegion = true;
while (addRegion) {
const regionId = await prompt<{ value: string }>({
message: "Enter region ID (eg: na, eu, sa, as)",
name: "value",
type: "text",
required: true,
});
const https = await prompt<{ value: boolean }>({
message: "Does this region support https?",
name: "value",
type: "confirm",
initial: true,
});
const address = await prompt<{ value: string }>({
message: "Enter region address",
name: "value",
type: "text",
});
const l10n = await prompt<{ value: string }>({
message: "Enter region translation key (eg: index-north-america, index-south-america)",
name: "value",
type: "text",
});
config.regions[regionId.value] = {
https: https.value,
address: address.value,
l10n: l10n.value,
};
const addMore = await prompt<{ value: boolean }>({
message: "Would you like to add another region?",
name: "value",
type: "confirm",
initial: false,
});
addRegion = addMore.value;
}
}
async function setupProxyCheck(config: PartialConfig) {
const enableProxyCheck = await prompt<{ value: boolean }>({
message: "Would you like to enable proxycheck.io to ban VPNs and proxies?",
name: "value",
type: "confirm",
initial: false,
});
if (enableProxyCheck.value) {
const proxycheckKey = await prompt<{ value: string }>({
message: "Enter proxycheck API key",
name: "value",
type: "text",
});
config.secrets ??= {};
config.secrets.PROXYCHECK_KEY = proxycheckKey.value;
}
}
async function setupProxyIPHeader(
config: PartialConfig,
server: "apiServer" | "gameServer",
) {
const serverName = server == "apiServer" ? "API server" : "game server";
const isBehindProxy = await prompt<{ value: boolean }>({
message: `Is the ${serverName} behind a proxy? (e.g nginx or cloudflare)`,
name: "value",
type: "confirm",
initial: false,
});
if (isBehindProxy.value) {
const proxyHeader = await prompt<{ value: string }>({
message: "Enter the proxy HTTP header",
name: "value",
type: "text",
initial: "X-Real-IP",
});
config[server] ??= {};
config[server].proxyIPHeader = proxyHeader.value;
}
}
async function setupSSL(config: PartialConfig, server: "apiServer" | "gameServer") {
const serverName = server == "apiServer" ? "API server" : "game server";
const enableSSL = await prompt<{ value: boolean }>({
message: `Would you like enabling SSL for the ${serverName}?`,
name: "value",
type: "confirm",
initial: false,
});
if (enableSSL.value) {
const keyFilePath = await prompt<{ value: string }>({
message: "Enter SSL key file path",
name: "value",
type: "text",
});
const certFilePath = await prompt<{ value: string }>({
message: "Enter SSL cert file path",
name: "value",
type: "text",
});
config[server] ??= {};
config[server].ssl = {
keyFile: keyFilePath.value,
certFile: certFilePath.value,
};
}
}
async function setupProductionConfig(config: PartialConfig) {
const apiOrGameServer = await prompt<{
value: "Both" | "API" | "Game server region";
}>({
message: "Are you deploying a an API server, a game server region or both?",
name: "value",
type: "select",
choices: ["Both", "API", "Game server region"],
});
if (apiOrGameServer.value === "Both") {
await setupAPIServer(config);
await setupRegions(config);
await setupGameServer(config);
} else if (apiOrGameServer.value === "API") {
await setupAPIServer(config);
await setupRegions(config);
} else {
await setupGameServer(config);
await importKeys(config);
}
await setupProxyCheck(config);
}
async function setupDevelopmentConfig(config: PartialConfig) {
await setupDatabase(config, false);
}
async function setupBotConfig(config: PartialConfig) {
config.secrets ??= {};
if (!config.secrets.DISCORD_CLIENT_ID) {
const clientId = await prompt<{ value: string }>({
message: "Enter discord client ID",
name: "value",
type: "text",
required: true,
});
config.secrets.DISCORD_CLIENT_ID = clientId.value;
}
const discordBotToken = await prompt<{ value: string }>({
message: "Enter the discord bot token",
name: "value",
type: "text",
});
config.secrets.DISCORD_BOT_TOKEN = discordBotToken.value;
const discordGuildId = await prompt<{ value: string }>({
message: "Enter the guild ID",
name: "value",
type: "text",
});
config.discordGuildId = discordGuildId.value;
const discordRoleId = await prompt<{ value: string }>({
message: "Enter the discord role ID",
name: "value",
type: "text",
});
config.discordRoleId = discordRoleId.value;
}
const configPath = path.join(import.meta.dirname, configFileName);
async function loadExistingConfig(config: PartialConfig) {
if (!fs.existsSync(configPath)) return;
const confirmation = await prompt<{ value: boolean }>({
message: "A config file already exists, would you like to run this setup anyway?",
name: "value",
type: "confirm",
initial: true,
});
if (!confirmation.value) {
process.exit(0);
}
const configText = fs.readFileSync(configPath).toString();
const localConfig = hjson.parse(configText);
util.mergeDeep(config, localConfig);
}
async function setupConfig() {
const config: PartialConfig = {
secrets: {
SURVEV_API_KEY: randomBytes(64).toString("base64"),
},
};
console.log("Welcome to Survev.io initial config setup!");
await loadExistingConfig(config);
const devOrProd = await prompt<{ value: "development" | "production" }>({
message: "Are you setting up a local development environment or a production server?",
name: "value",
type: "select",
choices: ["production", "development"],
});
if (devOrProd.value === "development") {
await setupDevelopmentConfig(config);
} else {
await setupProductionConfig(config);
}
const str = hjson.stringify(config, { bracesSameLine: true, space: 2 });
fs.writeFileSync(configPath, str);
console.log("Wrote config to", configPath, ":");
console.log(
hjson.stringify(config, { bracesSameLine: true, space: 2, colors: true }),
);
}
await setupConfig();