From 6facd5f6ccaf61ce41010ef94858d9a504a28990 Mon Sep 17 00:00:00 2001 From: Tianpao Date: Sat, 1 Nov 2025 10:57:15 +0800 Subject: [PATCH] init:init to origin --- .gitignore | 8 ++++++++ config.example.json | 6 ++++++ package.json | 27 +++++++++++++++++++++++++++ src/config.ts | 23 +++++++++++++++++++++++ src/main.ts | 20 ++++++++++++++++++++ src/middleware/Auth.ts | 16 ++++++++++++++++ src/router/auth.ts | 10 ++++++++++ src/utils/jwt.ts | 15 +++++++++++++++ tsconfig.json | 16 ++++++++++++++++ 9 files changed, 141 insertions(+) create mode 100644 config.example.json create mode 100644 package.json create mode 100644 src/config.ts create mode 100644 src/main.ts create mode 100644 src/middleware/Auth.ts create mode 100644 src/router/auth.ts create mode 100644 src/utils/jwt.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index 2309cc8..18864ad 100644 --- a/.gitignore +++ b/.gitignore @@ -136,3 +136,11 @@ dist .yarn/install-state.gz .pnp.* +# package +package-lock.json +pnpm-lock.yaml + +# config +.env +config.json + diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..5ab3c3b --- /dev/null +++ b/config.example.json @@ -0,0 +1,6 @@ +{ + "express":{ + "port": 3000, + "jwtSecret": "" + } +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..25a890f --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "Master", + "version": "1.0.0", + "description": "", + "main": "main.js", + "scripts": { + "start": "node dist/main.js", + "build": "tsc", + "test": "tsc && node dist/main.js" + }, + "keywords": [], + "author": "Tianpao", + "license": "ISC", + "type": "module", + "packageManager": "pnpm@10.18.0", + "devDependencies": { + "@tsconfig/node22": "^22.0.2", + "@types/express": "^5.0.5", + "@types/jsonwebtoken": "^9.0.10", + "typescript": "^5.9.3" + }, + "dependencies": { + "express": "^5.1.0", + "jsonwebtoken": "^9.0.2", + "socket.io": "^4.8.1" + } +} diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..438651c --- /dev/null +++ b/src/config.ts @@ -0,0 +1,23 @@ +import fs from "node:fs"; + +export interface Iconfig { + express: { + port: number; + jwtSecret: string; + } +} + +const defaultConfig = { + "express":{ + "port": 3000, + "jwtSecret": "JwtTianpaoMasterEasyTier000" + } +} + +if(!fs.existsSync("./config.json")){ + fs.writeFileSync("./config.json", JSON.stringify(defaultConfig)); +} + +const config:Iconfig = JSON.parse(fs.readFileSync("./config.json", "utf8")) + +export default config \ No newline at end of file diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..373979b --- /dev/null +++ b/src/main.ts @@ -0,0 +1,20 @@ +import express from 'express' +import { Server } from 'socket.io' +import http from 'node:http' +import config from './config.js' + +import authRouter from './router/auth.js' + +const app = express() +const server = http.createServer(app) +const io = new Server(server) + +app.use(express.json()) //Loading JSON + +/* Routes */ +app.use('/cluster/auth', authRouter) +/* Routes */ + +server.listen(config.express.port,()=>{ + console.log(`Server listening on port ${config.express.port}`) +}) \ No newline at end of file diff --git a/src/middleware/Auth.ts b/src/middleware/Auth.ts new file mode 100644 index 0000000..81b87d2 --- /dev/null +++ b/src/middleware/Auth.ts @@ -0,0 +1,16 @@ +import { Request,Response,NextFunction } from "express"; + +export class AuthMiddleware { + + static Challenge(req: Request, res: Response, next: NextFunction) { + const id = req.query.clusterId + if (!id){ + res.status(401).json({ + message: "where is your clusterId?" + }) + return; + } + + next() //Latest + } +} \ No newline at end of file diff --git a/src/router/auth.ts b/src/router/auth.ts new file mode 100644 index 0000000..22dae0f --- /dev/null +++ b/src/router/auth.ts @@ -0,0 +1,10 @@ +import { Router } from "express"; +import { AuthMiddleware } from "../middleware/Auth.js"; + +const router = Router(); + +router.post('/challenge',AuthMiddleware.Challenge,(req,res)=>{ + +}) + +export default router; \ No newline at end of file diff --git a/src/utils/jwt.ts b/src/utils/jwt.ts new file mode 100644 index 0000000..59ccead --- /dev/null +++ b/src/utils/jwt.ts @@ -0,0 +1,15 @@ +import jsonwebtoken from 'jsonwebtoken'; +import config from '../config.js'; +export class Jwt { + static sign(payload: object){ + return jsonwebtoken.sign(payload,config.express.jwtSecret,{ + expiresIn: 60 * 60 * 24, // 24 hours + algorithm: "HS256" + + }) + } + + static verify(token: string) { + return jsonwebtoken.verify(token,config.express.jwtSecret) + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5675328 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "extends": ["@tsconfig/node22"], + "compilerOptions": { + "sourceMap": true, + "noUnusedLocals": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "resolveJsonModule": true, + "outDir": "dist", + "skipLibCheck": true + }, + "exclude": [ + "node_modules", + "public/**/*" + ] +} \ No newline at end of file