diff options
author | RaindropsSys <raindrops@equestria.dev> | 2024-03-29 22:05:35 +0100 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2024-03-29 22:05:35 +0100 |
commit | 5860551daa0f60103ad24e93da29f401a653f144 (patch) | |
tree | 9ad97e04152a6edc11d5096c87880978df39086a /includes/external/pair/index.js | |
parent | a51979ad60074db84af78a06d30fcb888ccb0b03 (diff) | |
download | pluralconnect-5860551daa0f60103ad24e93da29f401a653f144.tar.gz pluralconnect-5860551daa0f60103ad24e93da29f401a653f144.tar.bz2 pluralconnect-5860551daa0f60103ad24e93da29f401a653f144.zip |
Updated 20 files, added 6 files, deleted 144 files and renamed .idea/ponycule.iml (automated)
Diffstat (limited to 'includes/external/pair/index.js')
-rw-r--r-- | includes/external/pair/index.js | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/includes/external/pair/index.js b/includes/external/pair/index.js deleted file mode 100644 index f933232..0000000 --- a/includes/external/pair/index.js +++ /dev/null @@ -1,117 +0,0 @@ -const { WebSocketServer } = require('ws'); -const crypto = require('crypto'); -const version = 1; - -const wss = new WebSocketServer({ port: 12378 }); -// wss://ponies.equestria.horse/_PairingServices-WebSocket-EntryPoint/socket - -let clients = {}; - -function generateCode() { - return crypto.randomBytes(32).toString("base64").replace(/[\/=+]/gm, "").substring(0, 5).toUpperCase(); -} - -wss.on('connection', (ws, req) => { - ws.code = generateCode(); - ws.ipAddress = req.headers['x-forwarded-for'] ? req.headers['x-forwarded-for'].split(',')[0].trim() : req.socket.remoteAddress; - - ws.on('error', (e) => { - console.error(e); - ws.close(); - }); - - ws.on('close', () => { - if (clients[ws.code]) { - delete clients[ws.code]; - } - }); - - ws.on('message', function message(raw) { - try { - let data = JSON.parse(raw.toString()); - - switch (data.type) { - case "fetch": - if (clients[data.code ?? ""] && clients[data.code ?? ""].socket) { - clients[data.code ?? ""].socket.send(JSON.stringify({ - type: "preflight", - identity: { - name: data.identity ? (data.identity.name ?? "<Unknown name>") : "<Unknown name>", - platform: data.identity ? (data.identity.platform ?? "<Unknown device>") : "<Unknown device>" - } - })); - ws.send(JSON.stringify({ - type: "device", - identity: { - name: clients[data.code ?? ""].name, - address: clients[data.code ?? ""].socket.ipAddress - } - })) - } else { - ws.send(JSON.stringify({ - type: "invalid" - })); - } - break; - - case "confirm": - if (clients[data.code ?? ""] && clients[data.code ?? ""].socket && data.token && ws.token) { - clients[data.code ?? ""].socket.send(JSON.stringify({ - type: "confirm", - token: data.token - })); - - delete clients[data.code ?? ""]; - } else { - ws.send(JSON.stringify({ - type: "invalid" - })); - } - break; - - case "reject": - if (clients[data.code ?? ""] && clients[data.code ?? ""].socket && ws.token) { - clients[data.code ?? ""].socket.send(JSON.stringify({ - type: "reject", - token: null - })); - - delete clients[data.code ?? ""]; - } else { - ws.send(JSON.stringify({ - type: "invalid" - })); - } - break; - - case "init": - if (data.token) { - ws.token = data.token; - } else { - clients[ws.code] = { - socket: ws, - name: (typeof data.name === "string" && data.name.length > 2 && data.name.length < 100) ? data.name : "<Unknown client>" - } - - ws.send(JSON.stringify({ - type: "waiting", - code: ws.code - })) - } - - break; - - default: - ws.close(); - } - } catch (e) { - console.error(e); - ws.close(); - } - }); - - ws.send(JSON.stringify({ - version, - type: "init" - })); -});
\ No newline at end of file |