Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var app = require("express")();
- var http = require("http").Server(app);
- var request = require("request");
- var io = require("socket.io")(http);
- var mySQL = require("mysql");
- var sql = mySQL.createConnection({
- host: "localhost",
- user: "us",
- password: "pass",
- database: "db",
- dateStrings: true
- });
- io.origins("http://orchidea.margonem.pl:*");
- http.listen(8080, () => { console.log("Started listening on port 8080"); });
- // VARIABLES AND FUNCTIONS
- var is = {
- set: a => typeof a !== "undefined" && a != null,
- num: a => typeof a === "number",
- str: a => typeof a === "string",
- bool: a => typeof a === "boolean",
- fun: a => typeof a === "function",
- arr: a => a instanceof Array,
- obj: a => typeof a === "object" && typeof a !== "function" && !(a instanceof Array),
- in: (a, min, max) => min <= a && max >= a,
- inArr: function(a, arr, bool) {
- for (let i = 0; i < arr.length; i++) if (arr[i] === a) return is.set(bool) ? !0 : i;
- return is.set(bool) ? !1 : -1;
- },
- inArrObj: function(a, arr, prop, bool) {
- for (let i = 0; i < arr.length; i++)
- if (!arr[i].hasOwnProperty(prop)) return is.set(bool) ? !1 : -1;
- else if (arr[i][prop] === a) return is.set(bool) ? !0 : i;
- return is.set(bool) ? !1 : -1;
- },
- correct: function(a, step, user) {
- for (let i in a) switch(i) {
- case "type":
- if (!is.num(a[i])) {
- _.log(step + ": " + i + " is not a number", user);
- return false;
- } else if (!is.in(a[i], 0, 5)) {
- _.log(step + ": " + i + " is not in a range", user);
- return false;
- }
- break;
- case "id":
- if (!is.num(a[i])) {
- _.log(step + ": " + i + " is not a number", user);
- return false;
- } else if (!is.in(a[i], 0, 999999999)) {
- _.log(step + ": " + i + " is not in a range", user);
- return false;
- } else if (is.inArr(a[i], _.arrs.banned, true)) {
- _.log(step + ": " + i + " is banned", user);
- return false;
- }
- break;
- case "nick":
- if (!is.str(a[i])) {
- _.log(step + ": " + i + " is not a string", user);
- return false;
- }
- break;
- case "c":
- if (a[i] != null && !is.str(a[i])) {
- _.log(step + ": " + i + " is neither a string nor a null", user);
- return false;
- }
- break;
- case "addon":
- if (!is.bool(a[i])) {
- _.log(step + ": " + i + " is not a boolean", user);
- return false;
- } else if (!a[i]) {
- _.log(step + ": " + i + " is false so user blocked addon", user);
- return false;
- }
- break;
- case "guest":
- if (!is.bool(a[i])) {
- _.log(step + ": " + i + " is not a boolean", user);
- return false;
- }
- break;
- case "settings":
- if (!is.obj(a[i])) {
- _.log(step + ": " + i + " is not an object", user);
- return false;
- }
- break;
- case "getAll":
- if (!is.bool(a[i])) {
- _.log(step + ": " + i + " is not an object", user);
- return false;
- }
- break;
- case "message":
- if (!is.str(a[i])) {
- _.log(step + ": " + i + " is not a string", user);
- return false;
- }
- break;
- case "time":
- if (!is.str(a[i])) {
- _.log(step + ": " + i + " is not a strnig", user);
- return false;
- }
- break;
- default:
- _.log(step + ": found unknown property " + i, user);
- return false;
- }
- return true;
- },
- online: function(a, inLogged) {
- if (is.set(inLogged)) for (let i = 0; i < _.arrs.logged.length; i++) { if (_.arrs.logged[i].sid === a.sid && _.arrs.logged[i].mid === a.mid && _.arrs.logged[i].ip === a.ip) return true; }
- else for (let i = 0; i < _.arrs.onlineA.length; i++) if (_.arrs.onlineA[i].charNick === a) return true;
- return false;
- }
- };
- var get = {
- info: {
- decoded: s => {
- if (s.search(/[^;0-9]/g) > -1) return null;
- s = s.split(";");
- if (!is.in(s.length, 8, 20)) return null;
- else {
- var c = +s[0], t = "", d = a => a.split("").reverse().map(a => +a - 1 > -1 ? +a - 1 : 9).join("");
- for (let i = 1; i < s.length; i++)
- if (i < 3) s[i] = +d(s[i]) - c;
- else if (i == 3) continue;
- else t += s[i] = String.fromCharCode(+d(s[i]) - c);
- s = s.slice(0, 4); s.push(t);
- return s;
- }
- },
- encoded: (id, time, random, nick) => {
- var r = +get.str(3, true), s = "", c = a => a.toString().split("").map(a => (+a + 1) % 10).reverse().join("");
- if (r === 0) r = 23;
- id += r; time += r;
- nick = nick.split("").map(a => c(a.charCodeAt() + r));
- s = r + ";" + c(id) + ";" + c(time) + ";" + random + ";" + nick.join(";");
- return s;
- },
- cookie: id => {
- if (id < 1) return id == 0 ? 1 : -1;
- var t = 31 - Math.floor(Math.log(id) * Math.LOG2E);
- return id + "_o" + (~(id << t) >>> t);
- }
- },
- time: () => Date.now(),
- ip: socket => socket.handshake.address.replace(/^.{7}/g, ""),
- str: (len, onlyNumbers) => {
- var chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", s = "";
- for (let i = 0; i < len; i++) s += chars[Math.floor(Math.random() * (is.set(onlyNumbers) ? 9 : chars.length))];
- return s;
- },
- logged: a => {
- for (let i = 0; i < _.arrs.logged.length; i++) if (_.arrs.logged[i].sid === a.sid && _.arrs.logged[i].mid === a.mid) return _.arrs.logged[i].nick;
- return null;
- },
- online: () => {
- request({
- url: "https://www.margonem.pl",
- method: "POST",
- qs: {task: "stats"}
- }, (a, b, c) => {
- if (a != null) _.log("GET ONLINE: could not reach stats");
- else {
- let t = /<h2>Świat Orchidea(.|\n)*?<\/p>/g.exec(c);
- if (!is.set(t)) _.log("GET ONLINE: orchidea world not found in stats");
- else {
- t = t[0].match(/<p.+>/g)[0].replace(/<.*?>/g, "");
- let o = t.split(",").map(x => x.replace(/^ +| +$/g, ""));
- _.arrs.onlineNA = o.filter(x => !is.online(x));
- io.emit("online", _.countOnline());
- }
- }
- });
- }
- };
- var _ = {
- arrs: {
- banned: [5256570, 4521505],
- themes: [
- {
- css: "themes/wing/tWing.css",
- js: "themes/wing/tWing.min.js"
- },
- {
- css: "themes/second/tSecond.css",
- js: "themes/second/tSecond.js"
- },
- {
- css: "themes/brown/tBrown.css",
- js: "themes/brown/tBrown.js"
- }
- ],
- // data format: {mid::number, randString::string}
- logging: [],
- // data format: {sid::string, mid::number, nick::string, charNick::string, ip::string, guest::boolean}
- logged: [],
- // data format: {sid::string, charNick::string}
- onlineA: [],
- // data format: charNick::string
- onlineNA: [],
- // data format: {mid::number, nick::string, colour::string}
- colours: []
- },
- escapeHTML: str => {
- let c = {
- "<": "<",
- ">": ">",
- "'": """,
- '"': "'",
- "`": "`",
- "=": "=",
- "&": "&",
- "/": "/"
- };
- return str.replace(/[<>'"`=&\/]/g, (b) => c[b]);
- },
- countOnline: retArr => {
- var t = [[], []];
- for (let i = 0; i < _.arrs.onlineA.length; i++) t[0].push(_.arrs.onlineA[i].charNick);
- for (let i = 0; i < _.arrs.onlineNA.length; i++) t[1].push(_.arrs.onlineNA[i]);
- return is.set(retArr) ? {type: 1, n : t[0].length + t[1].length, online: t} : {type: 0, n: t[0].length + t[1].length, online: null};
- },
- log: (a, user) => {
- var t = is.set(user);
- console.log(a + (t ? ". User " + user.nick + " with acc id " + user.id + " and IP " + get.ip(user.s) : ""));
- if (t) user.s.disconnect(true);
- },
- db: (q, f) => {
- sql.query(q, (e, r) => {
- if (e) console.log("DB: error [" + e + "] while quering with query " + q);
- else if (is.set(f)) f(r);
- });
- }
- };
- // START
- _.db("SELECT * FROM `colours`", a => {
- for (let i = 0; i < a.length; i++) _.arrs.colours.push({mid: a[i].mID, nick: a[i].nick, colour: a[i].colour});
- });
- get.online();
- setInterval(get.online, 120000);
- io.on("connection", s => {
- // DISCONNECT
- s.on("disconnect", () => {
- _.arrs.logging = _.arrs.logging.filter(a => a.sid !== s.id);
- _.arrs.logged = _.arrs.logged.filter(a => a.sid !== s.id);
- _.arrs.onlineA = _.arrs.onlineA.filter(a => a.sid !== s.id);
- s.broadcast.emit("online", _.countOnline());
- });
- // LOGIN
- // data format: {type::number, id::number, nick::string, guest::boolean, addon::boolean, c::nullORstring}
- // output format: {type::number, error::booleanORstring, c::nullORstring, rs::nullORstring, p::nullORstring}
- s.on("login", (m, f) => {
- var ip = get.ip(s), guest = null, user = {
- s: s,
- id: m.id,
- nick: m.nick
- };
- if (is.correct(m, "LOGIN", user)) {
- // if user is guest check id
- if (m.guest) request({
- url: "https://www.margonem.pl",
- method: "POST",
- qs: {task: "profile", id: m.id}
- }, (a, b, c) => {
- if (is.set(a)) _.log("LOGIN: could not reach profile while checking guest", user);
- else {
- let t = /<a href=\?task=profile&id=[0-9]+>/g.exec(c);
- if (!is.set(t)) _.log("LOGIN: could not check guest", user);
- else {
- guest = +t[0].match(/[0-9]+/g)[0];
- let r = get.str(20);
- _.arrs.logging.push({id: guest, randString: r});
- _.log("New logging attempt from " + m.nick + "'s guest with acc id " + guest + " and IP " + ip);
- f({
- type: 2,
- error: !1,
- id: guest,
- c: get.info.cookie(guest),
- rs: null,
- p: null
- });
- }
- }
- });
- else switch(m.type) {
- // cookie exists or first login step (generate string)
- case 0:
- let _f = e => {
- _.db("DELETE FROM `logged` WHERE `mID` = " + mySQL.escape(m.id));
- var n = is.inArrObj(m.id, _.arrs.logging, "id"), r = n > -1 ? _.arrs.logging[n].randString : get.str(20);
- if (n == -1) {
- _.arrs.logging.push({id: m.id, randString: r});
- _.log("New logging attempt from " + m.nick + " with acc id " + m.id + " and IP " + ip);
- }
- f({
- type: 0,
- error: is.set(e) ? e : !1,
- id: m.id,
- c: get.info.cookie(m.id),
- rs: r,
- p: null
- });
- };
- // cookie is set
- if (is.set(m.c)) {
- let t = get.info.decoded(m.c);
- // check cookie data
- if (!is.set(t)) {
- _.log("LOGIN 0: user " + m.nick + " with id " + m.id + " has incorrect data");
- _f("Twoje dane są niepoprawne – musisz zalogować się ponownie.");
- } else if (!is.in(get.time() - t[2], 0, 259200000)) {
- _.log("LOGIN 0: user " + m.nick + " with id " + m.id + " has too old data");
- _f("Twoje dane były przestarzałe – musisz zalogować się ponownie.");
- } else _.db(
- "SELECT `mID`, `IP`, `nick`, `last_date`, `randString` FROM `logged` WHERE `mID` = " + mySQL.escape(t[1]),
- a => {
- if (a.length < 1) {
- _.log("LOGIN 0: user " + m.nick + " with id " + m.id + " was not found in database");
- _f("Twoje dane nie zostały odnalezione w bazie danych – musisz zalogować się ponownie.");
- } else {
- a = a[0];
- if (t[1] !== a.mID || t[3] !== a.randString || t[4] !== a.nick || ip !== a.IP) {
- _.log("LOGIN 0: user " + m.nick + " with id " + m.id + " has cookie data which differs from database");
- _f("Twoje dane są niepoprawne – musisz zalogować się ponownie.");
- } else {
- let _t = get.time(), r = get.info.encoded(t[1], _t, t[3], t[4]);
- _.arrs.logging = _.arrs.logging.filter(a => a.id !== m.id);
- _.arrs.logged.push({sid: s.id, mid: t[1], nick: t[4], charNick: m.nick, ip: ip});
- _.arrs.onlineA.push({sid: s.id, charNick: m.nick});
- _.arrs.onlineNA = _.arrs.onlineNA.filter(a => a !== m.nick);
- _.log("New connection from IP " + ip + " with profile nick " + t[4] + ", game nick " + m.nick + " and ID " + t[1] + ".");
- s.broadcast.emit("online", _.countOnline());
- _.db("UPDATE `logged` SET `last_date` = " + _t + " WHERE `mID` = " + mySQL.escape(t[1]));
- f({
- type: 1,
- error: !0,
- id: t[1],
- c: get.info.cookie(t[1]),
- rs: r,
- p: "http://dev.d4nte.pl/orchidea/"
- });
- }
- }
- }
- );
- } else _f();
- break;
- // second login step (check profile for string)
- case 1:
- request({
- url: "https://www.margonem.pl",
- method: "POST",
- qs: {task: "profile", id: m.id}
- }, (a, b, c) => {
- if (is.set(a)) _.log("LOGIN 1: could not load user profile", user);
- else {
- let n = is.inArrObj(m.id, _.arrs.logging, "id");
- if (n == -1) {
- _.log("LOGIN 1: user " + m.id + " not found in logging array");
- f({
- type: 0,
- error: "Informacja o twoich danych nie została odnaleziona. Odśwież stronę i spróbuj ponownie.",
- id: m.id,
- c: get.info.cookie(m.id),
- rs: null,
- p: null
- });
- } else {
- let t = /id="nick" tip="([a-z]| |[ąęóćśźżńł]|-)+"/ig.exec(c), str = new RegExp(_.arrs.logging[n].randString, "g").exec(c);
- if (!is.set(t)) {
- _.log("LOGIN 1: could not check user " + m.id + " profile nick");
- f({
- type: 0,
- error: "Wystąpił błąd przy pobieraniu twoich danych z profilu. Spróbuj ponownie później, bądź zgłoś ten błąd autorowi dodatku.",
- id: m.id,
- c: get.info.cookie(m.id),
- rs: null,
- p: null
- });
- } else if (!is.set(str)) f({
- type: 0,
- error: "Na twoim profilu nie znajduje się podany ciąg znaków, bądź jest on niepoprawny.",
- id: m.id,
- c: get.info.cookie(m.id),
- rs: null,
- p: null
- });
- else {
- let nick = t[0].replace(/id="nick" tip=|"/g, ""), _t = get.time(), r = get.str(15, !0), c = get.info.encoded(m.id, _t, r, nick);
- _.arrs.logging = _.arrs.logging.filter(a => a.id !== m.id);
- _.arrs.logged.push({sid: s.id, mid: m.id, nick: nick, charNick: m.nick, ip: ip});
- _.arrs.onlineA.push({sid: s.id, charNick: m.nick});
- _.arrs.onlineNA = _.arrs.onlineNA.filter(a => a !== m.nick);
- _.log("New connection from IP " + ip + " with profile nick " + nick + ", game nick " + m.nick + " and ID " + m.id + ".");
- s.broadcast.emit("online", JSON.stringify(_.countOnline(!0)));
- _.db(
- "INSERT INTO `logged` (`mID`, `IP`, `nick`, `last_date`, `randString`) VALUES (" +
- mySQL.escape(m.id) + ", '" + ip + "', '" + nick + "', " + _t + ", '" + r + "')"
- );
- f({
- type: 1,
- error: !1,
- id: m.id,
- c: get.info.cookie(m.id),
- rs: c,
- p: "http://dev.d4nte.pl/orchidea/"
- });
- }
- }
- }
- });
- break;
- default:
- _.log("LOGIN: type avoided cases. User " + m.nick + " with acc id " + m.id + " and IP " + get.ip(s));
- }
- }
- });
- // SETTINGS
- // data format: {id::number, nick::string, settings::object}
- // output format: {error::booleanORstinrg, theme::nullORobject, adds::object, h::string, l::string}
- s.on("settings", (m, f) => {
- var user = {
- s: s,
- id: m.id,
- nick: m.nick
- };
- // check if is online
- if (!is.online({sid: s.id, mid: m.id, ip: get.ip(s)}, !0)) _.log("SETTINGS: #WARNING# could not found in logged array", user);
- // proccess data
- else if (is.correct(m, "SETTINGS", user)) {
- let t = {
- error: false,
- theme: null,
- adds: "additionals/additionals.min.js",
- l: "<link rel='stylesheet' type='text/css' href='&&&'>",
- h: "head"
- };
- switch (m.settings.theme) {
- case "tWing":
- t.theme = _.arrs.themes[0];
- break;
- case "tSecond":
- t.theme = _.arrs.themes[1];
- break;
- case "tBrown":
- t.theme = _.arrs.themes[2];
- break;
- default:
- _.log("SETTINGS: theme not found");
- t.error = true;
- }
- f(t);
- }
- });
- // ONLINE
- // data format: {id::number, nick::string, world::string, getAll::boolean}
- // output format: {type::number, n::number, online::nullORobject}
- s.on("online", (m, f) => {
- var user = {
- s: s,
- id: m.id,
- nick: m.nick
- };
- // check if is online
- if (!is.online({sid: s.id, mid: m.id, ip: get.ip(s)}, !0)) _.log("ONLINE: #WARNING# could not found in logged array", user);
- // proccess data
- else if (is.correct(m, "ONLINE", user)) {
- if (m.getAll) f(_.countOnline(!0));
- else f(_.countOnline());
- }
- });
- // CHAT
- // data format: {type::number, id::number, nick::string, world::string, message::string, time::string}
- // output format: {type::number, error::booleanORstring, mes::nullORarrayORobject}
- s.on("chat", (m, f) => {
- var user = {
- s: s,
- id: m.id,
- nick: m.nick
- };
- // check if is online
- if (!is.online({sid: s.id, mid: m.id, ip: get.ip(s)}, !0)) _.log("CHAT: #WARNING# could not found in logged array", user);
- // proccess data
- else if (is.correct(m, "CHAT", user)) {
- switch (m.type) {
- case 0:
- _.db("SELECT * FROM (SELECT * FROM `chat` WHERE UNIX_TIMESTAMP() * 1000 - `date` < 3600000 ORDER BY `id` DESC LIMIT 30) sub ORDER BY `id` ASC", a => {
- let t = [];
- for (let i = 0; i < a.length; i++) {
- let j = is.inArrObj(a[i].mID, _.arrs.colours, "mid");
- t.push({
- type: a[i].type,
- style: j > -1 ? "color: " + _.arrs.colours[j].colour + ";" : undefined,
- time: a[i].time,
- nick: a[i].nick,
- message: a[i].message
- });
- }
- f({type: 0, error: !1, mes: t});
- });
- break;
- case 1:
- let nick = get.logged({sid: s.id, mid: m.id});
- m.message = m.message.trim();
- if (!is.set(nick)) _.log("CHAT: nick is not set", user);
- else if (m.message.length > 250) f({error: "Błąd: wiadomość ma zbyt dużo znaków"});
- else if (m.message.length < 1) f({error: "Błąd: wiadomość jest pusta"});
- else if (m.message.search(/[^ -~ąćęłńóśżź]/gi) > -1) f({error: "Błąd: wiadomość zawiera niedozwolone znaki"});
- else {
- m.message = _.escapeHTML(m.message);
- m.time = _.escapeHTML(m.time);
- _.db(
- "INSERT INTO `chat` (`mID`, `date`, `time`, `type`, `nick`, `message`) VALUES (" +
- mySQL.escape(m.id) + ", " + get.time() + ", " + mySQL.escape(m.time) + ", 'textMes', '" + nick + "', " + mySQL.escape(m.message) + ")"
- );
- let t = is.inArrObj(m.id, _.arrs.colours, "mid");
- io.emit("chat", {
- type: 1,
- error: !1,
- mes: {
- type: "textMes",
- style: t > -1 ? "color: " + _.arrs.colours[t].colour + ";" : undefined,
- time: m.time,
- nick: nick,
- message: m.message
- }
- });
- }
- break;
- default:
- _.log("LOGIN: type avoided cases. User " + m.nick + " with acc id " + m.id + " and IP " + get.ip(s));
- }
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement