summaryrefslogtreecommitdiff
path: root/server/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/server.js')
-rwxr-xr-xserver/server.js65
1 files changed, 62 insertions, 3 deletions
diff --git a/server/server.js b/server/server.js
index 529f42a..01c61b0 100755
--- a/server/server.js
+++ b/server/server.js
@@ -6,9 +6,50 @@ const fs = require('fs');
const crypto = require('crypto');
const zlib = require("zlib");
-const version = "1.8-dev";
+const version = "1.9-prod";
const port = 27342;
+function mergeRecursive(obj1, obj2) {
+ for (const p in obj2) {
+ try {
+ if (obj2[p].constructor === Object) {
+ obj1[p] = mergeRecursive(obj1[p], obj2[p]);
+ } else {
+ obj1[p] = obj2[p];
+ }
+ } catch (e) {
+ obj1[p] = obj2[p];
+ }
+ }
+
+ return obj1;
+}
+
+function loadLang(lang) {
+ let language = lang ?? "en-US";
+
+ try {
+ return mergeRecursive(require('../shared/lang/en-US.json'), require('../shared/lang/' + language.replaceAll("/", "-") + ".json"));
+ } catch (e) {
+ return require('../shared/lang/en-US.json');
+ }
+}
+
+function l(path, lang) {
+ let line = 'langData["' + path.split("/").map(i => i.replaceAll('"', "'")).join('"]["') + '"]';
+ let langData = loadLang(lang);
+
+ try {
+ if (eval(line)) {
+ return eval(line);
+ } else {
+ return path;
+ }
+ } catch (e) {
+ return path;
+ }
+}
+
function log(message, address) {
if (address) {
console.log(`[${new Date().toISOString()}] <${address}> ${message}`);
@@ -83,6 +124,7 @@ readline.question("Enter the IP address users would use to connect to the server
wss.on('connection', function connection(ws, req) {
log("Connection from " + req.socket.remoteAddress);
ws.publicKey = null;
+ ws.lang = "en-US";
ws.id = uuid();
ws.address = parseAddress(req.socket.remoteAddress);
ws.lastPacket = 0;
@@ -614,8 +656,23 @@ readline.question("Enter the IP address users would use to connect to the server
let _decrypted = JSON.parse(decrypted);
_decrypted.admin = !(ws.address !== "::1" && !ws.address.startsWith("127.") && !operators.includes(ws.address));
- decrypted = JSON.stringify(_decrypted);
+ if (_decrypted["type"] === "system") {
+ try {
+ let sData = JSON.parse(_decrypted['text']);
+ let txt = l(sData['message'], socket.lang);
+
+ for (let substitute of sData['sub']) {
+ txt = txt.replace(substitute[0], substitute[1]);
+ }
+
+ _decrypted["text"] = txt;
+ } catch (e) {
+ console.error(e);
+ }
+ }
+
+ decrypted = JSON.stringify(_decrypted);
data.message = encrypt(decrypted, socket.publicKey);
socket.send(JSON.stringify(data));
@@ -623,6 +680,8 @@ readline.question("Enter the IP address users would use to connect to the server
}
}
} else if (data.type === "ping") {
+ ws.lang = data.lang;
+
ws.send(JSON.stringify({
type: "pong",
admin: !(ws.address !== "::1" && !ws.address.startsWith("127.") && !operators.includes(ws.address)),
@@ -654,7 +713,7 @@ readline.question("Enter the IP address users would use to connect to the server
message: encrypt(JSON.stringify({
type: "system",
date: new Date().getTime(),
- text: address + " left the chat"
+ text: l("status/leave2", socket.lang).replace("%1", address)
}), socket.publicKey)
}));
} catch (e) {}