diff options
author | Minteck <contact@minteck.org> | 2023-02-23 19:34:56 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2023-02-23 19:34:56 +0100 |
commit | 3d1cd02f27518f1a04374c7c8320cd5d82ede6e9 (patch) | |
tree | 75be5fba4368472fb11c8015aee026b2b9a71888 /includes/external/computers/index.js | |
parent | 8cc1f13c17fa2fb5a4410542d39e650e02945634 (diff) | |
download | pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.gz pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.tar.bz2 pluralconnect-3d1cd02f27518f1a04374c7c8320cd5d82ede6e9.zip |
Updated 40 files, added 37 files, deleted 1103 files and renamed 3905 files (automated)
Diffstat (limited to 'includes/external/computers/index.js')
-rw-r--r-- | includes/external/computers/index.js | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/includes/external/computers/index.js b/includes/external/computers/index.js new file mode 100644 index 0000000..4a5115e --- /dev/null +++ b/includes/external/computers/index.js @@ -0,0 +1,91 @@ +const { WebSocketServer } = require('ws'); +const fs = require('fs'); +const uuid = require('uuid-v4'); + +const wss = new WebSocketServer({ port: 38050 }); +const servers = {}; +const clients = {}; + +wss.on('connection', function connection(ws, req) { + ws.linked = null; + ws.id = null; + ws.linkedServer = null; + + console.log("New client: " + req.headers['x-forwarded-for'].split(',')[0].trim()); + + ws.on('message', (_data) => { + if (ws.linked) { + try { + let data = JSON.parse(_data.toString()); + data["_id"] = ws.id; + servers[ws.linked].send(JSON.stringify(data)); + } catch (e) { + console.error(e); + } + } else { + try { + let data = JSON.parse(_data.toString()); + + if (data.type === "client") { + if (Object.keys(servers).includes(data.id)) { + if (fs.existsSync(__dirname + "/../includes/tokens/" + data.token.replaceAll(".", "").replaceAll("/", ""))) { + ws.linked = data.id; + ws.id = uuid(); + + servers[data.id].send(JSON.stringify({ + type: "client", + id: ws.id, + address: req.headers['x-forwarded-for'].split(',')[0].trim(), + name: data.name + })); + + clients[ws.id] = ws; + } else { + console.log("close(c), reason: invalid token"); + ws.close(); + } + } else { + console.log("close(c), reason: no server"); + ws.close(); + } + } else if (data.type === "server") { + if (Object.keys(servers).includes(data.id)) { + console.log("close(s), reason: already online"); + ws.close(); + return; + } + + ws.linkedServer = data.id; + servers[data.id] = ws; + } else if (data._id) { + try { + if (clients[data._id]) { + clients[data._id].send(_data); + } + } catch (e) { + console.error(e); + } + } + } catch (e) { + console.error(e); + } + } + }) + + ws.on('close', () => { + if (ws.linked) { + try { + servers[ws.linked].send(JSON.stringify({ + type: "close", + id: ws.id + })) + } catch (e) { + console.error(e); + } + + delete clients[ws.id]; + } else if (ws.linkedServer) { + delete servers[ws.linkedServer]; + } + }) +});
\ No newline at end of file |