-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginhookexample.js
More file actions
164 lines (152 loc) · 7.34 KB
/
pluginhookexample.js
File metadata and controls
164 lines (152 loc) · 7.34 KB
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
'use strict';
/**********************************************************************
* Copyright (C) 2025 BitCtrl Systems GmbH
*
* pluginhookexample.js
*
* @author Daniel Hammerschmidt <daniel.hammerschmidt@bitctrl.de>
* @author Daniel Hammerschmidt <daniel@redneck-engineering.com>
* @version 0.0.3
*********************************************************************/
const { PLUGIN_SHORT_NAME } = require('../pluginhookscheduler')({
__dirname,
requiredPluginHooks: [
'hook_beforeCreateMeshAgent',
'hook_afterCreateMeshAgent',
'hook_beforeCreateMeshRelay',
'hook_afterCreateMeshRelay',
'hook_beforeCreateLocalRelay',
'hook_afterCreateLocalRelay',
'hook_beforeCreateMeshUser',
'hook_afterCreateMeshUser',
'hook_beforeNotifyUserOfDeviceStateChange',
'hook_afterNotifyUserOfDeviceStateChange',
'hook_agentWebSocketDisconnected',
],
});
// add hooks to configuration if not in config.json
const { pluginConfig: hookSchedulerConfig } = require('../pluginhookscheduler')({__dirname: __dirname + '../pluginhookscheduler'});
[
'hook_beforeCreateExample', 'hook_afterCreateExample',
'hook_beforeCreateAnotherExample', 'hook_afterCreateAnotherExample',
].forEach((hookname) => {
hookSchedulerConfig.backendhooks.hasOwnProperty(hookname) || (hookSchedulerConfig.backendhooks[hookname] = []);
});
// mimic the MeshCentral API, the same name is used in both namespaces to demonstrate aliases
const example = { namespace1: { handler: {} }, namespace2: { handler:{} } };
example.namespace1.handler.CreateExample = function CreateExample(value) {
var obj = { answer: value };
console.log(new Date().toISOString(), 'namespace1.handler.CreateExample', JSON.stringify([value, obj]));
return obj;
};
example.namespace2.handler.CreateExample = function CreateExample(value) {
var obj = { answer: value };
console.log(new Date().toISOString(), 'namespace2.handler.CreateExample', JSON.stringify([value, obj]));
return obj;
};
// mimic two other plugins
const module1 = { exports: {} }, PLUGIN_1_SHORT_NAME = PLUGIN_SHORT_NAME + '_1';
const module2 = { exports: {} }, PLUGIN_2_SHORT_NAME = PLUGIN_SHORT_NAME + '_2';
// this fake-plugin creates wrapper around tha API functions and hooks into both of them before and after
module1.exports[PLUGIN_1_SHORT_NAME] = function (pluginHandler) {
const PLUGIN_SHORT_NAME = PLUGIN_1_SHORT_NAME;
const meshserver = pluginHandler.parent;
let webserver;
return {
server_startup() {
webserver = meshserver.webserver;
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.server_startup');
pluginHandler.wrapFunctionCall(example.namespace1.handler, 'CreateExample');
pluginHandler.wrapFunctionCall(example.namespace2.handler, 'CreateExample', 'CreateAnotherExample');
// call the API with a delay
setTimeout(() => {
const value1 = 17;
const value2 = 23;
console.log('###');
const obj1 = example.namespace1.handler.CreateExample(value1);
console.log('###');
const obj2 = example.namespace2.handler.CreateExample(value2);
console.log('###');
console.log(new Date().toISOString(), 'Examples created:', JSON.stringify([value1, obj1]), JSON.stringify([value2, obj2]));
console.log('###');
}, 1000);
},
hook_beforeCreateExample(value) {
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_beforeCreateExample', value);
},
hook_afterCreateExample(obj, value) {
obj.answer += 12;
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_afterCreateExample', JSON.stringify([value, obj]));
},
hook_beforeCreateAnotherExample(value) {
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_beforeCreateAnotherExample', value);
},
hook_afterCreateAnotherExample(obj, value) {
obj.answer += 19;
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_afterCreateAnotherExample', JSON.stringify([value, obj]));
},
}
};
// this fake-plugin hooks into both one of the APIs mentioned above as well as the predefined APIs wrapped in pluginhookscheduler
module2.exports[PLUGIN_2_SHORT_NAME] = function (pluginHandler) {
const PLUGIN_SHORT_NAME = PLUGIN_2_SHORT_NAME;
const meshserver = pluginHandler.parent;
let webserver;
return {
server_startup() {
webserver = meshserver.webserver;
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.server_startup');
},
hook_beforeCreateExample(value) {
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_beforeCreateExample', value);
},
hook_afterCreateExample(obj, value) {
obj.answer += 13;
console.log(new Date().toISOString(), PLUGIN_SHORT_NAME + '.hook_afterCreateExample', JSON.stringify([value, obj]));
},
hook_beforeCreateMeshAgent(parent, db, ws, req, args, domain) {
console.log(new Date().toISOString(), 'hook_beforeCreateMeshAgent', req.url, req.socket.remotePort);
},
hook_afterCreateMeshAgent(meshagent, parent, db, ws, req, args, domain) {
return console.log(new Date().toISOString(), 'hook_afterCreateMeshAgent', encodeURIComponent(meshagent.nonce).slice(0, 24)), meshagent;
},
hook_agentCoreIsStable(meshagent) {
console.log(new Date().toISOString(), 'hook_agentCoreIsStable', meshagent.agentName ?? meshagent.name ?? meshagent.nodeid );
},
hook_agentWebSocketDisconnected(meshagent) {
console.log(new Date().toISOString(), 'hook_agentWebSocketDisconnected', meshagent.agentName ?? meshagent.name ?? meshagent.nodeid );
},
hook_beforeCreateMeshRelay(parent, ws, req, domain, user, cookie) {
console.log(new Date().toISOString(), 'hook_beforeCreateMeshRelay', req.url);
},
hook_afterCreateMeshRelay(meshrelay, parent, ws, req, domain, user, cookie) {
return console.log(new Date().toISOString(), 'hook_afterCreateMeshRelay'), meshrelay;
},
hook_beforeCreateLocalRelay(parent, ws, req, domain, user, cookie) {
console.log(new Date().toISOString(), 'hook_beforeCreateLocalRelay', req.url);
},
hook_afterCreateLocalRelay(localrelay, parent, ws, req, domain, user, cookie) {
return console.log(new Date().toISOString(), 'hook_afterCreateLocalRelay'), localrelay;
},
hook_beforeCreateMeshUser(parent, db, ws, req, args, domain, user) {
console.log(new Date().toISOString(), 'hook_beforeCreateMeshUser', req.url);
},
hook_afterCreateMeshUser(meshuser, parent, db, ws, req, args, domain, user) {
return console.log(new Date().toISOString(), 'hook_afterCreateMeshUser'), meshuser;
},
hook_beforeNotifyUserOfDeviceStateChange(__, nodeid, connectTime, connectType, powerState, serverid, stateSet, extraInfo) {
console.log(new Date().toISOString(), 'hook_beforeNotifyUserOfDeviceStateChange', stateSet);
},
hook_afterNotifyUserOfDeviceStateChange(__, meshid, nodeid, connectTime, connectType, powerState, serverid, stateSet, extraInfo) {
return console.log(new Date().toISOString(), 'hook_beforeNotifyUserOfDeviceStateChange'), stateSet;
},
};
};
// this is the export for the real plugin
module.exports = {
[PLUGIN_SHORT_NAME]: function (pluginHandler) {
pluginHandler.plugins[PLUGIN_1_SHORT_NAME] = module1.exports[PLUGIN_1_SHORT_NAME](pluginHandler);
pluginHandler.plugins[PLUGIN_2_SHORT_NAME] = module2.exports[PLUGIN_2_SHORT_NAME](pluginHandler);
return {};
}
};