-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
378 lines (332 loc) · 8.93 KB
/
app.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
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
const express = require("express");
const fs = require("fs");
const Docker = require("dockerode");
const crypto = require("crypto");
const Ipware = require("@fullerstack/nax-ipware");
const ipware = new Ipware.Ipware();
const logger = require("pino")({
level: "debug",
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});
/**
* @typedef Auth
* @property {string} username
* @property {string} password
* @property {string} email
* @property {string} serveraddress
*/
class ConfigEntry {
/**
* Create a new configuration entry.
* @param {Object} conf
* @param {string} conf.name
* @param {string} conf.key
* @param {string} conf.image
* @param {Auth} conf.auth
* @param {string[]} [conf.queryParamsToEnv=[]]
* @param {string[]?} [conf.allowFrom=["127.0.0.1", "::1"]]
* @param {string[]?} [conf.cmd=[]]
* @param {Object?} [conf.createOptions={}]
* @param {number} [conf.limit=1]
*/
constructor(conf) {
this._name = conf.name;
this._key = conf.key;
this._image = conf.image;
this._auth = conf.auth;
this._queryParamsToEnv = conf.queryParamsToEnv || [];
this._allowFrom = conf.allowFrom || ["127.0.0.1", "::1"];
this._cmd = conf.cmd || [];
this._createOptions = conf.createOptions || {};
this._limit = conf.limit || 1;
}
/**
* The name of the configuration as provided without replace non-alphanumeric characters.
* @private
* @type {string}
*/
get _rawName() {
return this._name;
}
/**
* Unique name for the configuration.
* All non-alphanumeric characters are replaced with dashes
* @type {string}
*/
get name() {
return cleanConfigName(this._name);
}
/**
* @type {string}
*/
get key() {
return this._key;
}
/**
* @type {string[]}
*/
get allowFrom() {
return this._allowFrom;
}
/**
* @type {string}
*/
get image() {
return this._image;
}
/**
* @type {Auth}
*/
get auth() {
return this._auth;
}
/**
* @type {string[]}
*/
get cmd() {
return this._cmd;
}
/**
* @type {string[]}
*/
get queryParamsToEnv() {
return this._queryParamsToEnv;
}
/**
* @type {Object}
*/
get createOptions() {
return this._createOptions;
}
/**
* @type {number}
*/
get limit() {
return this._limit;
}
}
let docker, config;
/**
* Replace all non-alphanumeric characters in the given name with dashes (-)
* @param {string} name
* @returns {string} Cleaned name
*/
function cleanConfigName(name) {
return name.replace(/[\W_]+/g, "-");
}
/**
* Attempt to validate the provided configuration. Throws if there is an issue.
* @param {ConfigEntry[]} configArray
*/
function tryValidateConfig(configArray) {
configArray.forEach((curr) => {
if (!curr.name) {
throw "Missing config name.";
}
if (!curr.key) {
throw `Missing key for config '${curr.name}'`;
}
if (curr.key.length < 50) {
logger.warn("Key for config '%s' is < 50 chars long", curr.name);
}
if (!curr.image) {
throw `Missing image for config '${curr.name}'`;
}
});
// names must be unique
const allNames = configArray.map((curr) => curr.name);
if (Array.from(new Set(allNames)).length !== allNames.length) {
throw "Configuration entry names must be unique.";
}
// keys must be unique
const allKeys = configArray.map((curr) => curr.key);
if (Array.from(new Set(allKeys)).length !== allKeys.length) {
throw "Configuration entry keys must be unique.";
}
logger.info("Configuration appears to be valid");
}
/**
* Key is the generated name of the running container instance.
* @type {Map<string,string[]}>}
*/
let runningMap = new Map();
/**
* @param {ConfigEntry} configEntry
* @returns Unique instance name for the container run
*/
function genNewInstanceName(configEntry) {
let rootName = configEntry.name;
let uniq = crypto.randomUUID();
return `${rootName}_${uniq}`;
}
/**
* Try adding a container instance to tracking. Checks if the configured run limit is exceeded.
* @param {ConfigEntry} config
* @param {string} instanceName
* @returns {boolean} true if running a new instance is allowed; false if the configured limit is exceeded
*/
function tryTrackNewRunningInstance(config, instanceName) {
if (runningMap.has(config.name)) {
let alreadyRunning = runningMap.get(config.name);
if (alreadyRunning.length >= config.limit) {
return false;
}
alreadyRunning.push(instanceName);
} else {
runningMap.set(config.name, [instanceName]);
}
return true;
}
/**
* Remove a container instance from tracking (because it has finished executing and been removed).
* @param {ConfigEntry} config
* @param {string} instanceName
*/
function untrackRunningInstance(config, instanceName) {
let alreadyRunning = runningMap.get(config.name);
if (alreadyRunning && alreadyRunning.length) {
alreadyRunning.splice(alreadyRunning.indexOf(instanceName));
}
}
/**
* Extract allowlisted query parameters into environment variable format.
* @param {ConfigEntry} config
* @param {Object} query
* @returns {string[]}
*/
function extractAllowedQueryParams(config, query) {
return config.queryParamsToEnv
.filter((key) => key in query)
.map((key) => `${key}=${decodeURIComponent(query[key])}`);
}
// Configure app
const app = express();
// Status endpoint
app.get("/", (req, res) => {
res.status(200).end();
});
// Kicker endpoint
app.post("/:key", function (req, res) {
const key = req.params.key;
const match = config.find((e) => e.key === key);
if (!match) {
res.status(400).end();
return;
}
const ipInfo = ipware.getClientIP(req, {
proxy: connectConfig.proxy,
});
logger.info("Computed client IP: %s", ipInfo?.ip)
const clientIp = ipInfo?.ip ?? req.ip
if (
(match.allowFrom &&
typeof match.allowFrom === "string" &&
match.allowFrom !== clientIp) ||
(Array.isArray(match.allowFrom) &&
!match.allowFrom.some((af) => af === clientIp))
) {
logger.warn("Rejecting kick request from %s", clientIp);
res.status(403).end();
return;
}
logger.info(
"Kicking %s / %s via web request from %s",
match.name,
match.cmd,
clientIp
);
// Add any query-to-env vars to env
const queryToEnvVars = extractAllowedQueryParams(match, req.query);
logger.debug("allowed query params: %s", match.queryParamsToEnv);
logger.debug("extracted query params: %s", queryToEnvVars);
if (queryToEnvVars) {
let env = [...(match.createOptions?.env ?? []), ...queryToEnvVars];
if (match.createOptions) {
match.createOptions.env = env;
} else {
match.createOptions = { env };
}
}
// Transform create options
let createOptions = {
name: genNewInstanceName(match),
...match.createOptions,
};
logger.debug({ createOptions });
try {
// Pull latest image down
docker.pull(match.image, {authconfig: match.auth}).then(function () {
logger.info("Running instance %s", createOptions.name);
if (!tryTrackNewRunningInstance(match, createOptions.name)) {
logger.warn(
"Limit for configuration %s reached; will not kick.",
config.name
);
res.status(429).end();
return;
}
// Run the container
docker
.run(match.image, match.cmd, process.stdout, createOptions)
.then(function (data) {
var output = data[0];
var container = data[1];
logger.debug({ output });
return container.remove();
})
.then(function (data) {
untrackRunningInstance(match, createOptions.name);
logger.info("Container %s removed", createOptions.name);
})
.catch(function (err) {
untrackRunningInstance(match, createOptions.name);
logger.error(err);
});
});
} catch (err) {
logger.error(err);
}
res.status(200).end();
});
// Startup
// Read connection info
let connectConfig
fs.readFile(
process.env.KICKER_CONNECTCONFIG || "connect-config.json",
"utf8",
function (err, data) {
connectConfig = JSON.parse(data);
docker = new Docker(connectConfig.docker);
// Proxy
//app.use(function (req, res, next) {
// req.ipInfo = ipware.getClientIP(req, {
// proxy: connectConfig.proxy,
// });
// logger.info("ipWareResult: %s", req.ipInfo?.ip)
// next();
//});
// Read configuration
fs.readFile(
process.env.KICKER_CONFIG || "kicker-config.json",
"utf8",
function (err, data) {
const configRaw = JSON.parse(data);
if (!Array.isArray(configRaw)) {
throw "Invalid config.";
}
config = configRaw.map((r) => new ConfigEntry(r));
tryValidateConfig(config);
// Everything seems to be working... start listening...
const port = process.env.KICKER_PORT || 41331;
app.listen(port, () => {
logger.info(`Docker Kicker listening on port ${port}`);
});
}
);
}
);