-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagentname2servername.js
65 lines (61 loc) · 2.45 KB
/
agentname2servername.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
'use strict';
/**********************************************************************
* Copyright (C) 2025 BitCtrl Systems GmbH
*
* agentname2servername.js
*
* @author Daniel Hammerschmidt <[email protected]>
* @author Daniel Hammerschmidt <[email protected]>
* @version 0.0.3
*********************************************************************/
const { PLUGIN_SHORT_NAME, pluginConfig } = require('../pluginhookscheduler')({
__dirname,
requiredPluginHooks: ['hook_afterCreateMeshAgent'],
});
pluginConfig.include = new Set(pluginConfig.include);
pluginConfig.exclude = new Set(pluginConfig.exclude);
let meshserver, webserver;
const kReset = Symbol(PLUGIN_SHORT_NAME + '/reset');
module.exports = {
[PLUGIN_SHORT_NAME]: function (pluginHandler) {
meshserver = pluginHandler.parent;
return {
server_startup: function () {
webserver = meshserver.webserver;
},
hook_afterCreateMeshAgent(meshagent, parent, db, ws, req, args, domain) {
const { include, exclude } = pluginConfig;
let busy = 0;
// let ii = 0;
ws.on('message', function listener(data) {
const { authenticated, agentName, dbMeshKey, dbNodeKey } = meshagent;
if (authenticated < 1) { return; }
// console.log(++ii, busy, authenticated, [data[0],data[1],data[2],data[3]], meshagent.dbMeshKey, meshagent.dbNodeKey);
if (this[kReset]?.mesh?.flags && !--busy) { this[kReset].mesh.flags &= ~2; }
if (authenticated > 1) { return void ws.off('message', listener); }
try {
const mesh = webserver.meshes[dbMeshKey];
const domain = dbMeshKey.split('/')[1];
const keys = [dbMeshKey, dbNodeKey, 'mesh/' + domain + '/' + mesh.name, 'node/' + domain + '/' + agentName ];
const white = include.size == 0 || keys.some((key)=>(include.has(key)));
const black = keys.some((key)=>(exclude.has(key)));
// console.log(keys);
// console.log(include);
// console.log(exclude);
// console.log([white, black, white && !black]);
if (white && !black) {
++busy;
this[kReset] = { mesh };
mesh.flags |= 2;
meshagent.agentInfo.computerName = agentName;
}
void(0);
} catch (_) {
ws.off('message', listener);
}
});
return meshagent;
},
};
},
};