From dd39c3e63cb47995cc81761909d6ae68b4f1bb52 Mon Sep 17 00:00:00 2001 From: Tianpao Date: Sat, 1 Nov 2025 03:27:28 +0800 Subject: [PATCH 1/3] chore:docker yaml --- .gitignore | 5 ++++- docker-compose.yml | 3 ++- package.json | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 package.json diff --git a/.gitignore b/.gitignore index 33ac004..4e41a30 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ thewhitesilk_config.json thewhitesilk_data/ deno.lock -node_modules/ \ No newline at end of file +node_modules/ + +#npm +package-lock.json \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index aa097a1..efa5aa5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,4 +12,5 @@ services: restart: always volumes: - ./thewhitesilk_config.json:/app/thewhitesilk_config.json - - ./thewhitesilk_data:/app/thewhitesilk_data \ No newline at end of file + - ./thewhitesilk_data:/app/thewhitesilk_data + network_mode: bridge diff --git a/package.json b/package.json new file mode 100644 index 0000000..c18234b --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "lingchair", + "version": "1.0.0", + "description": "铃之椅", + "repository": { + "type": "git", + "url": "https://codeberg.org/CrescentLeaf/LingChair.git" + }, + "license": "ISC", + "author": "CrescentLeaf", + "type": "module", + "main": "index.js", + "scripts": { + "test":"" + "build-server": "tsc ." + }, + "devDependencies": { + "@types/cookie-parser": "^1.4.10", + "@types/express": "^5.0.5", + "typescript": "^5.9.3" + }, + "dependencies": { + "chalk": "^5.6.2", + "cookie-parser": "^1.4.7", + "express": "^5.1.0", + "file-type": "^21.0.0", + "socket.io": "^4.8.1" + } +} From ed5e9623708463681f0813545e0d4bdb5378a12f Mon Sep 17 00:00:00 2001 From: Tianpao Date: Sat, 1 Nov 2025 04:19:17 +0800 Subject: [PATCH 2/3] refactor: middleware --- package.json | 2 +- server/main.ts | 65 ++++-------------------------------------- server/middleware.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 server/middleware.ts diff --git a/package.json b/package.json index c18234b..d689892 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "type": "module", "main": "index.js", "scripts": { - "test":"" + "test":"", "build-server": "tsc ." }, "devDependencies": { diff --git a/server/main.ts b/server/main.ts index 36b6992..394396b 100644 --- a/server/main.ts +++ b/server/main.ts @@ -11,50 +11,22 @@ import process from "node:process" import chalk from "chalk" import child_process from "node:child_process" import FileManager from "./data/FileManager.ts" -import TokenManager from "./api/TokenManager.ts" -import UserChatLinker from "./data/UserChatLinker.ts" import path from "node:path" import cookieParser from 'cookie-parser' import fs from 'node:fs/promises' // @ts-types="npm:@types/express-fileupload" import fileUpload from 'express-fileupload' +import { Middleware } from "./middleware.ts" const app = express() app.use('/', express.static(config.data_path + '/page_compiled')) app.use(cookieParser()) -app.get('/uploaded_files/:hash', (req, res) => { - const hash = req.params.hash as string - if (hash == null) { - res.status(404).send({ - msg: "404 Not Found", - }) - return - } - const file = FileManager.findByHash(hash) +app.get('/uploaded_files/:hash',Middleware.Get_uploaded_files, (req, res) => { + const file = FileManager.findByHash(req.params.hash as string) if (file == null) { - res.status(404).send({ - msg: "404 Not Found", - }) - return + return; } - - if (file.getChatId() != null) { - const userToken = TokenManager.decode(req.headers.token || req.cookies.token) - if (!TokenManager.checkToken(userToken, req.headers['device-id'] || req.cookies.device_id)) { - res.status(401).send({ - msg: "401 UnAuthorized", - }) - return - } - if (!UserChatLinker.checkUserIsLinkedToChat(userToken.author, file.getChatId() as string)) { - res.status(403).send({ - msg: "403 Forbidden", - }) - return - } - } - const fileName = encodeURIComponent(file!.getName()?.replaceAll('"', '')) res.setHeader('Content-Disposition', `inline; filename="${fileName}"`) res.setHeader('Content-Type', file!.getMime()) @@ -69,35 +41,8 @@ app.use(fileUpload({ tempFileDir: config.data_path + '/upload_cache', abortOnLimit: true, })) -app.post('/upload_file', async (req, res) => { - const userToken = TokenManager.decode(req.headers.token || req.cookies.token) - if (!TokenManager.checkToken(userToken, req.headers['device-id'] || req.cookies.device_id)) { - res.status(401).send({ - msg: "401 UnAuthorized", - }) - return - } - if (req.body.chat_id && !UserChatLinker.checkUserIsLinkedToChat(userToken.author, req.body.chat_id)) { - res.status(403).send({ - msg: "403 Forbidden", - }) - return - } - +app.post('/upload_file',Middleware.Post_upload_file, async (req, res) => { const file = req.files?.file as fileUpload.UploadedFile - if (file?.data == null) { - res.status(400).send({ - msg: "No file was found or multiple files were uploaded", - }) - return - } - if (req.body.file_name == null) { - res.status(400).send({ - msg: "Filename is required", - }) - return - } - const hash = (await FileManager.uploadFile(req.body.file_name, await fs.readFile(file.tempFilePath), req.body.chat_id)).getHash() res.status(200).send({ diff --git a/server/middleware.ts b/server/middleware.ts new file mode 100644 index 0000000..c779c32 --- /dev/null +++ b/server/middleware.ts @@ -0,0 +1,68 @@ +import { Request, Response, NextFunction } from "express"; +import FileManager from "./data/FileManager.ts"; +import TokenManager from "./api/TokenManager.ts"; +import UserChatLinker from "./data/UserChatLinker.ts"; +import fileUpload from "express-fileupload"; + +export class Middleware { + static Authroize(req: Request, res: Response, chat_id: string | undefined) { + const userToken = TokenManager.decode(req.headers.token || req.cookies.token); + if (!TokenManager.checkToken(userToken, req.headers["device-id"] || req.cookies.device_id)) { + res.status(401).send({ + msg: "401 UnAuthorized", + }); + return false; + } + if (chat_id && !UserChatLinker.checkUserIsLinkedToChat(userToken.author, chat_id)) { + res.status(403).send({ + msg: "403 Forbidden", + }); + return false; + } + return true; + } + + static Get_uploaded_files(req: Request, res: Response, next: NextFunction) { + const hash = req.params.hash as string; + if (hash == null) { + res.status(404).send({ + msg: "404 Not Found", + }); + return; + } + const file = FileManager.findByHash(hash); + + if (file == null) { + res.status(404).send({ + msg: "404 Not Found", + }); + return; + } + + if (file.getChatId() != null) { + if (!Middleware.Authroize(req, res, file.getChatId() as string)) { + return; + } + } + next(); + } + + static Post_upload_file(req: Request, res: Response, next: NextFunction) { + if (!Middleware.Authroize(req, res, req.body.chat_id)) { + return; + } + const file = req.files?.file as fileUpload.UploadedFile; + if (file?.data == null) { + res.status(400).send({ + msg: "No file was found or multiple files were uploaded", + }); + return; + } + if (req.body.file_name == null) { + res.status(400).send({ + msg: "Filename is required", + }); + return; + } + } +} From 3a5641596811c7cbcf4b8f0ff9fac6c81ae4bf02 Mon Sep 17 00:00:00 2001 From: Tianpao Date: Sat, 1 Nov 2025 09:28:26 +0800 Subject: [PATCH 3/3] fix: install error --- package.json | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 package.json diff --git a/package.json b/package.json deleted file mode 100644 index d689892..0000000 --- a/package.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "lingchair", - "version": "1.0.0", - "description": "铃之椅", - "repository": { - "type": "git", - "url": "https://codeberg.org/CrescentLeaf/LingChair.git" - }, - "license": "ISC", - "author": "CrescentLeaf", - "type": "module", - "main": "index.js", - "scripts": { - "test":"", - "build-server": "tsc ." - }, - "devDependencies": { - "@types/cookie-parser": "^1.4.10", - "@types/express": "^5.0.5", - "typescript": "^5.9.3" - }, - "dependencies": { - "chalk": "^5.6.2", - "cookie-parser": "^1.4.7", - "express": "^5.1.0", - "file-type": "^21.0.0", - "socket.io": "^4.8.1" - } -}