-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
331 lines (324 loc) · 13.8 KB
/
server.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
const WebSocketServer = require('ws').Server;
const WebSocketClient = require('ws');
const { v4: uuidv4 } = require('uuid');
const authMap = new Map();
const funcMaps = new Map();
const wss = new WebSocketServer({ host: Config.LISTEN_HOST, port: Config.LISTEN_PORT });
const ZAES = new ZeroAES(Config.KEY, Config.IV);
const eventMap = new Map();
const netEventMap = new Map();
wss.on('connection', function connection(ws, request) {
ws.uuid = uuidv4();
// console.log('Client Connected: %s', ws.uuid);
ws.on('error', console.error);
ws.on('close', function close() {
// console.log('Client Disconnected: %s', ws.uuid);
authMap.delete(ws.uuid);
});
ws.on('message', function message(data) {
let response = 'Invalid request';
try {
let decrypted = ZAES.decrypt(data);
let json = JSON.parse(decrypted);
if (json && json.action) {
switch (json.action) {
case 'auth':
let auth = json.data.auth;
if (auth !== undefined) {
authMap.set(ws.uuid, ws);
response = JSON.stringify({
action: 'auth',
eid: json.eid,
data: ws.uuid
});
}
break;
case 'executeNative':
let name = json.data.name;
let args = json.data.args;
let result = null;
let resultType = null;
if (name !== undefined && args !== undefined) {
let func = global[name];
if (func !== undefined) {
args = ProcessVector3(args);
result = func.apply(null, args);
resultType = GetResultType(name, result);
}
}
response = JSON.stringify({
action: 'executeNative',
eid: json.eid,
data: {
type: resultType,
result: result
}
});
break;
case 'triggerEvent':
let eventName = json.data.name;
let eventArgs = json.data.args;
if (eventName !== undefined && eventArgs !== undefined) {
eventArgs = ProcessArgs(ws, eventArgs);
// eventArgs = ProcessVector3(eventArgs);
emit(eventName, ...eventArgs);
}
response = JSON.stringify({
action: 'triggerEvent',
eid: json.eid,
data: 'ok'
});
break;
case 'triggerClientEvent':
let clientEventName = json.data.name;
let clientEventArgs = json.data.args;
if (clientEventName !== undefined && clientEventArgs !== undefined && clientEventArgs.length > 0) {
// clientEventArgs = ProcessVector3(clientEventArgs);
emitNet(clientEventName, ...clientEventArgs);
}
response = JSON.stringify({
action: 'triggerClientEvent',
eid: json.eid,
data: 'ok'
});
break;
case 'registerEvent':
let event = json.data.name;
if (event !== undefined && !eventMap.has(event)) {
eventMap.set(event, true);
on(event, function(...args) {
let text = JSON.stringify({
action: 'event',
data: {
name: event,
args: args
}
});
let encrypted = ZAES.encrypt(text);
authMap.forEach(function(client) {
client.send('!' + encrypted + ';\n');
});
});
}
response = JSON.stringify({
action: 'registerEvent',
eid: json.eid,
data: 'ok'
});
break;
case 'registerServerEvent':
let serverEvent = json.data.name;
if (serverEvent !== undefined && !netEventMap.has(serverEvent)) {
netEventMap.set(serverEvent, true);
onNet(serverEvent, function(...args) {
let _source = source;
args.unshift(_source);
let text = JSON.stringify({
action: 'serverEvent',
data: {
name: serverEvent,
args: args,
}
});
let encrypted = ZAES.encrypt(text);
authMap.forEach(function(client) {
client.send('!' + encrypted + ';\n');
});
});
}
response = JSON.stringify({
action: 'registerServerEvent',
eid: json.eid,
data: 'ok'
});
break;
case 'registerCommand':
let command = json.data.name;
let restricted = json.data.restricted || false;
if (command !== undefined) {
RegisterCommand(command, async(source, args, raw) => {
let player = source;
let text = JSON.stringify({
action: 'command',
data: {
name: command,
source: player,
args: args,
raw: raw
}
});
let encrypted = ZAES.encrypt(text);
authMap.forEach(function(client) {
client.send('!' + encrypted + ';\n');
});
}, restricted);
}
response = JSON.stringify({
action: 'registerCommand',
eid: json.eid,
data: 'ok'
});
break;
case 'eval':
let code = json.data.code;
if (code !== undefined) {
try {
var evalResult = eval(code);
var copyResult = DeepCopy(evalResult);
var processedResult = ProcessObject(copyResult);
response = JSON.stringify({
action: 'eval',
eid: json.eid,
data: processedResult
});
} catch (e) {
response = JSON.stringify({
action: 'eval',
eid: json.eid,
data: e.message
});
}
} else {
response = JSON.stringify({
action: 'eval',
eid: json.eid,
data: 'Invalid code'
});
}
break;
case 'callFunction':
let funcId = json.data.id;
let funcArgs = json.data.args;
if (funcId !== undefined && funcArgs !== undefined) {
let func = funcMaps.get(funcId);
if (func !== undefined) {
funcArgs = ProcessArgs(ws, funcArgs);
// funcArgs = ProcessVector3(funcArgs);
let result = func.ref.apply(null, funcArgs);
let resultType = GetResultType(func.ref.name, result);
response = JSON.stringify({
action: 'callFunction',
eid: json.eid,
data: {
type: resultType,
result: result
}
});
} else {
response = JSON.stringify({
action: 'callFunction',
eid: json.eid,
data: 'Invalid function'
});
}
} else {
response = JSON.stringify({
action: 'callFunction',
eid: json.eid,
data: 'Invalid function'
});
}
break;
default:
console.error('Invalid action: %s', json.action);
response = JSON.stringify({
action: 'error',
eid: json.eid,
data: 'Invalid action'
});
break;
}
} else {
console.error('Invalid request: %s', decrypted);
}
} catch (e) {
console.error(e);
}
let encrypted = ZAES.encrypt(response);
ws.send('!' + encrypted + ';\n');
});
});
on('onResourceStop', function(resource) {
if (GetCurrentResourceName() === resource) {
authMap.forEach(function(client) {
let text = JSON.stringify({
action: 'stop',
data: 'ok'
});
let encrypted = ZAES.encrypt(text);
client.send('!' + encrypted + ';\n');
});
}
});
function DeepCopy(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
let copy = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
copy[key] = DeepCopy(obj[key]);
}
}
return copy;
}
function ProcessObject(obj) {
for (let key in obj) {
if (typeof obj[key] === 'function') {
let uuid = uuidv4();
funcMaps.set(uuid, {
expire: Date.now() + 120000,
ref: obj[key]
});
obj[key] = '__FUNCTION__:' + uuid;
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
ProcessObject(obj[key]);
}
}
return obj;
}
function ProcessArgs(ws, args) {
for (let i = 0; i < args.length; i++) {
if (typeof args[i] === 'string' && args[i].startsWith('__FUNCTION__:')) {
let uuid = args[i].substring(13);
if (uuid !== undefined) {
args[i] = function(...args) {
args = ProcessObject(args);
console.log('Calling function: %s', uuid);
let text = JSON.stringify({
action: 'function',
data: {
id: uuid,
args: args
}
});
let encrypted = ZAES.encrypt(text);
ws.send('!' + encrypted + ';\n');
}
}
}
}
return args;
}
function ProcessVector3(data) {
for (let key in data) {
if (typeof data[key] === 'string' && data[key].startsWith('__VECTOR3__:')) {
let parts = JSON.parse(data[key].substring(12));
data[key] = parts; // new Vector3(parseFloat(parts.x), parseFloat(parts.y), parseFloat(parts.z));
} else if (typeof data[key] === 'object' && data[key] !== null) {
ProcessVector3(data[key]);
}
}
return data;
}
function GetResultType(native, result) {
if (Config.VEC3FUNCS.includes(native)) {
return 'vector3';
}
let type = typeof result;
// check is array
if (type === 'object' && Array.isArray(result)) {
return 'array';
}
return type;
}