Lanyard Proxy
This commit is contained in:
parent
0b3d3db61f
commit
61467f2635
4 changed files with 110 additions and 13 deletions
12
basicDocs.json
Normal file
12
basicDocs.json
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"info": "Welcome to the API! It's still a work in process, so there's not much here yet. I don't really know how to do APIs yet so expect it to suck if I add anything.",
|
||||||
|
"documentation": "https://codeberg.org/Bingus_Violet/api.violets-purgatory.dev",
|
||||||
|
"current_commands": {
|
||||||
|
"/v1/pfp": {
|
||||||
|
"description": "Generates a random PFP. For use on the main page & with UserPFP on Discord."
|
||||||
|
},
|
||||||
|
"/v1/lanyard": {
|
||||||
|
"description": "Fetches the most recently updated Lanyard update. Only gives data about *my* lanyard status. Don't worry about 'overusing' this, as it doesn't call a request to Lanyard or Discord everytime, it just gets the most recent data gathered by the web socket. Mainly designed to prevent my many sites from overloading the Lanyard socket, but also so that I can add extra features in the future if I so desire."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
85
index.js
85
index.js
|
@ -1,32 +1,95 @@
|
||||||
const express = require('express'),
|
const express = require('express'),
|
||||||
path = require("path"),
|
path = require("path"),
|
||||||
fs = require("fs")
|
fs = require("fs"),
|
||||||
|
WebSocket = require("ws")
|
||||||
|
|
||||||
const PORT = process.env.PORT || 8080
|
const PORT = process.env.PORT || 8080
|
||||||
|
|
||||||
var app = express()
|
var app = express()
|
||||||
|
|
||||||
|
var lanyardData = "Nothing Fetched yet!"
|
||||||
|
|
||||||
|
var sockets = []
|
||||||
|
|
||||||
function randomFileInDir(directory) {
|
function randomFileInDir(directory) {
|
||||||
var dir = fs.readdirSync(directory)
|
var dir = fs.readdirSync(directory)
|
||||||
return path.join(directory, dir[Math.floor(Math.random() * dir.length)])
|
return path.join(directory, dir[Math.floor(Math.random() * dir.length)])
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.send({
|
var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json"))
|
||||||
info: "Welcome to the API! It's still a work in process, so there's not much here yet. I don't really know how to do APIs yet so expect it to suck if I add anything.",
|
docs = JSON.parse(docs)
|
||||||
documentation: "https://codeberg.org/Bingus_Violet/api.violets-purgatory.dev/wiki",
|
res.send(docs)
|
||||||
current_commands: {
|
|
||||||
"/v1/pfp": {
|
|
||||||
description: "Generates a random PFP. For use on the main page & with UserPFP on Discord."
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/v1/pfp", (req, res) => {
|
app.get("/v1/pfp", (req, res) => {
|
||||||
res.sendFile(randomFileInDir(path.join(__dirname, 'pfps')))
|
res.sendFile(randomFileInDir(path.join(__dirname, 'pfps')))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get("/v1/lanyard", (req, res) => {
|
||||||
|
res.send(lanyardData)
|
||||||
|
})
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log("API is now listening on port 8080!")
|
console.log("API is now listening on port 8080!")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
|
||||||
|
|
||||||
|
function beat(dur) {
|
||||||
|
lanyard.send(JSON.stringify({
|
||||||
|
op: 3
|
||||||
|
}))
|
||||||
|
setTimeout(() => {
|
||||||
|
beat(dur)
|
||||||
|
}, dur);
|
||||||
|
}
|
||||||
|
|
||||||
|
lanyard.addEventListener("message", async (res) => {
|
||||||
|
var data = JSON.parse(res.data)
|
||||||
|
if (data.op == 1) {
|
||||||
|
beat(data.d.heartbeat_interval)
|
||||||
|
lanyard.send(JSON.stringify({
|
||||||
|
op: 2,
|
||||||
|
d: {
|
||||||
|
subscribe_to_id: "534132311781015564"
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
} else if (data.op == 0) {
|
||||||
|
lanyardData = data
|
||||||
|
|
||||||
|
for (let index = 0; index < sockets.length; index++) {
|
||||||
|
var socketData = sockets[index]
|
||||||
|
var socket = socketData.socket;
|
||||||
|
socket.send(JSON.stringify(lanyardData))
|
||||||
|
|
||||||
|
if ((Date.now() - socketData.lastPing) > 120000) {
|
||||||
|
sockets.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
wsServer = new WebSocket.WebSocketServer({ port: 8090 })
|
||||||
|
|
||||||
|
wsServer.on("connection", function connection(socket) {
|
||||||
|
socket.on('message', function message(data) {
|
||||||
|
data = JSON.parse(data)
|
||||||
|
console.log(data)
|
||||||
|
if (data.op == 3) {
|
||||||
|
for (let index = 0; index < sockets.length; index++) {
|
||||||
|
const socketData = sockets[index];
|
||||||
|
if (socketData.socket == socket) {
|
||||||
|
sockets[index].lastPing = Date.now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.send(`{"op": 3}`)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
socket.send(JSON.stringify(lanyardData))
|
||||||
|
socket.send(`{ "op": 1 }`)
|
||||||
|
|
||||||
|
sockets.push({socket, lastPing: Date.now()})
|
||||||
|
})
|
23
package-lock.json
generated
23
package-lock.json
generated
|
@ -9,7 +9,8 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2"
|
"express": "^4.18.2",
|
||||||
|
"ws": "^8.16.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
|
@ -653,6 +654,26 @@
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.8"
|
"node": ">= 0.8"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "8.16.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz",
|
||||||
|
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": ">=5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
"author": "violet@violets-purgatory.dev",
|
"author": "violet@violets-purgatory.dev",
|
||||||
"license": "GPL-3.0-or-later",
|
"license": "GPL-3.0-or-later",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2"
|
"express": "^4.18.2",
|
||||||
|
"ws": "^8.16.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue