From 971783dafcb441de323bd282c375830c852a5c75 Mon Sep 17 00:00:00 2001 From: Minteck Date: Wed, 5 May 2021 13:00:23 +0200 Subject: Commit --- demo/host.js | 4 +- demo/security.js | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 8 ++-- 3 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 demo/security.js diff --git a/demo/host.js b/demo/host.js index e91f4c7..8df7100 100644 --- a/demo/host.js +++ b/demo/host.js @@ -20,7 +20,7 @@ function exit() { var net = require('net'); -var host = 'localhost'; +var host = '86.196.89.109'; var port = 8888; var client = new net.Socket(); @@ -121,4 +121,4 @@ client.on('error', (e) => { break; } crash(e); -}) \ No newline at end of file +}) diff --git a/demo/security.js b/demo/security.js new file mode 100644 index 0000000..4681325 --- /dev/null +++ b/demo/security.js @@ -0,0 +1,130 @@ +global.pingStart = null; + +const sampleData = { + _type: "init", + name: "Kartik Core", + version: "21.05.0", + id: null, + moded: false +} +const linkTo = process.argv[2]; +console.log("Will link to client " + linkTo); + +function crash(e) { + console.log("Communication error"); + console.error(e); + process.exit(2); +} + +function exit() { + process.exit(); +} + +var net = require('net'); + +var host = 'localhost'; +var port = 8888; + +var client = new net.Socket(); +client.initialized = false; + +client.connect(port, host, () => { + console.log("Connected to " + host + ":" + port); + client.write(JSON.stringify(sampleData)); +}) + +client.on('data', (data) => { + try { + d = data.toString(); + try { + info = JSON.parse(d); + } catch(e) { + if (e.message.startsWith("Unexpected token")) { + info = JSON.parse(d.substr(0, e.message.split(" ")[e.message.split(" ").length - 1])); + } + } + } catch (e) { + crash(e) + } + if (typeof info['_type'] != "string") { + crash(new Error("Invalid JSON data")); + } + if (!client.initialized) { + switch (info['_type']) { + case "init": + if (info['name'] !== "Kartik Server") { + crash(new Error("Invalid server")); + } + console.log("Connection initialized. Server running " + info.name + " version " + info.version + ", client ID " + info.id); + client.initialized = true; + console.log("Linking to client " + linkTo + "...") + client.write(JSON.stringify({ + _type: "link", + client: linkTo + })); + break; + case "error": + console.log(info['type'] + ": " + info['message']); + break; + default: + crash(new Error("Trying to receive data but client not initialized")); + break; + } + } else { + switch (info['_type']) { + case "init": + crash(new Error("Trying to initialize client but client is already initialized")); + break; + case "error": + console.log(info['type'] + ": " + info['message']); + break; + case "linked": + console.log("Now hooked into link: (H) " + info['ids']['host'] + " <-> " + info['ids']['guest'] + " (G)"); + setInterval(() => { + client.write(JSON.stringify({ + _type: "ipc", + action: "Ping", + message: null + })) + global.pingStart = new Date(); + }, 1000) + break; + default: + if (info['_type'] === "ipc" && info['action'] === "Ping") { + client.write(JSON.stringify({ + _type: "ipc", + action: "Pong", + message: null + })) + return; + } + if (info['_type'] === "ipc" && info['action'] === "Pong") { + pingEnd = new Date(); + ping = Math.round(pingEnd - pingStart); + global.pingStart = null; + console.log("Ping: " + ping + " ms"); + return; + } + console.log("Data:"); + console.dir(info); + break; + } + } +}) + +client.on('close', () => { + console.log("Kicked from server"); + exit(); +}) + +client.on('error', (e) => { + switch (e.code) { + case "ECONNREFUSED": + console.log("Unable to connect to server"); + break; + default: + console.log("Internal error"); + break; + } + crash(e); +}) diff --git a/index.js b/index.js index 86091c1..cf1e65f 100644 --- a/index.js +++ b/index.js @@ -269,7 +269,7 @@ server.on('connection', (socket) => { ) + "\n") break; case "link": - if (typeof info['client'] !== "string" || isNaN(parseInt(info['client'], 16))) { + if (typeof info['client'] !== "string" || isNaN(parseInt(info['client'], 36))) { throw new KartikError("Invalid client link ID", "net.minteckprojects.kartik.KartikServer.GuestIdentifierException"); } if (typeof clients[info['client']] === "undefined") { @@ -309,7 +309,7 @@ server.on('connection', (socket) => { if (socket.linkedTo === null) { throw new KartikError("Client not linked to another client", "net.minteckprojects.kartik.KartikServer.DataRoutingException"); } else { - socket.linkedTo.write(JSON.stringify(info) + "\n"); + socket.linkedTo.write(JSON.stringify(info).replaceAll("<", "-").replaceAll(">", "-") + "\n"); } } } @@ -321,8 +321,8 @@ server.on('connection', (socket) => { } socket.write(JSON.stringify({ _type: "error", - message: e.message, - type: e.ktype + message: e.message.replaceAll("<", "-").replaceAll(">", "-"), + type: e.ktype.replaceAll("<", "-").replaceAll(">", "-") }) + "\n") socket.end(); } -- cgit