From 80d45efdf7fe6204de5378b2e5536d241d9996df Mon Sep 17 00:00:00 2001 From: WlodekM Date: Fri, 9 Aug 2024 20:46:04 +0300 Subject: v1.3.0 (also use prettier (bad idea (why doesnt it have an undo command) ) ) --- .prettierrc | 8 +++ README.md | 16 ++--- accounts.js | 71 ++++++++++++++---- client.html | 97 +++++++++++++++++++------ commands.js | 207 +++++++++++++++++++++++++++++++++-------------------- commands/motd.js | 8 +-- config.json | 9 ++- index.js | 210 ++++++++++++++++++++++++++++++++++++------------------ jsconfig.json | 44 ++++++------ lib.js | 2 +- package-lock.json | 120 +++++++++++++++++++++++++++++++ package.json | 45 ++++++------ 12 files changed, 594 insertions(+), 243 deletions(-) create mode 100644 .prettierrc create mode 100644 package-lock.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1491ea5 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "semi": true, + "singleQuote": false, + "printWidth": 9999, + "endOfLine": "lf" +} diff --git a/README.md b/README.md index e08f31f..87e2ecc 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,14 @@ node index.js ## TODO -- ~~Make commands be in an object instead of a switch statement~~ -- ~~/help command~~ -- ~~/about command~~ -- ~~Config (JSON)~~ -- ~~MOTD~~ -- ~~Custom commands (plugins)~~ -- ~~Auth (optional)~~ +- ~~Make commands be in an object instead of a switch statement~~ +- ~~/help command~~ +- ~~/about command~~ +- ~~Config (JSON)~~ +- ~~MOTD~~ +- ~~Custom commands (plugins)~~ +- ~~Auth (optional)~~ ## IN PROGRESS -- JSON support (for clients and stuff) +- JSON support (for clients and stuff) diff --git a/accounts.js b/accounts.js index f45ab6e..27de609 100644 --- a/accounts.js +++ b/accounts.js @@ -1,31 +1,74 @@ -import { createHash } from "crypto" -import cuid from 'cuid'; -import fs from "fs" +import { createHash } from "crypto"; +import cuid from "cuid"; +import fs from "fs"; -if(!fs.existsSync("db")) fs.mkdirSync("db"); -if(!fs.existsSync("db/users.json")) fs.writeFileSync("db/users.json", "{}"); +if (!fs.existsSync("db")) fs.mkdirSync("db"); +if (!fs.existsSync("db/users.json")) fs.writeFileSync("db/users.json", "{}"); -const db = JSON.parse(fs.readFileSync("db/users.json").toString()) +const db = JSON.parse(fs.readFileSync("db/users.json").toString()); +/** + * Syncs DB to file + */ function syncDB() { - fs.writeFileSync("db/users.json", JSON.stringify(db)) + fs.writeFileSync("db/users.json", JSON.stringify(db)); } +/** + * Checks if account exists + * @param {String} username Username to check + * @returns {Boolean} + */ export function checkAccount(username) { - return db[username] != undefined + return db[username] != undefined; } -export function createAccount(username, password) { - let hashedPassword = createHash('sha256').update(password).digest('hex'); +/** + * Create an account + * @param {String} username The username + * @param {String} password The password + */ +export function createAccount(username, password, admin = false) { + let hashedPassword = createHash("sha256").update(password).digest("hex"); db[username] = { + admin, id: cuid(), password: hashedPassword, - t: Number(new Date()) / 1000 + ips: [], + t: Number(new Date()) / 1000, }; syncDB(); } +/** + * Log IP address (for IP bans) + * @param {String} username Username + * @param {String} ip IP address + */ +export function logIP(username, ip) { + if (!db[username].ips) db[username].ips = []; + if (!db[username].ips.includes(ip)) db[username].ips.push(ip); + syncDB(); +} + +/** + * Check if password is correct + * @param {String} username The username + * @param {String} password The password + * @returns {Boolean} + */ export function checkPassword(username, password) { - let hashedPassword = createHash('sha256').update(password).digest('hex'); - return db[username]?.password === hashedPassword -} \ No newline at end of file + let hashedPassword = createHash("sha256").update(password).digest("hex"); + return db[username]?.password === hashedPassword; +} + +/** + * Get account data + * @param {String} username The username + * @returns {Object} + */ +export function getAccountData(username) { + let returnData = JSON.parse(JSON.stringify(db[username])); + returnData.password = null; + return returnData; +} diff --git a/client.html b/client.html index db7a4d3..ed5f9d4 100644 --- a/client.html +++ b/client.html @@ -1,34 +1,63 @@
-
wsChat
-
+ -
-
- - -
+
+
wsChat
+
+
+
+ + +
+