forked from ftde0/yt2009
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.js
89 lines (79 loc) · 2.2 KB
/
docker-entrypoint.js
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
const fs = require('fs');
let cfg = {};
let tokensCount = 25;
// try get tokens from old config
try {
var oldtokens = require('./back/config.json').tokens;
} catch {}
// generate config using env
// set port
try {
cfg.port = parseInt(process.env.YT2009_PORT);
} catch {
throw new Error('invalid YT2009_PORT');
}
// set env
if(process.env.YT2009_ENV === 'dev' || process.env.YT2009_ENV === 'prod') {
cfg.env = process.env.YT2009_ENV;
} else {
throw new Error('invalid YT2009_ENV')
}
// set ip
// no simple way to verify validity afaik, will need to trust the user on this one
cfg.ip = process.env.YT2009_IP;
//set protobuf
switch (process.env.YT2009_PB) {
case 'true':
cfg.use_pb = true;
break;
case 'false':
cfg.use_pb = false;
break;
default:
throw new Error('invalid YT2009_PB')
}
// set ssl
switch (process.env.YT2009_SSL) {
case 'true':
cfg.useSSL = true;
break;
case 'false':
cfg.useSSL = false;
break;
default:
throw new Error('invalid YT2009_SSL')
}
if (cfg.useSSL) {
try {
cfg.SSLPort = parseInt(process.env.YT2009_SSLPORT);
} catch {
throw new Error('invalid YT2009_SSLPORT');
}
cfg.SSLCertPath = process.env.YT2009_SSLPATH;
cfg.SSLKeyPath = process.env.YT2009_SSLKEY;
// check if ssl files exist
if (!fs.existsSync(cfg.SSLCertPath) || !fs.existsSync(cfg.SSLKeyPath)) {
throw new Error('no ssl cert/key provided')
}
}
// try to get tokens from old config and if no tokens are found then generate new ones
if (oldtokens) {
cfg.tokens = oldtokens;
} else {
// code taken from post_config_setup.js
if(cfg.env == "prod"
&& !cfg.tokens) {
console.log("environment set to prod but no tokens. generating!!")
let tokens = []
while(tokens.length !== tokensCount) {
let token = ""
while(token.length !== 9) {
token += "qwertyuiopasdfghjklzxcvbnm1234567890".split("")
[Math.floor(Math.random() * 36)]
}
tokens.push(token)
}
cfg.tokens = tokens
}
};
fs.writeFileSync(`${__dirname}/back/config.json`, JSON.stringify(cfg))