From da8247526248762d0f85502b2f819a131d29d0db Mon Sep 17 00:00:00 2001 From: Aidan Date: Tue, 8 Jul 2025 20:49:30 -0400 Subject: [PATCH] bump, add /info endpoint, cleanup --- server/.gitignore | 8 +++++- server/config/config.example.ts | 25 ++++++++++++++++++ server/package.json | 8 +++--- server/src/index.ts | 45 ++++++++++++++++++++++++++++----- 4 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 server/config/config.example.ts diff --git a/server/.gitignore b/server/.gitignore index a857864..f7bbada 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -37,4 +37,10 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json bun.lock* # Files -serve/ \ No newline at end of file +serve/ + +# db +dev.db + +# config +config/config.ts \ No newline at end of file diff --git a/server/config/config.example.ts b/server/config/config.example.ts new file mode 100644 index 0000000..5dbc666 --- /dev/null +++ b/server/config/config.example.ts @@ -0,0 +1,25 @@ +export const config: Config = { + name: "Example Server", // what your server should be named in BeeSrv + author: "example", // who owns this server? + channelLink: "https://t.me/example", // telegram channel link + + currentBox: { + enabled: true, // if you want to serve the box, set to true + name: "Example Beebox", // what your box should be named in BeeSrv + originalAuthor: "Author", // who originally shared the beebox, if applicable + }, +} + + + +// don't change this +export interface Config { + name: string + author: string + channelLink: string + currentBox: { + enabled: boolean + name: string + originalAuthor: string + } +} \ No newline at end of file diff --git a/server/package.json b/server/package.json index ef5349c..f89c5a2 100644 --- a/server/package.json +++ b/server/package.json @@ -8,17 +8,17 @@ "start": "bun run src/index.ts" }, "devDependencies": { - "@types/bun": "^1.2.9", + "@types/bun": "^1.2.18", "drizzle-kit": "^0.30.6" }, "peerDependencies": { "typescript": "^5.8.3" }, "dependencies": { - "@types/express": "^5.0.1", - "better-sqlite3": "^11.9.1", + "@types/express": "^5.0.3", + "better-sqlite3": "^11.10.0", "chalk": "^5.4.1", - "dotenv": "^16.5.0", + "dotenv": "^16.6.1", "drizzle-orm": "^0.41.0", "express": "^5.1.0" } diff --git a/server/src/index.ts b/server/src/index.ts index 6f45e79..1551624 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -8,6 +8,7 @@ import { eq, and, gte } from "drizzle-orm" import chalk from "chalk" import * as schema from './db/schema' import path from 'path' +import { config } from '../config/config' const app = express() const sqlite = new Database(process.env.DB_FILE_NAME || 'dev.db') @@ -29,20 +30,50 @@ enum ReqStatus { function checkRateLimit(email: string): boolean { const now = Date.now() const userLimit = rateLimitMap.get(email) - + if (!userLimit || now > userLimit.resetTime) { rateLimitMap.set(email, { count: 1, resetTime: now + RATE_LIMIT_WINDOW_MS }) return true } - + if (userLimit.count >= MAX_REQUESTS_PER_WINDOW) { return false } - + userLimit.count++ return true } +app.get("/info", (async (req, res) => { + const info: Record = { + name: config.name, + author: config.author, + channelLink: config.channelLink, + }; + + if (config.currentBox.enabled) { + info.currentBox = config.currentBox; + try { + const beebox = await Bun.file(path.join(__dirname, '../serve/beebox.xml')) + const bbxContent = await beebox.text() + if (!bbxContent) { + console.log(chalk.red(`[INFO]`) + ` Error reading beebox file`) + info.error = "Unable to find beebox file on server" + } else { + const hasher = new Bun.CryptoHasher("sha256") + const bbxHash = await hasher.update(bbxContent).digest() + const bbxHashHex = bbxHash.toString('hex') + info.currentBox.hash = bbxHashHex + } + } catch (error) { + console.log(chalk.red(`[INFO]`) + ` Error reading beebox file`) + info.error = "Unable to find beebox file on server" + } + } + + res.json(info); +}) as RequestHandler) + app.post("/bee-req", (async (req, res) => { const { deviceID, version, email } = req.body const requestID = randomUUID() @@ -75,7 +106,7 @@ app.post("/bee-req", (async (req, res) => { console.log(chalk.yellow(`[REQ | ${requestID}]`) + ` Existing user: ${existingUser ? "true" : "false"}`) let userId: string; - + if (!existingUser) { try { // Create new user @@ -83,11 +114,11 @@ app.post("/bee-req", (async (req, res) => { id: randomUUID(), email: email as string, }).returning() - + if (!newUser[0]) { throw new Error("Failed to create user") } - + userId = newUser[0].id console.log(chalk.green(`[REQ | ${requestID}]`) + ` Created new user for request: ${userId}`) } catch (error) { @@ -165,6 +196,6 @@ app.listen(process.env.PORT || 3000, async () => { const pkgJson = Bun.file(path.join(__dirname, '../package.json')) const version = JSON.parse(await pkgJson.text()).version console.log(chalk.bgBlue("BEESRV") + " v" + version + "\n") - + console.log(chalk.green(`[SERVER] `) + `Running on port ${process.env.PORT || 3000}`) }) \ No newline at end of file