diff options
author | RaindropsSys <contact@minteck.org> | 2023-07-09 17:18:50 +0200 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-07-09 17:18:50 +0200 |
commit | 13d6e5b53b656c1db79d962d63cc047de8befa3b (patch) | |
tree | c8b2f59e07a80a2b04068ca67255f74b59500e57 /includes | |
parent | 2dd43d2822b6a4b0645e70afa0b7b3bad6321e15 (diff) | |
download | bridleshare-13d6e5b53b656c1db79d962d63cc047de8befa3b.tar.gz bridleshare-13d6e5b53b656c1db79d962d63cc047de8befa3b.tar.bz2 bridleshare-13d6e5b53b656c1db79d962d63cc047de8befa3b.zip |
Updated 5 files and added 3 files (automated)
Diffstat (limited to 'includes')
-rw-r--r-- | includes/functions.php | 12 | ||||
-rw-r--r-- | includes/header.php | 1 | ||||
-rw-r--r-- | includes/ws/index.js | 24 | ||||
-rw-r--r-- | includes/ws/upstream.js | 7 | ||||
-rw-r--r-- | includes/ws/upstream_run.js | 120 |
5 files changed, 137 insertions, 27 deletions
diff --git a/includes/functions.php b/includes/functions.php index 82dfea4..42c4ec1 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -54,24 +54,20 @@ function getLimits(): array { if (!isLoggedIn()) { return [ - 'speed' => 100*1024, - 'size' => 50*1024**2 + 'speed' => 500*1024 ]; } else { if (in_array(getUserID(), $app["high2"])) { return [ - 'speed' => 5*1024**2, - 'size' => 20480*1024**2 + 'speed' => 10*1024**2 ]; } elseif (in_array(getUserID(), $app["high1"])) { return [ - 'speed' => 1024**2, - 'size' => 1024*1024**2 + 'speed' => 3*1024**2 ]; } else { return [ - 'speed' => 500*1024, - 'size' => 100*1024**2 + 'speed' => 1024**2 ]; } } diff --git a/includes/header.php b/includes/header.php index 0a4627c..b168dae 100644 --- a/includes/header.php +++ b/includes/header.php @@ -12,6 +12,7 @@ <script src="/assets/bootstrap.bundle.min.js"></script> <script src="/assets/baseex.js"></script> <script src="/assets/libcrypt.js"></script> + <script src="/assets/qrcode.js"></script> <style> @media (max-width: 900px) { #box { diff --git a/includes/ws/index.js b/includes/ws/index.js index ef4073d..f01ac7c 100644 --- a/includes/ws/index.js +++ b/includes/ws/index.js @@ -29,7 +29,6 @@ wss.on('connection', function connection(ws, req) { let token = ws.token = parseCookies(req)['BS_SESSION_TOKEN'] ?? null; let user = ws.user = token && !token.includes("/") && token.startsWith("bs") && fs.existsSync("../tokens/" + token) ? JSON.parse(fs.readFileSync("../tokens/" + token).toString()) : null; let id = ws.id = crypto.randomBytes(96).toString("hex").match(/.{1,4}/g).map(i => parseInt(i, 16).toString(36)).join("").substring(0, 96); - let transmittedData = 0; users[id] = ws; @@ -40,23 +39,19 @@ wss.on('connection', function connection(ws, req) { if (user?.id && privilegeList['high2'].includes(user?.id)) { limits = ws.limits = { - speed: 5*1024**2, - size: 20480*1024**2 + speed: 5*1024**2 } } else if (user?.id && privilegeList['high1'].includes(user?.id)) { limits = ws.limits = { - speed: 1024**2, - size: 1024*1024**2 + speed: 1024**2 } } else if (user?.id) { limits = ws.limits = { - speed: 500*1024, - size: 100*1024**2 + speed: 500*1024 } } else { limits = ws.limits = { - speed: 100*1024, - size: 50*1024**2 + speed: 100*1024 } } @@ -69,16 +64,6 @@ wss.on('connection', function connection(ws, req) { if (data.type === "encrypted" || data.type === "encryptedData") { if (ws.peer && data.message) { - transmittedData += JSON.stringify(data.message).length; - - /*if (transmittedData >= limits.size * 1.02) { - ws.send(JSON.stringify({ - type: "error", - error: "SIZE_LIMIT_EXCEEDED" - })); - ws.close(); - }*/ - let delay = new Date().getTime() - ws.lastMessage > 0 ? Math.round((JSON.stringify(data.message).length / limits.speed) * 1000) : 0; setTimeout(() => { @@ -86,6 +71,7 @@ wss.on('connection', function connection(ws, req) { ws.peer.send(JSON.stringify({ type: data.type, + chunk: data.chunk ?? null, message: data.message, delay })); diff --git a/includes/ws/upstream.js b/includes/ws/upstream.js new file mode 100644 index 0000000..9b1bb82 --- /dev/null +++ b/includes/ws/upstream.js @@ -0,0 +1,7 @@ +const child_process = require('child_process'); + +child_process.execFile("node", [ "upstream_run.js", "19202" ], { stdio: "inherit" }); +child_process.execFile("node", [ "upstream_run.js", "19203" ], { stdio: "inherit" }); +child_process.execFile("node", [ "upstream_run.js", "19204" ], { stdio: "inherit" }); +child_process.execFile("node", [ "upstream_run.js", "19205" ], { stdio: "inherit" }); +child_process.execFile("node", [ "upstream_run.js", "19206" ], { stdio: "inherit" });
\ No newline at end of file diff --git a/includes/ws/upstream_run.js b/includes/ws/upstream_run.js new file mode 100644 index 0000000..193a7e2 --- /dev/null +++ b/includes/ws/upstream_run.js @@ -0,0 +1,120 @@ +const { WebSocketServer } = require('ws'); +const fs = require('fs'); +const crypto = require('crypto'); + +const tokens = {}; + +function parseCookies (request) { + const list = {}; + const cookieHeader = request.headers?.cookie; + if (!cookieHeader) return list; + + cookieHeader.split(`;`).forEach(function(cookie) { + let [ name, ...rest] = cookie.split(`=`); + name = name?.trim(); + if (!name) return; + const value = rest.join(`=`).trim(); + if (!value) return; + list[name] = decodeURIComponent(value); + }); + + return list; +} + +function connectionHandler(ws, req, port) { + let privilegeList = JSON.parse(fs.readFileSync("../app.json").toString()); + + let token = ws.token = parseCookies(req)['BS_SESSION_TOKEN'] ?? null; + let user = ws.user = token && !token.includes("/") && token.startsWith("bs") && fs.existsSync("../tokens/" + token) ? JSON.parse(fs.readFileSync("../tokens/" + token).toString()) : null; + + let limits; + + ws.token = null; + ws.peer = null; + ws.lastMessage = 0; + + if (user?.id && privilegeList['high2'].includes(user?.id)) { + limits = ws.limits = { + speed: 2*1024**2 + } + } else if (user?.id && privilegeList['high1'].includes(user?.id)) { + limits = ws.limits = { + speed: 1024**2 + } + } else if (user?.id) { + limits = ws.limits = { + speed: 500*1024 + } + } + + ws.on('error', console.error); + + ws.on('message', function message(_data) { + try { + let data = JSON.parse(_data); + if (!data.type || (data.type && data.type !== "keepalive" && data.type !== "encryptedData" && data.type !== "encrypted")) console.log(data); + + if (data.type === "encryptedData") { + if (ws.peer && data.message) { + let delay = new Date().getTime() - ws.lastMessage > 0 ? Math.round((JSON.stringify(data.message).length / limits.speed) * 1000) : 0; + + setTimeout(() => { + ws.lastMessage = new Date().getTime(); + + ws.peer.send(JSON.stringify({ + type: data.type, + chunk: data.chunk ?? null, + message: data.message, + delay + })); + }, delay); + } + } + + if (data.type === "token") { + if (tokens[data.token]) { + tokens[data.token][1] = ws; + ws.peer = tokens[data.token][0]; + ws.token = data.token; + tokens[data.token][0].peer = ws; + } else { + if (!user?.id || (port > 19204 && !(user?.id && privilegeList['high2'].includes(user?.id))) || (port > 19203 && !(user?.id && privilegeList['high1'].includes(user?.id)) && !(user?.id && privilegeList['high2'].includes(user?.id)))) ws.close(); + tokens[data.token] = [ ws, null ]; + } + } + } catch (e) { + console.error(e); + } + }); + + ws.on('close', () => { + if (ws.peer) { + ws.peer.close(); + if (ws.token) delete tokens[ws.token]; + } + }) + + ws.send(JSON.stringify({ + type: 'init', + limits, + code: ws.id + })); +} + +const { createServer } = require('https'); + +function startServer(port) { + const server = createServer({ + cert: fs.readFileSync('/etc/letsencrypt/live/minteck.org/fullchain.pem'), + key: fs.readFileSync('/etc/letsencrypt/live/minteck.org/privkey.pem') + }); + + const wss = new WebSocketServer({ server }); + wss.on('connection', (ws, req) => { + connectionHandler(ws, req, port); + }); + + server.listen(port); +} + +startServer(parseInt(process.argv[2]));
\ No newline at end of file |