const Express = require("express"); const BodyParser = require("body-parser"); const mongodb = require("mongodb"); const ObjectId = require("mongodb").ObjectID; const CONNECTION_URL = "mongodb+srv://edge-mongodb"; const DATABASE_NAME = "edgecomm"; const COLLECTION_NAME = "nettranslate"; var cors = require('cors'); const MongoClient = require("mongodb").MongoClient; const requestIp = require('request-ip'); var app = Express(); app.use(cors()); app.use(BodyParser.json()); app.use(BodyParser.urlencoded({ extended: true })); app.use(requestIp.mw()); app.enable('trust proxy'); var corsOptions = { origin: 'http://matchmaking.obsidiancorestudios.com', optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 } var database, collection; var requestEnable = false; /**GET*/ app.get("/", (request, response) => { response.json({ Error: "Please refer to documentation.", Status: "Failed", Info: "Matchmaking Server" }) }); /**GET:USRPASS:SESSION*/ app.get("/lobby/listall/", async (request, response) => { /** Field LobbyCount notifies the client of the number of indexed lobbies in the response Field LBN[num] breaks down into Lobby Name [Number], this informs the client what the name is of the assosiated lobby Field LBIP[num] breaks down into Lobby IP [Number], this informs the client what the IP is of the assosiated lobby */ if (requestEnable) { var o_id = new mongodb.ObjectID(); var ar; auth.insertOne({ _id : o_id, type: 1, status: false }) await auth.findOne({_id: o_id}, (err, res) => { const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) const dec = async i => (await delay(200), --i) ar = res; const verifyUser = async () => { let responseEnabled = true; let enableLookup = true; let i = 8; let timeout = 7; while(i = await dec(i)) try { if (enableLookup) { timeout = timeout - 1; if (timeout > 0) { await auth.findOne({_id: o_id}, (err, res) => { if (res.status) { if (responseEnabled) { timeout = 0; i = 1; if (res.type == 2) { responseEnabled = false; enableLookup = false; response.status(200).json({ Info: "Good", LBN : res.lobbies, LBIP : res.ip, LobbyCount : res.LobbyCount }); res = null; } else { responseEnabled = false; ar = null; res = null; response.status(403).json({ Info: "Forbidden", Message: "Something went wrong." }); } } } }); } else { if (responseEnabled) { response.status(403).json({ Info: "Forbidden", Message: "Something went wrong..." }); responseEnabled = false; ar = null; res = null; } } } } catch (err) { enableLookup = false; res = null; if (responseEnabled) { response.status(503).json({ Info: "Forbidden", Message: "Something went wrong..." }); } responseEnabled = false; console.log(err); timeout = 1; } } verifyUser(); }); } else { response.status(503).json({ Info: "Forbidden", Message: "The server is starting up, please retry again in a moment." }); } }); app.get("/lobby/submit/", async (request, response) => { /** Lobby submission documentation */ if (typeof request.query.name === 'undefined' || typeof request.query.ip === 'undefined' || typeof request.query.version === 'undefined' || request.query.name === '' || request.query.ip === '' || request.query.version === '') { response.status(403).json({ Info : "Forbidden", Message : "Missing requirements", POSTSTATE : false }); } else { if (requestEnable) { var o_id = new mongodb.ObjectID(); var ar; auth.insertOne({ _id : o_id, type : 3, LobbyName : request.query.name, LobbyIP : request.query.ip, LobbyVersion : request.query.version, status : false }) await auth.findOne({_id: o_id}, (err, res) => { const delay = ms => new Promise(resolve => setTimeout(resolve, ms)) const dec = async i => (await delay(200), --i) ar = res; const verifyUser = async () => { let responseEnabled = true; let enableLookup = true; let i = 8; let timeout = 7; while(i = await dec(i)) try { if (enableLookup) { timeout = timeout - 1; if (timeout > 0) { await auth.findOne({_id: o_id}, (err, res) => { if (res.status) { if (responseEnabled) { timeout = 0; i = 1; if (res.type == 4 && res.lobbyPosted == true) { responseEnabled = false; enableLookup = false; response.status(200).json({ Info: "Good", LBN : res.lobbyName, SECRET : res.lobbySecret, POSTSTATE : res.lobbyPosted }); res = null; } else { responseEnabled = false; if (res.type == 4 && res.lobbyPosted == false) response.status(403).json({ Info: "Forbidden", Message: res.info, POSTSTATE : res.lobbyPosted }); } res = null; ar = null; } } }); } else { if (responseEnabled) { response.status(403).json({ Info: "Forbidden", Message: "Something went wrong.", POSTSTATE : false }); responseEnabled = false; ar = null; res = null; } } } } catch (err) { enableLookup = false; res = null; if (responseEnabled) { response.status(503).json({ Info: "Forbidden", Message: "Something went wrong...", POSTSTATE : false }); } responseEnabled = false; console.log(err); timeout = 1; } } verifyUser(); }); } else { response.status(503).json({ Info: "Forbidden", Message: "The server is starting up, please retry again in a moment.", POSTSTATE : false }); } } }); app.get("/lobby/remove/", async (request, response) => { /** Lobby removal documentation */ response.status(503).json({ INFORMATION: "THE LOBBY REMOVAL ENDPOINT IS NOT ACTIVE, TRY AGAIN LATER.", VERSION: 1.0 }); }); app.listen(10000, () => { console.log("Application is now online, attempting to connect to Database. Requests will be set to 503 until connected!") MongoClient.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true} , (error, client) => { if(error) { throw error; } database = client.db(DATABASE_NAME); auth = database.collection(COLLECTION_NAME); requestEnable = true; console.log("Database is connected and requests are enabled..."); }); });