aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
authorMinteck <nekostarfan@gmail.com>2021-08-27 19:05:00 +0200
committerMinteck <nekostarfan@gmail.com>2021-08-27 19:05:00 +0200
commit35b20bb63dc3852023e32dd14d99cda379685aae (patch)
tree94387570559df789ba4eb65663d37c7f39a39346 /index.js
parentd25e11bee6ca5ca523884da132d18e1400e077b9 (diff)
downloadkartik-iridium-35b20bb63dc3852023e32dd14d99cda379685aae.tar.gz
kartik-iridium-35b20bb63dc3852023e32dd14d99cda379685aae.tar.bz2
kartik-iridium-35b20bb63dc3852023e32dd14d99cda379685aae.zip
Commmmmit!
Diffstat (limited to 'index.js')
-rw-r--r--index.js181
1 files changed, 39 insertions, 142 deletions
diff --git a/index.js b/index.js
index 7fdd071..32647d1 100644
--- a/index.js
+++ b/index.js
@@ -12,13 +12,9 @@ class MessageBuffer {
}
isFinished() {
- if (
- this.buffer.length === 0 ||
- this.buffer.indexOf(this.delimiter) === -1
- ) {
- return true
- }
- return false
+ return this.buffer.length === 0 ||
+ this.buffer.indexOf(this.delimiter) === -1;
+
}
push(data) {
@@ -58,23 +54,37 @@ class KartikError extends Error {
const server = new Net.Server();
server.on('connection', (socket) => {
- socket.connectionId = (Math.random().toString(36).split(".")[1] + Math.random().toString(36).split(".")[1]).substr(0, 8);
+ socket.connectionId = "ird-" + (Math.random().toString().split(".")[1] + Math.random().toString().split(".")[1]).substr(0, 4);
while (Object.keys(clients).includes(socket.connectionId)) {
- socket.connectionId = (Math.random().toString(36).split(".")[1] + Math.random().toString(36).split(".")[1]).substr(0, 8);
+ socket.connectionId = "ird-" + (Math.random().toString().split(".")[1] + Math.random().toString().split(".")[1]).substr(0, 4);
}
socket.linkedTo = null;
clients[socket.connectionId] = socket;
- console.log("New connection " + socket.connectionId)
+ console.log("New connection " + socket.connectionId + " (" + socket.remoteAddress + ")")
- if (clients.length > 280) {
- throw new KartikError("Server is full", "lt.ro.minteck.cutefox.kartik.KartikServer.IdentifierAllocationException");
+ if (Object.keys(clients).length > 280) {
+ socket.write(JSON.stringify(
+ {
+ _type: "init",
+ name: "Kartik Server",
+ version: _version + "-iridium",
+ id: socket.connectionId,
+ modded: null
+ }
+ ) + "\n")
+ socket.write(JSON.stringify({
+ _type: "error",
+ message: "Game server is full, please try again later",
+ type: "E_IRIDIUM_FULL"
+ }) + "\n")
+ return;
}
socket.write(JSON.stringify(
{
_type: "init",
name: "Kartik Server",
- version: _version,
+ version: _version + "-iridium",
id: socket.connectionId,
modded: null
}
@@ -83,12 +93,12 @@ server.on('connection', (socket) => {
setTimeout(() => {
try {
if (socket.linkedTo === null) {
- throw new KartikError("Not linked within 3 minutes", "lt.ro.minteck.cutefox.kartik.KartikServer.ClientConnectTimeoutException");
+ throw new KartikError("Linking timed out", "E_IRIDIUM_LINKTO");
}
} catch (e) {
console.error(e);
if (e.name !== "KartikError") {
- e.ktype = "nodejs.lang." + e.name.replaceAll("Error", "Exception");
+ e.ktype = "E_SYSTEM_" + e.name.toUpperCase().replaceAll("ERROR", "");
}
socket.write(JSON.stringify({
_type: "error",
@@ -99,119 +109,6 @@ server.on('connection', (socket) => {
}
}, 180000)
- /*let received = "";
- socket.on("data", (data) => {
- data = data.toString();
-
- received = received + data.substr(1);
- console.log("{{" + data + "}}");
- if (data.startsWith(":")) {
- return;
- }
- try {
- raw = chunk.toString().replaceAll("}{", "}|{");
-
- datas = raw.split("|").filter(i => i.trim() !== "");
- datas.forEach((data) => {
- try {
- info = JSON.parse(data);
- } catch(e) {
- console.dir(data);
- throw e;
- }
-
- if (data.length > 1200) {
- console.dir(data);
- throw new KartikError("Received data is too long", "lt.ro.minteck.cutefox.kartik.KartikServer.DataLengthException");
- }
-
- if (typeof info['_type'] != "string") {
- throw new KartikError("Invalid JSON data", "lt.ro.minteck.cutefox.kartik.KartikServer.JsonDataException");
- }
- if (!socket.initialized) {
- switch (info['_type']) {
- case "init":
- if (info['name'] !== "Kartik Core") {
- throw new KartikError("Invalid client", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
- }
- if (!info.modded) {
- console.log("Connection initialized. Client running " + info.name + " version " + info.version + ", official client");
- } else {
- console.log("Connection initialized. Client running " + info.name + " version " + info.version + ", MODDED client");
- if (!AllowMods) {
- console.log("Modded clients are not accepted");
- socket.end();
- }
- }
- socket.initialized = true;
- break;
- default:
- throw new KartikError("Trying to receive data but client not initialized", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
- }
- } else {
- switch (info['_type']) {
- case "init":
- throw new KartikError("Trying to initialize client but client is already initialized", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
- case "link":
- if (typeof info['client'] !== "string" || isNaN(parseInt(info['client'], 16))) {
- throw new KartikError("Invalid client link ID", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestIdentifierException");
- }
- if (typeof clients[info['client']] === "undefined") {
- throw new KartikError("Guest client not found", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestConnectException");
- }
- if (clients[info['client']].linkedTo === null) {
- socket.linkedTo = clients[info['client']];
- clients[info['client']].linkedTo = socket;
- socket.linkedTo.role = "host";
- socket.linkedTo.write(JSON.stringify(
- {
- _type: "linked",
- role: "host",
- ids: {
- host: socket.linkedTo.connectionId,
- guest: socket.connectionId
- }
- }
- ))
- socket.role = "guest";
- socket.write(JSON.stringify(
- {
- _type: "linked",
- role: "guest",
- ids: {
- host: socket.linkedTo.connectionId,
- guest: socket.connectionId
- }
- }
- ))
- console.log("Link created: (H) " + socket.connectionId + " <-> " + socket.linkedTo.connectionId + " (G)")
- } else {
- throw new KartikError("Client already linked to another client", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestAllocationException")
- }
- break;
- default:
- if (socket.linkedTo === null) {
- throw new KartikError("Client not linked to another client", "lt.ro.minteck.cutefox.kartik.KartikServer.DataRoutingException");
- } else {
- socket.linkedTo.write(JSON.stringify(info));
- }
- }
- }
- })
- } catch (e) {
- console.error(e);
- if (e.name !== "KartikError") {
- e.ktype = "nodejs.lang." + e.name.replaceAll("Error", "Exception");
- }
- socket.write(JSON.stringify({
- _type: "error",
- message: e.message,
- type: e.ktype
- }))
- socket.end();
- }
- })*/
-
let received = new MessageBuffer("\n")
socket.on("data", data => {
received.push(data)
@@ -225,23 +122,21 @@ server.on('connection', (socket) => {
try {
info = JSON.parse(data);
} catch(e) {
- console.dir(data);
throw e;
}
if (data.length > 1200) {
- console.dir(data);
- throw new KartikError("Received data is too long", "lt.ro.minteck.cutefox.kartik.KartikServer.DataLengthException");
+ throw new KartikError("Payload too large", "E_IRIDIUM_PLSIZE");
}
if (typeof info['_type'] != "string") {
- throw new KartikError("Invalid JSON data", "lt.ro.minteck.cutefox.kartik.KartikServer.JsonDataException");
+ throw new KartikError("Payload syntax error", "E_IRIDIUM_PLSYNTAX");
}
if (!socket.initialized) {
switch (info['_type']) {
case "init":
if (info['name'] !== "Kartik Core") {
- throw new KartikError("Invalid client", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
+ throw new KartikError("Client brand not supported", "E_IRIDIUM_BRAND");
}
if (!info.modded) {
console.log("Connection initialized. Client running " + info.name + " version " + info.version + ", official client");
@@ -255,12 +150,12 @@ server.on('connection', (socket) => {
socket.initialized = true;
break;
default:
- throw new KartikError("Trying to receive data but client not initialized", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
+ throw new KartikError("Payload received too early", "E_IRIDIUM_PLEARLY");
}
} else {
switch (info['_type']) {
case "init":
- throw new KartikError("Trying to initialize client but client is already initialized", "lt.ro.minteck.cutefox.kartik.KartikServer.AuthenticationException");
+ throw new KartikError("Initialization already completed", "E_IRIDIUM_REINIT");
case "ping":
socket.write(JSON.stringify(
{
@@ -270,10 +165,10 @@ server.on('connection', (socket) => {
break;
case "link":
if (typeof info['client'] !== "string" || isNaN(parseInt(info['client'], 36))) {
- throw new KartikError("Invalid client link ID", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestIdentifierException");
+ throw new KartikError("Invalid initial payload data", "E_IRIDIUM_PLINIT");
}
if (typeof clients[info['client']] === "undefined") {
- throw new KartikError("Guest client not found", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestConnectException");
+ throw new KartikError("No such client", "E_IRIDIUM_NOTFOUND");
}
if (clients[info['client']].linkedTo === null) {
socket.linkedTo = clients[info['client']];
@@ -302,12 +197,12 @@ server.on('connection', (socket) => {
) + "\n")
console.log("Link created: (H) " + socket.connectionId + " <-> " + socket.linkedTo.connectionId + " (G)")
} else {
- throw new KartikError("Client already linked to another client", "lt.ro.minteck.cutefox.kartik.KartikServer.GuestAllocationException")
+ throw new KartikError("Client is already linked", "E_IRIDIUM_ALLOC")
}
break;
default:
if (socket.linkedTo === null) {
- throw new KartikError("Client not linked to another client", "lt.ro.minteck.cutefox.kartik.KartikServer.DataRoutingException");
+ throw new KartikError("Client is not linked", "E_IRIDIUM_ROUTING");
} else {
socket.linkedTo.write(JSON.stringify(info).replaceAll("<", "-").replaceAll(">", "-") + "\n");
}
@@ -315,9 +210,11 @@ server.on('connection', (socket) => {
}
})
} catch (e) {
- console.error(e);
if (e.name !== "KartikError") {
- e.ktype = "nodejs.lang." + e.name.replaceAll("Error", "Exception");
+ console.error(e);
+ e.ktype = "E_SYSTEM_" + e.name.toUpperCase().replaceAll("ERROR", "");
+ } else {
+ console.error(e.ktype + ": " + e.message)
}
socket.write(JSON.stringify({
_type: "error",
@@ -363,5 +260,5 @@ server.on('connection', (socket) => {
})
server.listen(ServerPort, () => {
- console.log("Kartik Server " + _version + " listening for connections on 0.0.0.0:" + ServerPort)
+ console.log("Iridium " + _version + " listening for connections on 0.0.0.0:" + ServerPort)
})