fix: add query user config to redirect home

This commit is contained in:
2025-04-02 22:16:11 +08:00
parent 912d3196cf
commit 3b32ba7244
8 changed files with 238 additions and 14 deletions

View File

@@ -47,12 +47,13 @@ export const getLoginUser = async (req: http.IncomingMessage) => {
const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
const parsedCookies = cookie.parse(req.headers.cookie || '');
console.log('getLoginUser', parsedCookies, 'parsedCookies.token', parsedCookies.token);
if (token) {
token = token.replace('Bearer ', '');
}
if (!token) {
return null;
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);

View File

@@ -38,11 +38,6 @@ type ConfigType = {
* 允许跨域访问的地址
*/
allowedOrigin: string[];
/**
* home 不在代理范围内跳转到的地址
*/
home: string;
};
};
@@ -65,6 +60,5 @@ export const config: ConfigType = {
domain: envConfig.PROXY_DOMAIN,
resources: envConfig.PROXY_RESOURCES,
allowedOrigin: (envConfig.PROXY_ALLOWED_ORIGINS as string)?.split(',') || [],
home: envConfig.PROXY_HOME,
},
};

View File

@@ -10,17 +10,16 @@ import { fileProxy } from './proxy/file-proxy.ts';
import { getTextFromStreamAndAddStat, httpProxy } from './proxy/http-proxy.ts';
import { UserPermission } from '@kevisual/permission';
import { getLoginUser } from '@/middleware/auth.ts';
import { rediretHome } from './user-home/index.ts';
const api = config?.api || { host: 'kevisual.xiongxiao.me', path: '/api/router' };
const domain = config?.proxy?.domain || 'kevisual.xiongxiao.me';
const allowedOrigins = config?.proxy?.allowedOrigin || [];
const home = config?.proxy?.home || '/ai/chat';
const noProxyUrl = ['/', '/favicon.ico'];
export const handleRequest = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const querySearch = new URL(req.url, `http://${req.headers.host}`).searchParams;
const password = querySearch.get('p');
const loginUser = await getLoginUser(req);
console.log('loginUser', loginUser);
if (req.url === '/favicon.ico') {
res.writeHead(200, { 'Content-Type': 'image/x-icon' });
res.end('proxy no favicon.ico\n');
@@ -146,8 +145,10 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
if (url === '/') {
// TODO: 获取一下登陆用户如果没有登陆用户重定向到ai-chat页面
// 重定向到
res.writeHead(302, { Location: home });
return res.end();
// res.writeHead(302, { Location: home });
// return res.end();
rediretHome(req, res);
return;
}
// 不是域名代理且是在不代理的url当中
res.write('No proxy for this URL\n');
@@ -257,7 +258,6 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
username: loginUser?.tokenUser?.username || '',
password: password,
});
console.log('checkPermission', loginUser, permission);
if (!checkPermission.success) {
return createNotFoundPage('no permission');
}

10
src/module/query.ts Normal file
View File

@@ -0,0 +1,10 @@
import { Query } from '@kevisual/query';
import { QueryConfig } from '@kevisual/query-config';
export const query = new Query({
url: 'https://kevisual.cn/api/router',
});
export const queryConfig = new QueryConfig({
query: query,
});

View File

@@ -1,6 +1,6 @@
import { config } from '../config.ts';
const api = config?.api || { host: 'https://kevisual.xiongxiao.me', path: '/api/router' };
const api = config?.api || { host: 'https://kevisual.cn', path: '/api/router' };
const apiPath = api.path || '/api/router';
export const fetchTest = async (id: string) => {
const fetchUrl = api.host + apiPath;

View File

@@ -0,0 +1,31 @@
import http from 'http';
import { getLoginUser } from '@/middleware/auth.ts';
import { queryConfig } from '../query.ts';
/**
* 重定向到用户首页
* @param req
* @param res
*/
export const rediretHome = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const user = await getLoginUser(req);
if (!user?.token) {
res.writeHead(302, { Location: '/user/login/' });
res.end();
return;
}
let redirectURL = '/root/center/';
try {
const token = user.token;
const resConfig = await queryConfig.getConfigByKey('user.json', { token });
if (resConfig.code === 200) {
const configData = resConfig.data?.data as any;
redirectURL = configData?.redirectURL || redirectURL;
}
} catch (error) {
console.error('get resConfig user.json', error);
} finally {
res.writeHead(302, { Location: redirectURL });
res.end();
}
};