diff --git a/routes/friends.js b/routes/friends.js index fec3270..8f91652 100644 --- a/routes/friends.js +++ b/routes/friends.js @@ -20,6 +20,48 @@ app.get("/friends/api/public/list/fortnite/*/recentPlayers", (req, res) => { res.json([]); }); +app.all("/friends/api/v1/*/friends/:friendId/alias", verifyToken, getRawBody, async (req, res) => { + let friends = await Friends.findOne({ accountId: req.user.accountId }); + + let validationFail = () => error.createError( + "errors.com.epicgames.validation.validation_failed", + "Validation Failed. Invalid fields were [alias]", + ["[alias]"], 1040, undefined, 404, res + ); + + const allowedCharacters = (" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~").split(""); + + for (let character of req.rawBody) { + if (!allowedCharacters.includes(character)) return validationFail(); + } + + if (!friends.list.accepted.find(i => i.accountId == req.params.friendId)) return error.createError( + "errors.com.epicgames.friends.friendship_not_found", + `Friendship between ${req.user.accountId} and ${req.params.friendId} does not exist`, + [req.user.accountId,req.params.friendId], 14004, undefined, 404, res + ); + + const friendIndex = friends.list.accepted.findIndex(i => i.accountId == req.params.friendId); + + switch (req.method) { + case "PUT": + if ((req.rawBody < 3) || (req.rawBody > 16)) return validationFail(); + + friends.list.accepted[friendIndex].alias = req.rawBody; + + await friends.updateOne({ $set: { list: friends.list } }); + break; + + case "DELETE": + friends.list.accepted[friendIndex].alias = ""; + + await friends.updateOne({ $set: { list: friends.list } }); + break; + } + + res.status(204).end(); +}); + app.get("/friends/api/public/friends/:accountId", verifyToken, async (req, res) => { let response = []; @@ -170,48 +212,6 @@ app.get("/friends/api/public/blocklist/*", verifyToken, async (req, res) => { }); }); -app.all("/friends/api/v1/*/friends/:friendId/alias", verifyToken, getRawBody, async (req, res) => { - let friends = await Friends.findOne({ accountId: req.user.accountId }).lean(); - - let validationFail = () => error.createError( - "errors.com.epicgames.validation.validation_failed", - "Validation Failed. Invalid fields were [alias]", - ["[alias]"], 1040, undefined, 404, res - ); - - const allowedCharacters = (" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~").split(""); - - for (let character of req.rawBody) { - if (!allowedCharacters.includes(character)) return validationFail(); - } - - if (!friends.list.accepted.find(i => i.accountId == req.params.friendId)) return error.createError( - "errors.com.epicgames.friends.friendship_not_found", - `Friendship between ${req.user.accountId} and ${req.params.friendId} does not exist`, - [req.user.accountId,req.params.friendId], 14004, undefined, 404, res - ); - - const friendIndex = friends.list.accepted.findIndex(i => i.accountId == req.params.friendId); - - switch (req.method) { - case "PUT": - if ((req.rawBody < 3) || (req.rawBody > 16)) return validationFail(); - - friends.list.accepted[friendIndex].alias = req.rawBody; - - await friends.updateOne({ $set: { list: friends.list } }); - break; - - case "DELETE": - friends.list.accepted[friendIndex].alias = ""; - - await friends.updateOne({ $set: { list: friends.list } }); - break; - } - - res.status(204).end(); -}); - function getRawBody(req, res, next) { if (req.headers["content-length"]) { if (Number(req.headers["content-length"]) > 16) return res.status(403).json({ "error": "File size must be 16 bytes or less." }); diff --git a/structs/functions.js b/structs/functions.js index f32aaa4..f12a8b8 100644 --- a/structs/functions.js +++ b/structs/functions.js @@ -222,7 +222,7 @@ function sendXmppMessageToAll(body) { global.Clients.forEach(ClientData => { ClientData.client.send(XMLBuilder.create("message") - .attribute("from", "xmpp-admin@prod.ol.epicgames.com") + .attribute("from", `xmpp-admin@${global.xmppDomain}`) .attribute("xmlns", "jabber:client") .attribute("to", ClientData.jid) .element("body", `${body}`).up().toString()); @@ -237,7 +237,7 @@ function sendXmppMessageToId(body, toAccountId) { if (!receiver) return; receiver.client.send(XMLBuilder.create("message") - .attribute("from", "xmpp-admin@prod.ol.epicgames.com") + .attribute("from", `xmpp-admin@${global.xmppDomain}`) .attribute("to", receiver.jid) .attribute("xmlns", "jabber:client") .element("body", `${body}`).up().toString()); diff --git a/xmpp/xmpp.js b/xmpp/xmpp.js index ab23e48..45a00a5 100644 --- a/xmpp/xmpp.js +++ b/xmpp/xmpp.js @@ -14,7 +14,7 @@ const port = 80; const wss = new WebSocket({ server: app.listen(port) }); const matchmaker = require("../matchmaker/matchmaker.js"); -let domain = "prod.ol.epicgames.com"; +global.xmppDomain = "prod.ol.epicgames.com"; global.Clients = []; @@ -78,7 +78,7 @@ wss.on('connection', async (ws) => { ws.send(XMLBuilder.create("open") .attribute("xmlns", "urn:ietf:params:xml:ns:xmpp-framing") - .attribute("from", domain) + .attribute("from", global.xmppDomain) .attribute("id", ID) .attribute("version", "1.0") .attribute("xml:lang", "en").toString()); @@ -147,7 +147,7 @@ wss.on('connection', async (ws) => { if (!findResource.content) return; resource = findResource.content; - jid = `${accountId}@${domain}/${resource}`; + jid = `${accountId}@${global.xmppDomain}/${resource}`; ws.send(XMLBuilder.create("iq") .attribute("to", jid) @@ -164,7 +164,7 @@ wss.on('connection', async (ws) => { ws.send(XMLBuilder.create("iq") .attribute("to", jid) - .attribute("from", domain) + .attribute("from", global.xmppDomain) .attribute("id", "_xmpp_session1") .attribute("xmlns", "jabber:client") .attribute("type", "result").toString()); @@ -177,7 +177,7 @@ wss.on('connection', async (ws) => { ws.send(XMLBuilder.create("iq") .attribute("to", jid) - .attribute("from", domain) + .attribute("from", global.xmppDomain) .attribute("id", msg.root.attributes.id) .attribute("xmlns", "jabber:client") .attribute("type", "result").toString()); @@ -255,7 +255,7 @@ wss.on('connection', async (ws) => { case "unavailable": if (!msg.root.attributes.to) return; - if (msg.root.attributes.to.endsWith(`@muc.${domain}`) || msg.root.attributes.to.split("/")[0].endsWith(`@muc.${domain}`)) { + if (msg.root.attributes.to.endsWith(`@muc.${global.xmppDomain}`) || msg.root.attributes.to.split("/")[0].endsWith(`@muc.${global.xmppDomain}`)) { if (!msg.root.attributes.to.toLowerCase().startsWith("party-")) return; let roomName = msg.root.attributes.to.split("@")[0]; @@ -275,7 +275,7 @@ wss.on('connection', async (ws) => { .attribute("type", "unavailable") .element("x").attribute("xmlns", "http://jabber.org/protocol/muc#user") .element("item") - .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${domain}/`, "")) + .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${global.xmppDomain}/`, "")) .attribute("jid", jid) .attribute("role", "none").up() .element("status").attribute("code", "110").up() @@ -305,7 +305,7 @@ wss.on('connection', async (ws) => { .attribute("xmlns", "jabber:client") .element("x").attribute("xmlns", "http://jabber.org/protocol/muc#user") .element("item") - .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${domain}/`, "")) + .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${global.xmppDomain}/`, "")) .attribute("jid", jid) .attribute("role", "participant") .attribute("affiliation", "none").up() @@ -325,7 +325,7 @@ wss.on('connection', async (ws) => { .element("x") .attribute("xmlns", "http://jabber.org/protocol/muc#user") .element("item") - .attribute("nick", getMUCmember(roomName, ClientData.displayName, ClientData.accountId, ClientData.resource).replace(`${roomName}@muc.${domain}/`, "")) + .attribute("nick", getMUCmember(roomName, ClientData.displayName, ClientData.accountId, ClientData.resource).replace(`${roomName}@muc.${global.xmppDomain}/`, "")) .attribute("jid", ClientData.jid) .attribute("role", "participant") .attribute("affiliation", "none").up().up().toString()); @@ -339,7 +339,7 @@ wss.on('connection', async (ws) => { .element("x") .attribute("xmlns", "http://jabber.org/protocol/muc#user") .element("item") - .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${domain}/`, "")) + .attribute("nick", getMUCmember(roomName, displayName, accountId, resource).replace(`${roomName}@muc.${global.xmppDomain}/`, "")) .attribute("jid", jid) .attribute("role", "participant") .attribute("affiliation", "none").up().up().toString()); @@ -516,7 +516,7 @@ function sendXmppMessageToClient(senderJid, msg, body) { } function getMUCmember(roomName, displayName, accountId, resource) { - return `${roomName}@muc.${domain}/${encodeURI(displayName)}:${accountId}:${resource}`; + return `${roomName}@muc.${global.xmppDomain}/${encodeURI(displayName)}:${accountId}:${resource}`; } function isObject(value) {