feat: 优化 getToken 函数,移除无用的响应参数,调整授权过滤逻辑以支持新路径

This commit is contained in:
2025-12-23 13:15:16 +08:00
parent 4bdebd66d4
commit 371d66b289
3 changed files with 8 additions and 13 deletions

View File

@@ -15,14 +15,9 @@ const cookie = {
return cookies; return cookies;
} }
} }
export const getToken = async (req: http.IncomingMessage, res: http.ServerResponse) => { export const getToken = async (req: http.IncomingMessage) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || ''; let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost'); const url = new URL(req.url || '', 'http://localhost');
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (!token) { if (!token) {
token = url.searchParams.get('token') || ''; token = url.searchParams.get('token') || '';
} }
@@ -30,9 +25,6 @@ export const getToken = async (req: http.IncomingMessage, res: http.ServerRespon
const parsedCookies = cookie.parse(req.headers.cookie || ''); const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || ''; token = parsedCookies.token || '';
} }
if (!token) {
return resNoPermission();
}
if (token) { if (token) {
token = token.replace('Bearer ', ''); token = token.replace('Bearer ', '');
} }

View File

@@ -10,7 +10,7 @@ import chalk from 'chalk';
import { AssistantApp } from './lib.ts'; import { AssistantApp } from './lib.ts';
import { getBunPath } from './module/get-bun-path.ts'; import { getBunPath } from './module/get-bun-path.ts';
import { qwenAsr } from './services/asr/qwen-asr.ts'; import { qwenAsr } from './services/asr/qwen-asr.ts';
export const runServer = async (port: number = 51015, listenPath = '127.0.0.1') => { export const runServer = async (port: number = 51515, listenPath = '127.0.0.1') => {
let _port: number | undefined; let _port: number | undefined;
if (port) { if (port) {
_port = await getPort({ port }); _port = await getPort({ port });
@@ -21,7 +21,7 @@ export const runServer = async (port: number = 51015, listenPath = '127.0.0.1')
} }
if (!_port) { if (!_port) {
// 检车端口可用性 // 检车端口可用性
const isPortAvailable = await getPort({ port: portNumbers(51015, 52000) }); const isPortAvailable = await getPort({ port: portNumbers(51515, 52000) });
if (!isPortAvailable) { if (!isPortAvailable) {
console.log(`Port ${isPortAvailable} is not available`); console.log(`Port ${isPortAvailable} is not available`);
process.exit(1); process.exit(1);

View File

@@ -34,7 +34,7 @@ const authFilter = async (req: http.IncomingMessage, res: http.ServerResponse) =
return false; return false;
} }
// 放开首页 // 放开首页
if (pathname.startsWith('/root/home')) { if (pathname.startsWith('/root/home') || pathname === '/root/cli') {
return false; return false;
} }
// 放开api 以 /api /v1, /client, /serve 开头的请求 // 放开api 以 /api /v1, /client, /serve 开头的请求
@@ -47,8 +47,11 @@ const authFilter = async (req: http.IncomingMessage, res: http.ServerResponse) =
if (share === 'public') { if (share === 'public') {
return false; return false;
} }
const { token } = await getToken(req, res) const { token } = await getToken(req)
if (!token) { if (!token) {
// no token 转到登录页面
res.writeHead(302, { Location: `/root/home/` });
res.end();
return false; return false;
} }
const tokenUser = await getTokenUserCache(token); const tokenUser = await getTokenUserCache(token);