Skip to content

Commit

Permalink
refactor: 把authRoute拆出去
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jun 20, 2024
1 parent de151c0 commit 9d5867d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
23 changes: 4 additions & 19 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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'))
Expand Down
24 changes: 24 additions & 0 deletions src/routes/auth.route.ts
Original file line number Diff line number Diff line change
@@ -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')

Check warning on line 10 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (20)

No magic number: 403

Check warning on line 10 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (22)

No magic number: 403

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')

Check warning on line 17 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (20)

No magic number: 403

Check warning on line 17 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (22)

No magic number: 403
}
res.sendStatus(204)

Check warning on line 19 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (20)

No magic number: 204

Check warning on line 19 in src/routes/auth.route.ts

View workflow job for this annotation

GitHub Actions / build (22)

No magic number: 204
} catch (e) {
return next(e)
}
}
}
4 changes: 2 additions & 2 deletions src/measure.route.ts → src/routes/measure.route.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit 9d5867d

Please sign in to comment.