Skip to content

Commit

Permalink
1.4.1
Browse files Browse the repository at this point in the history
Header authorization and minor code improvements
  • Loading branch information
gerrustalker committed Mar 18, 2023
1 parent 67567c9 commit 5e714c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
10 changes: 5 additions & 5 deletions garrysmod/addons/lrelaysockets/lua/autorun/server/sv_sockets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ require("gwsockets")

util.AddNetworkString("LRELAYS:Message")

local socket = LRELAY.socket and LRELAY.socket or GWSockets.createWebSocket(LRELAY.socketPath)
if socket ~= LRELAY.socket then LRELAY.socket = socket socket:open() else print("[LRelay] Script updated, using previos connection") end
local socket = LRELAY.socket or GWSockets.createWebSocket(LRELAY.socketPath)
if socket ~= LRELAY.socket then LRELAY.socket = socket socket:setHeader("authorization", LRELAY.token) socket:open() else print("[LRelay] Script updated, using previos connection") end

LRELAY.SendMessage = function(content)
if socket:isConnected() then socket:write(util.TableToJSON({type = "message", message = content, token = LRELAY.token})) end
if socket:isConnected() then socket:write(util.TableToJSON({type = "message", message = content})) end
end

LRELAY.SendToConsole = function(content)
if socket:isConnected() then socket:write(util.TableToJSON({type = "console", message = utf8.force(content), token = LRELAY.token})) end
if socket:isConnected() then socket:write(util.TableToJSON({type = "console", message = utf8.force(content)})) end
end

LRELAY.SendStatus = function(uptime, players, map, ip, name)
if socket:isConnected() then socket:write(util.TableToJSON({type = "status", name = LRELAY.statusName, uptime = uptime or 0, players = players or {}, map = map or "gm_bigcity_improved", ip = ip or game.GetIPAddress() or "unknown ip", hostname = name or "no name", token = LRELAY.token})) end
if socket:isConnected() then socket:write(util.TableToJSON({type = "status", name = LRELAY.statusName, uptime = uptime or 0, players = players or {}, map = map or "gm_bigcity_improved", ip = ip or game.GetIPAddress() or "unknown ip", hostname = name or "no name"})) end
end

-- pasted from original LRelay written by me (Lenofag-2000)
Expand Down
30 changes: 18 additions & 12 deletions nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ var expressWS = require('express-ws')(app)
var wssrelay = expressWS.getWss("/relay")

var checkAuth = (t) => {
config.auth.forEach(token => {if(token == t) return true})
return false
var ret = false
config.auth.forEach(token => {if(token == t) ret = true})
return ret
}

var queue = []
var status = {}

app.ws("/relay", (ws) => {
var goodip = `${ws._socket.remoteAddress.slice(7)}:${ws._socket.remotePort}`
if(!req.headers.authorization || !checkAuth(req.headers.authorization)) {
console.log(`[express] rejected ${goodip} (not authorized)`)
ws.send(JSON.stringify({error: "auth", details: `invalid token`}))
return ws.close(4401)
}
console.log(`[express] connected with ${goodip}`)
ws.on("message", msg => {
var parsed;
try {
parsed = JSON.parse(msg);
} catch(err) {ws.send(JSON.stringify({error: "parse", details: `${err}`})); return console.log(`[express] JSON.parse errored: ${err}`)}

if(!parsed.token || checkAuth(parsed.token)) return ws.send(JSON.stringify({error: "auth", details: `invalid token`}));
switch(parsed.type) {
case "message":
if(!parsed.message) return ws.send(JSON.stringify({error: "message", details: `invalid message`}));
Expand Down Expand Up @@ -56,14 +61,15 @@ app.ws("/relay", (ws) => {
break
case "status":
if(!parsed.name || typeof parsed.name != "string" || parsed.name.trim() == "") return ws.send(JSON.stringify({error: "status", details: `invalid name`}));
var sdata = {}
sdata.uptime = parsed.uptime ?? 0
sdata.players = parsed.players ?? []
sdata.map = parsed.map ?? "unknown"
sdata.ip = parsed.ip ?? "unknown"
sdata.name = parsed.hostname ?? "unknown"
sdata.lastupdate = Date.now()
status[parsed.name] = sdata

status[parsed.name] = {
uptime: parsed.uptime ?? 0,
players: parsed.players ?? [],
map: parsed.map ?? "unknown",
ip: parsed.ip ?? "unknown",
name: parsed.hostname ?? "unknown",
lastupdate: Date.now()
}
break
}
})
Expand All @@ -78,7 +84,7 @@ setInterval(() => {
var oqueue = queue; var big = false;
var msg = "";
if(queue.length >= 16) {var a = [...pastedChunking(queue, 8)]; queue = a[0]; oqueue = oqueue.slice(8); big = true};
queue.forEach(a => msg += `${a.text.trim()}${a.count > 1 ? ` (x${a.count})` : ""}\n`); queue = big ? oqueue : []; oqueue = []; msg = msg.replaceAll("_", "\\_")/*.replaceAll("*", "\\*").replaceAll("`", "\\`").replaceAll("|", "\\|").replaceAll("~", "\\~")*/.replaceAll("@", "@ ") // pasted myself
queue.forEach(a => msg += `${a.text.trim()}${a.count > 1 ? ` (x${a.count})` : ""}\n`); queue = big ? oqueue : []; oqueue = []; msg = msg.replaceAll("@", "@\u200b")/*.replaceAll("_", "\u200b_").replaceAll("*", "\\*").replaceAll("`", "\\`").replaceAll("|", "\\|").replaceAll("~", "\\~")*/ // pasted myself
dclient.channels.cache.get(config.channels.console).send({content: `${msg}`}).catch(err => console.log(`[discord] error sending to console: ${err}`))
}, 2000)

Expand Down

0 comments on commit 5e714c2

Please sign in to comment.