-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
294 lines (229 loc) · 15.3 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
const fs = require('fs');
const nodepath = require('path');
const _ = require('lodash');
const yaml = require('js-yaml');
const glob = require('glob');
const yargs = require('yargs/yargs');
const execa = require('execa');
const axios = require('axios').default;
//
//const XT = require('/DEV/CODE/xtdev/node_modules/@cogsmith/xt').Init();
const XT = require('@cogsmith/xt').Init();
const App = XT.App; const LOG = XT.LOG;
//
const fastify = require('fastify')({
logger: true, maxParamLength: 999, ignoreTrailingSlash: false,
});
App.InitArgs = function () {
App.Argy = yargs(process.argv).wrap(125)
.usage("\n" + App.Meta.Full + "\n\n" + 'USAGE: node $0 [options]')
.epilog('DT: ' + new Date().toISOString() + "\n\n" + process.argv.join(' ') + "\n")
.demandOption(['ip', 'port']) // ,'hivepath','hive'])
.describe('v', 'Logging Level').default('v', 0).alias('v', 'verbose').count('verbose')
.describe('ip', 'Bind IP').default('ip', process.env.HOST || '127.0.0.1')
.describe('port', 'Bind Port').default('port', process.env.PORT || 99)
.describe('hivepath', 'Hive Path').default('hivepath', '/hive')
.describe('hive', 'Hive ID Name').default('hive', undefined)
.describe('do', 'Action').default('do', 'run')
.describe('cell', 'Cell ID')
.describe('hiveip', 'Hive Public IP').default('hiveip', '127.0.0.1')
.describe('hivebind', 'Hive Bind IP').default('hivebind', '127.0.0.1')
.describe('admin', 'Admin IP').default('adminip', null).array('adminip');
let AppArgs = App.Argy.argv;
App.Args = AppArgs;
App.AdminIP = AppArgs.adminip;
App.Requests = 0;
App.Clients = {};
App.Port = AppArgs.port;
App.IP = AppArgs.ip;
App.Hive = AppArgs.hive;
App.HivePath = AppArgs.hivepath;
App.Do = AppArgs.do.toUpperCase();
App.Cell = AppArgs.cell;
App.HiveIP = AppArgs.hiveip;
App.HiveBind = AppArgs.hivebind;
}
App.InitData = function () {
App.PortFirst = 9000;
App.PortNext = App.PortFirst + 1;
App.PortGet = function () { return App.PortNext++; }
App.PortDB = {};
App.CellDB = {};
}
App.RunInit = function () {
LOG.DEBUG('App.RunInit');
try {
let cmdpurge = 'docker container stop $(docker container ls -a -q --filter name=ZX_' + App.Hive + '_*) ; docker container rm $(docker container ls -a -q --filter name=ZX_' + App.Hive + '_*)' + ' ; ' + 'sleep 1';
execa.commandSync(cmdpurge, { shell: true }).stdout.pipe(process.stdout);
} catch (ex) { };
fastify.register(require('fastify-compress'));
fastify.addHook('onRequest', (req, rep, nxt) => {
let reqip = req.socket.remoteAddress;
App.Requests++; if (!App.Clients[reqip]) { App.Clients[reqip] = 1; } else { App.Clients[reqip]++; }
nxt();
});
fastify.get('/', function (req, rep) { rep.send('ZX'); });
fastify.get('/zx/hive/load', function (req, rep) { App.Load(req.query.cell); rep.send({ AX: 'ZX.Hive.Load', Q: req.query }); });
fastify.get('/zx/hive/stop', function (req, rep) { App.Stop(req.query.cell); rep.send({ AX: 'ZX.Hive.Stop', Q: req.query }); });
fastify.get('/zx/hive/nuke', function (req, rep) { App.Nuke(); rep.send({ AX: 'ZX.Hive.Nuke', Q: req.query }); });
fastify.get('/zx/db/json', function (req, rep) { rep.send({ CellDB: App.CellDB, PortDB: App.PortDB }); });
fastify.listen(App.Port, App.IP, (err, address) => { if (err) { LOG.ERROR(err); throw err; } else { LOG.DEBUG('App.RunInit:Done'); App.RunMain(); } });
}
App.Nuke = function () {
execa.commandSync('docker container stop $(docker container ls -q --filter name=ZX*)', { shell: true }).stdout.pipe(process.stdout);
execa.commandSync('docker container rm $(docker container ls -q --filter name=ZX*)', { shell: true }).stdout.pipe(process.stdout);
}
App.Stop = function (cell) {
if (App.CellDB[cell]) {
LOG.INFO('App.Stop = ' + cell);
try {
execa.commandSync('docker container stop $(docker container ls -q --filter name=ZX_' + App.Hive + '_' + App.CellDB[cell].Port + ')', { shell: true });
execa.commandSync('docker container rm $(docker container ls -q --filter name=ZX_' + App.Hive + '_' + App.CellDB[cell].Port + ')', { shell: true });
} catch (ex) { }
}
}
App.LoadCell = function (cell) {
LOG.DEBUG('App.LoadCell: ' + cell);
let cz = cell.split('/');
let slug = cz[0];
let host = App.GetSlugHost(slug); let slughost = host;
let type = 'HTML';
let base = '/';
let path = App.HivePath + '/' + App.Hive + '/' + slug + '/' + cz.slice(1).join('/');
if (fs.existsSync('/hive' + '/' + cell + '/' + 'app.js')) { type = 'APPJS'; }
if (fs.existsSync('/hive' + '/' + cell + '/' + 'docker.run')) { type = 'DOCKER-RUN'; }
if (fs.existsSync('/hive' + '/' + cell + '/' + 'GOTO.URL')) { type = 'GOTO-URL'; }
let port = 0; if (App.CellDB[cell]) { port = App.CellDB[cell].Port; } else if (type == 'HTML' && cell.substr(-1) == '@') { port = 88; } else { port = App.PortGet(); }
let z = { Port: port, Slug: slug, Host: host, Type: type, Base: base, Path: path, Cell: cell };
if (z.Type == 'DOCKER-RUN') { z.Run = fs.readFileSync('/hive' + '/' + cell + '/' + 'docker.run') + ''; z.Run = z.Run.replace(/\n/g, ''); }
if (z.Type == 'GOTO-URL') { z.GotoURL = fs.readFileSync('/hive' + '/' + cell + '/' + 'GOTO.URL') + ''; }
App.PortDB[port] = z;
App.CellDB[cell] = z;
let RUN = [];
let runrun = false;
let cellpath = '/hive' + '/' + cell + '/';
let celltag = nodepath.basename(cellpath).toUpperCase();
let cellbase = nodepath.basename(cellpath);
if (celltag == '@') { celltag = slughost.toLowerCase(); cellbase = '/'; }
else { celltag = slughost.toLowerCase() + '/' + nodepath.basename(cellpath); }
let dockid = 'ZX_' + App.Hive + '_' + z.Port;
let dockimg = ''; if (z.Type == 'DOCKER-RUN') { dockimg = z.Run.split(' ')[0]; }
if (z.Type == 'HTML' && z.Port != 88) { runrun = "docker stop " + dockid + " ; docker wait " + dockid + " ; sleep 1 ; docker rm -f " + dockid + " ; sleep 1 ; docker run -d -t --restart always --name " + dockid + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " --env HOST=0.0.0.0 --env PORT=9 --env CELLTAG=" + celltag + " -p " + App.HiveBind + ":" + z.Port + ":9 -v " + z.Path + ":/www cogsmith/webhost --port 9 --ip 0.0.0.0 --www /www --base /"; let basepath = cz.join('/').replace(slug + '/web/raw/', ''); if (basepath != '@') { runrun += basepath + '/' }; RUN.push(runrun); }
if (z.Type == 'DOCKER-RUN') { RUN.push("docker stop " + dockid + " ; docker wait " + dockid + " ; sleep 1 ; docker rm -f " + dockid + " ; sleep 1 ; docker run -d -t --restart always --name " + dockid + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " --env HOST=0.0.0.0 --env PORT=9 --env CELLTAG=" + celltag + " -p " + App.HiveBind + ":" + z.Port + ":9 -v " + z.Path + "/data:/app/data " + z.Run + " --port 9 --ip 0.0.0.0"); }
//if (z.Type == 'APPJS') { RUN.push("docker stop " + dockid + " ; docker rm " + dockid + " ; docker run --restart always --name " + dockid + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " --env HOST=0.0.0.0 --env PORT=9 -p " + App.HiveBind + ":" + z.Port + ":9 -v " + z.Path + ":/app node node /app/app.js --port 9 --ip 0.0.0.0"); }
//if (z.Type == 'APPJS') { RUN.push("docker stop " + dockid + " ; docker rm " + dockid + " ; docker run --restart always --name " + dockid + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " --env HOST=0.0.0.0 --env PORT=9 -p " + App.HiveBind + ":" + z.Port + ":9 -v " + z.Path + ":/app cogsmith/nodemon nodemon /app/app.js --port 9 --ip 0.0.0.0 --loglevel trace"); }
//if (z.Type == 'APPJS') { if (!fs.existsSync(cellpath + '/package.json')) { RUN.push('echo \'{"dependencies":{"@cogsmith/xt":"*"}}\' > package.json'); } }
//if (z.Type == 'APPJS') { if (!fs.existsSync(cellpath + '/package.json')) { LOG.INFO('APPJS_NOPACKAGE'); fs.writeFileSync(cellpath + '/package.json', JSON.stringify({ dependencies: { '@cogsmith/xt': '*' } })); } }
let donpmi = 'npm install';
if (z.Type == 'APPJS') { if (!fs.existsSync(cellpath + '/package.json')) { donpmi = 'echo APPJS_NOPACKAGE'; LOG.INFO('APPJS_NOPACKAGE'); } }
if (z.Type == 'APPJS') { RUN.push("cd " + cellpath + " ; docker stop " + dockid + " ; docker wait " + dockid + " ; sleep 1 ; docker rm -f " + dockid + " ; " + donpmi + " ; docker run -d -t --restart always --name " + dockid + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " --env HOST=0.0.0.0 --env PORT=9 --env CELLBASE=" + cellbase + " --env CELLTAG=" + celltag + " -p " + App.HiveBind + ":" + z.Port + ":9 -v " + z.Path + ":/app -v /hive/WX/node_modules:/node_modules cogsmith/nodemon nodemon --delay 2.5 --ignore package.json --ignore package-lock.json /app/app.js --port 9 --ip 0.0.0.0 --loglevel trace"); }
//RUN.forEach(x => { LOG.DEBUG('LoadCell.CMD: ' + cell + "\n" + x); execa.command(x, { shell: true }).stdout.pipe(process.stdout); });
//RUN.forEach(x => { LOG.DEBUG('LoadCell.CMD: ' + cell + "\n" + x); let stdout = execa.commandSync(x, { shell: true }).stdout; console.log(stdout); });
RUN.forEach(x => { LOG.DEBUG('LoadCell.CMD: ' + cell + "\n" + x); let stdout = execa.commandSync(x, { shell: true }).stdout; console.log(stdout); execa.command('docker logs -f ' + dockid, { shell: true }).stdout.pipe(process.stdout); });
let map = {};
let kz = Object.keys(App.CellDB);
kz.forEach((k) => {
let z = App.CellDB[k];
k = k.replace('/web/raw/@', '').replace('/web/raw/_', '/_').replace('/web/raw/', '/').replace('/web/app/@', '').replace('/web/app/_', '/_').replace('/web/app/', '/');
//let kk = App.GetSlugHost(k.toLowerCase());
let kk = App.GetSlugHost(k);
//console.log('K = ' + k + ' || ' + 'KK = ' + kk);
if (kk.startsWith('.')) { kk = kk.substr(1); }
if (kk.includes('/')) { kk = kk + '/*'; }
// map[kk] = (!k.substr(-1) == '!' ? '@' : '') + 'http://' + App.HiveBind + ':' + z.Port;
//map[mapkey] = (!k.includes('/') ? '@' : '') + 'http://' + App.HiveBind + ':' + z.Port;
let mapkey = kk; // if (!kk.includes('/')) { } else { mapkey += '/*' };
// map[mapkey] = z.GotoURL || '@' + 'http://' + App.HiveBind + ':' + z.Port;
map[mapkey] = z.GotoURL || '^' + 'http://' + App.HiveBind + ':' + z.Port;
//console.log('K = ' + k + ' || ' + 'KK = ' + kk + ' || ' + 'MAPKEY = ' + mapkey);
});
fs.mkdirSync('/hive/WEBGATE/MAPS', { recursive: true }); fs.writeFileSync('/hive/WEBGATE/MAPS/HIVE.MAP', yaml.dump(map));
}
//
App.GetHostSlug = function (host) { if (!host) { return host; } let slug = host.replace(/\./g, '_').toUpperCase(); let z = slug.split('_'); if (z.length >= 3) { slug = z.slice(-2).join('_') + '_' + z.slice(0, z.length - 2).reverse().join('_'); }; return slug; };
App.GetSlugHost = function (slug) { if (!slug) { return slug; } let host = slug.split('/')[0].replace(/_/g, '.'); let path = slug.split('/').slice(1).join('/') || ''; let z = host.split('.'); if (z.length >= 2) { host = z.slice(2).reverse().join('.') + '.' + z.slice(0, 2).join('.'); }; return host + (path ? '/' + path : ''); }
//
App.LoadSlug = function (slug) {
let slugpath = '/hive' + '/' + slug;
let host = App.GetSlugHost(slug);
LOG.INFO('App.LoadSlug: ' + slug + ' @ ' + slugpath);
let dirsraw = []; try { dirsraw = fs.readdirSync(slugpath + '/web/raw') } catch (ex) { }
for (let i = 0; i < dirsraw.length; i++) {
let x = dirsraw[i];
if (glob.sync(slugpath + '/web/raw/' + x + '/' + '*.html').length > 0) { App.LoadCell(slug + '/' + 'web/raw' + '/' + x); }
if (fs.existsSync(slugpath + '/web/raw/' + x + '/' + 'GOTO.URL')) { App.LoadCell(slug + '/' + 'web/raw' + '/' + x); }
}
let dirsapp = []; try { dirsapp = fs.readdirSync(slugpath + '/web/app') } catch (ex) { };
for (let i = 0; i < dirsapp.length; i++) {
let x = dirsapp[i];
if (fs.existsSync(slugpath + '/web/app/' + x + '/' + 'app.js')) { App.LoadCell(slug + '/' + 'web/app' + '/' + x); }
if (fs.existsSync(slugpath + '/web/app/' + x + '/' + 'docker.run')) { App.LoadCell(slug + '/' + 'web/app' + '/' + x); }
}
}
App.LoadAll = function () {
LOG.INFO('App.LoadAll: ' + App.Hive);
let hivepath = '/hive'; // let hivepath = App.HivePath;
let slugs = fs.readdirSync(hivepath);
for (let i = 0; i < slugs.length; i++) {
let slug = slugs[i]; let host = App.GetSlugHost(slug);
if (slug == 'ACME') { continue; }
if (slug == 'WWW') { continue; }
if (slug == 'ZWWW') { continue; }
if (slug == 'WEBGATE') { continue; }
if (slug == 'node_modules') { continue; }
if (slug == 'package-lock.json') { continue; }
if (slug == 'SYNC.CMD') { continue; }
if (slug == 'package.json') { continue; }
/*
if (slug.startsWith('hive.')) { continue; }
if (slug.startsWith('proxy.') || slug.startsWith('proxyauto.')) { continue; }
if (slug.startsWith('redirect.') || slug.startsWith('redirectauto.')) { continue; }
if (slug == 'ports.json') { continue; }
*/
App.LoadSlug(slug);
}
}
App.Load = function (cell) {
LOG.INFO('App.Load: ' + cell);
let slug = cell.split('/')[0];
let slughost = App.GetSlugHost(slug);
LOG.DEBUG([App.HivePath, App.Hive]);
fs.mkdirSync('/hive/WWW/.well-known/acme-challenge', { recursive: true });
fs.writeFileSync('/hive/WWW/.well-known/acme-challenge/acme.txt', 'ACME');
if (cell == 'ALL') {
let cmdnpmi = 'cd /hive ; npm remove @cogsmith/xt ; npm install @cogsmith/xt';
console.log(execa.commandSync(cmdnpmi, { shell: true }).stdout);
let adminips = ''; for (let i = 0; i < App.AdminIP.length; i++) { let ip = App.AdminIP[i]; if (ip) { adminips += '--admin ' + ip + ' ' }; }
let cmd = "docker stop ZXPROXY_" + App.Hive + " ; docker rm ZXPROXY_" + App.Hive + " ; docker run -t --restart always --name ZXPROXY_" + App.Hive + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " -p " + App.HiveBind + ":80:80 -p " + App.HiveBind + ":443:443 -v " + App.HivePath + "/" + App.Hive + ":/webgate cogsmith/webgate " + adminips + " --public " + App.HiveIP + " --private " + App.HiveBind + " --to " + App.HiveBind + ' --mapfile BASE.MAP --mapfile HIVE.MAP --mapfile GOTO.MAP --loglevel trace';
LOG.DEBUG(cmd);
execa.command(cmd, { shell: true }).stdout.pipe(process.stdout);
let cmd88 = "docker stop ZXWEB_" + App.Hive + " ; docker rm ZXWEB_" + App.Hive + " ; docker run -t --restart always --name ZXWEB_" + App.Hive + ' --env HIVESLUG=' + slug + ' --env SLUGHOST=' + slughost.toLowerCase() + " -p " + App.HiveBind + ":88:9 -v " + App.HivePath + "/" + App.Hive + ":/webhost cogsmith/webhost --loglevel trace --port 9 --ip 0.0.0.0 --www /webhost --base / --vhost --xhost";
LOG.DEBUG(cmd88);
execa.command(cmd88, { shell: true }).stdout.pipe(process.stdout);
}
setTimeout(function () {
if (cell == 'ALL') { return App.LoadAll(); }
else if (!cell.split('/')[1]) { return App.LoadSlug(cell); }
else { return App.LoadCell(cell); }
}, 2500);
}
App.CallLoad = function (cell) {
LOG.INFO('App.CallLoad = ' + cell);
axios.get('http://127.0.0.1:99/zx/hive/load', { params: { cell: cell } }).then(function (res) { console.log(res.data); }).catch(function (err) { console.error(err); }); //.then(function () { console.log('AXIOS.THEN'); });
}
App.CallStop = function (cell) {
LOG.INFO('App.CallStop = ' + cell);
axios.get('http://127.0.0.1:99/zx/hive/stop', { params: { cell: cell } }).then(function (res) { console.log(res.data); }).catch(function (err) { console.error(err); }); //.then(function () { console.log('AXIOS.THEN'); });
}
App.Init = function () {
if (App.Do == 'NUKE') { App.Nuke(); }
if (App.Do == 'RUN') { App.RunInit(); }
if (App.Do == 'STOP') { App.CallStop(App.Cell); }
if (App.Do == 'LOAD' || App.Do == 'RELOAD') { App.CallLoad(App.Cell); }
};
App.RunMain = function () {
LOG.INFO('App.RunMain');
if (App.Cell) { App.CallLoad(App.Cell); }
};
//
App.Run();