forked from ftde0/yt2009
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.js
163 lines (143 loc) · 4.02 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
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
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 auto maintain
switch (process.env.YT2009_AUTO_MAINTAIN) {
case 'true':
cfg.auto_maintain = true;
break;
case 'false':
cfg.auto_maintain = false;
break;
default:
throw new Error('invalid YT2009_AUTO_MAINTAIN')
}
// set maintain max size
try {
cfg.maintain_max_size = parseInt(process.env.YT2009_MAINTAIN_MAX_SIZE);
} catch {
throw new Error('invalid YT2009_MAINTAIN_MAX_SIZE');
}
// set maintain max cache size
try {
cfg.maintain_max_cache_size = parseInt(process.env.YT2009_MAINTAIN_MAX_CACHE_SIZE);
} catch {
throw new Error('invalid YT2009_MAINTAIN_MAX_CACHE_SIZE');
}
// 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
if(process.env.YT2009_IP) {
cfg.ip = process.env.YT2009_IP;
}
// set fallback
switch (process.env.YT2009_FALLBACK) {
case 'true':
cfg.fallbackMode = true;
break;
case 'false':
cfg.fallbackMode = false;
break;
default:
throw new Error('invalid YT2009_FALLBACK')
}
// set master server
if(process.env.YT2009_MASTERSERVER) {
cfg.overrideMaster = process.env.YT2009_MASTERSERVER;
}
// set disable master server
switch (process.env.YT2009_DISABLEMASTER) {
case 'true':
cfg.disableWs = true;
break;
case 'false':
cfg.disableWs = false;
break;
default:
throw new Error('invalid YT2009_DISABLEMASTER')
}
// set locked tokens
if(process.env.YT2009_LOCKED_TOKENS) {
cfg.templocked_tokens = process.env.YT2009_LOCKED_TOKENS.split(',');
}
// set logged tokens
if(process.env.YT2009_LOGGED_TOKENS) {
cfg.logged_tokens = process.env.YT2009_LOGGED_TOKENS.split(',');
}
// set homepage text
if(process.env.YT2009_HOMEPAGETEXT) {
cfg.customHomepageText = process.env.YT2009_HOMEPAGETEXT;
}
// set redir
if(process.env.YT2009_REDIR) {
cfg.redirmode = process.env.YT2009_REDIR;
}
// 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')
}
}
// set ratelimit
try {
cfg.ratelimit = parseInt(process.env.YT2009_RATELIMIT);
} catch {
throw new Error('invalid YT2009_RATELIMIT');
}
// try to get tokens from env, if there's none then take get from old config and if no tokens are found then generate new ones
if (process.env.YT2009_TOKENS) {
cfg.tokens = process.env.YT2009_TOKENS.split(',');
} else 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))