summaryrefslogtreecommitdiff
path: root/server/server.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/server.js')
-rwxr-xr-xserver/server.js60
1 files changed, 36 insertions, 24 deletions
diff --git a/server/server.js b/server/server.js
index 6860138..1ead0c6 100755
--- a/server/server.js
+++ b/server/server.js
@@ -5,8 +5,19 @@ const uuid = require('uuid-v4');
const fs = require('fs');
const crypto = require('crypto');
const zlib = require("zlib");
+const os = require("os");
-const version = "1.10";
+let localchatDataRoot = (os.platform() === "win32" ? os.homedir() + "/AppData/Roaming" : (os.platform() === "darwin" ? os.homedir() + "/Library/Application Support" : os.homedir())) + "/.localchat";
+
+if (!fs.existsSync(localchatDataRoot)) fs.mkdirSync(localchatDataRoot);
+if (!fs.existsSync(localchatDataRoot + "/server")) fs.mkdirSync(localchatDataRoot + "/server");
+if (!fs.existsSync(localchatDataRoot + "/server/exports")) fs.mkdirSync(localchatDataRoot + "/server/exports");
+if (!fs.existsSync(localchatDataRoot + "/server/config")) fs.mkdirSync(localchatDataRoot + "/server/config");
+if (!fs.existsSync(localchatDataRoot + "/server/logs")) fs.mkdirSync(localchatDataRoot + "/server/logs");
+if (!fs.existsSync(localchatDataRoot + "/server/data")) fs.mkdirSync(localchatDataRoot + "/server/data");
+process.chdir(localchatDataRoot + "/server");
+
+const version = "1.11";
const port = 27342;
function mergeRecursive(obj1, obj2) {
@@ -26,24 +37,25 @@ function mergeRecursive(obj1, obj2) {
}
function loadLang(lang) {
- let language = lang ?? "en-US";
+ let language = lang ?? "en";
try {
- return mergeRecursive(require('../shared/lang/en-US.json'), require('../shared/lang/' + language.replaceAll("/", "-") + ".json"));
+ return mergeRecursive(require('../shared/lang/en.json'), require('../shared/lang/' + language.replaceAll("/", "-") + ".json"));
} catch (e) {
try {
- return mergeRecursive(require('../../shared/lang/en-US.json'), require('../../shared/lang/' + language.replaceAll("/", "-") + ".json"));
+ return mergeRecursive(require('../../shared/lang/en.json'), require('../../shared/lang/' + language.replaceAll("/", "-") + ".json"));
} catch (e) {
try {
- return require('../shared/lang/en-US.json');
+ return require('../shared/lang/en.json');
} catch (e) {
- return require('../../shared/lang/en-US.json');
+ return require('../../shared/lang/en.json');
}
}
}
}
function l(path, lang) {
+ console.log(lang);
let line = 'langData["' + path.split("/").map(i => i.replaceAll('"', "'")).join('"]["') + '"]';
let langData = loadLang(lang);
@@ -79,46 +91,46 @@ const readline = require('readline').createInterface({
readline.question("Enter the IP address users would use to connect to the server: ", (serverConnectAddress) => {
readline.close();
- if (!fs.existsSync("./verify.json")) {
+ if (!fs.existsSync("./data/verify.json")) {
log("Generating key exchange keys...");
- fs.writeFileSync("./verify.json", JSON.stringify(crypt.generateKeys()));
+ fs.writeFileSync("./data/verify.json", JSON.stringify(crypt.generateKeys()));
log("Generated key exchange keys");
}
- if (!fs.existsSync("./motd.md")) {
+ if (!fs.existsSync("./config/motd.md")) {
log("Generating MOTD template...");
- fs.writeFileSync("./motd.md", "Welcome to the Localchat server");
+ fs.writeFileSync("./config/motd.md", "Welcome to the Localchat server. This file was created using Localchat Server v" + version + ", edit " + process.cwd() + "/config/motd.md to change this.");
log("Generated MOTD template");
}
log("Loading encryption keys...");
- const verify = JSON.parse(fs.readFileSync("./verify.json").toString());
+ const verify = JSON.parse(fs.readFileSync("./data/verify.json").toString());
const keys = crypt.generateKeys();
const wss = new WebSocketServer({ port });
- const motd = fs.readFileSync("./motd.md").toString();
+ const motd = fs.readFileSync("./config/motd.md").toString();
log("Loaded encryption keys");
log("Generating local server configuration...");
- fs.writeFileSync("./local.lctsc", zlib.brotliCompressSync(JSON.stringify({
+ fs.writeFileSync("./exports/local.lctsc", zlib.brotliCompressSync(JSON.stringify({
address: "ws://127.0.0.1:" + port,
key: verify.publicKey
})));
log("Generating remote server configuration...");
- fs.writeFileSync("./remote.lctsc", zlib.brotliCompressSync(JSON.stringify({
+ fs.writeFileSync("./exports/remote.lctsc", zlib.brotliCompressSync(JSON.stringify({
address: "ws://" + serverConnectAddress + ":" + port,
key: verify.publicKey
})));
log("Generated server configuration files to local.lctsc and remote.lctsc");
log("Loading ban list...");
- if (!fs.existsSync("./bans.json")) fs.writeFileSync("./bans.json", "[]");
- let bans = JSON.parse(fs.readFileSync("./bans.json").toString());
+ if (!fs.existsSync("./config/bans.json")) fs.writeFileSync("./config/bans.json", "[]");
+ let bans = JSON.parse(fs.readFileSync("./config/bans.json").toString());
let clients = [];
log("Loaded ban list");
log("Loading operators list...");
- if (!fs.existsSync("./ops.json")) fs.writeFileSync("./ops.json", "[]");
- let operators = JSON.parse(fs.readFileSync("./ops.json").toString());
+ if (!fs.existsSync("./config/ops.json")) fs.writeFileSync("./config/ops.json", "[]");
+ let operators = JSON.parse(fs.readFileSync("./config/ops.json").toString());
log("Loaded operators list");
function parseAddress(address) {
@@ -132,7 +144,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.lang = "en";
ws.id = uuid();
ws.address = parseAddress(req.socket.remoteAddress);
ws.lastPacket = 0;
@@ -198,7 +210,7 @@ readline.question("Enter the IP address users would use to connect to the server
} catch (e) {}
}
- fs.writeFileSync("./bans.json", JSON.stringify(bans));
+ fs.writeFileSync("./config/bans.json", JSON.stringify(bans));
}
return;
@@ -446,7 +458,7 @@ readline.question("Enter the IP address users would use to connect to the server
}
}
- fs.writeFileSync("./bans.json", JSON.stringify(bans));
+ fs.writeFileSync("./config/bans.json", JSON.stringify(bans));
log("Banned user " + message.user, ws.address);
}
} else if (message.type === "unban") {
@@ -504,7 +516,7 @@ readline.question("Enter the IP address users would use to connect to the server
}
log("Unbanned user " + message.user, ws.address);
- fs.writeFileSync("./bans.json", JSON.stringify(bans));
+ fs.writeFileSync("./config/bans.json", JSON.stringify(bans));
}
} else if (message.type === "op") {
if (ws.address !== "::1" && !ws.address.startsWith("127.")) {
@@ -576,7 +588,7 @@ readline.question("Enter the IP address users would use to connect to the server
}
}
- fs.writeFileSync("./operators.json", JSON.stringify(operators));
+ fs.writeFileSync("./config/ops.json", JSON.stringify(operators));
log("Op-ed user " + message.user, ws.address);
}
} else if (message.type === "deop") {
@@ -650,7 +662,7 @@ readline.question("Enter the IP address users would use to connect to the server
}
log("Deop-ed user " + message.user, ws.address);
- fs.writeFileSync("./operators.json", JSON.stringify(operators));
+ fs.writeFileSync("./config/ops.json", JSON.stringify(operators));
}
}
} else {