From 9d5867d409f0661e35affee573dbd0ba6ded683b Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Thu, 20 Jun 2024 11:55:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=8AauthRoute=E6=8B=86?= =?UTF-8?q?=E5=87=BA=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cluster.ts | 23 ++++------------------- src/routes/auth.route.ts | 24 ++++++++++++++++++++++++ src/{ => routes}/measure.route.ts | 4 ++-- 3 files changed, 30 insertions(+), 21 deletions(-) create mode 100644 src/routes/auth.route.ts rename src/{ => routes}/measure.route.ts (90%) diff --git a/src/cluster.ts b/src/cluster.ts index b36e873..1a73755 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -21,7 +21,7 @@ import {userInfo} from 'node:os' import {tmpdir} from 'os' import pMap from 'p-map' import pRetry from 'p-retry' -import {basename, dirname, join} from 'path' +import {dirname, join} from 'path' import {connect, Socket} from 'socket.io-client' import {Tail} from 'tail' import {fileURLToPath} from 'url' @@ -30,7 +30,8 @@ import {FileListSchema} from './constants.js' import {validateFile} from './file.js' import {Keepalive} from './keepalive.js' import {logger} from './logger.js' -import MeasureRouteFactory from './measure.route.js' +import {AuthRouteFactory} from './routes/auth.route.js' +import MeasureRouteFactory from './routes/measure.route.js' import {getStorage, type IStorage} from './storage/base.storage.js' import type {TokenManager} from './token.js' import type {IFileList} from './types.js' @@ -271,23 +272,7 @@ export class Cluster { const app = http2Express(express) app.enable('trust proxy') - app.get('/auth', (req: Request, res: Response, next: NextFunction) => { - try { - const oldUrl = req.get('x-original-uri') - if (!oldUrl) return res.status(403).send('invalid sign') - - const url = new URL(oldUrl, 'http://localhost') - const hash = basename(url.pathname) - const query = Object.fromEntries(url.searchParams.entries()) - const signValid = checkSign(hash, this.clusterSecret, query) - if (!signValid) { - return res.status(403).send('invalid sign') - } - res.sendStatus(204) - } catch (e) { - return next(e) - } - }) + app.get('/auth', AuthRouteFactory(config)) if (!config.disableAccessLog) { app.use(morgan('combined')) diff --git a/src/routes/auth.route.ts b/src/routes/auth.route.ts new file mode 100644 index 0000000..86e5e65 --- /dev/null +++ b/src/routes/auth.route.ts @@ -0,0 +1,24 @@ +import type {NextFunction, Request, Response} from 'express' +import {basename} from 'path' +import {Config} from '../config.js' +import {checkSign} from '../util.js' + +export function AuthRouteFactory(config: Config) { + return (req: Request, res: Response, next: NextFunction) => { + try { + const oldUrl = req.get('x-original-uri') + if (!oldUrl) return res.status(403).send('invalid sign') + + const url = new URL(oldUrl, 'http://localhost') + const hash = basename(url.pathname) + const query = Object.fromEntries(url.searchParams.entries()) + const signValid = checkSign(hash, config.clusterSecret, query) + if (!signValid) { + return res.status(403).send('invalid sign') + } + res.sendStatus(204) + } catch (e) { + return next(e) + } + } +} diff --git a/src/measure.route.ts b/src/routes/measure.route.ts similarity index 90% rename from src/measure.route.ts rename to src/routes/measure.route.ts index 89d378e..29d3e1c 100644 --- a/src/measure.route.ts +++ b/src/routes/measure.route.ts @@ -1,6 +1,6 @@ import express, {type Router} from 'express' -import type {Config} from './config.js' -import {checkSign} from './util.js' +import type {Config} from '../config.js' +import {checkSign} from '../util.js' export default function MeasureRouteFactory(config: Config): Router { const router = express.Router()