feat: 注释掉调试日志,优化代码清晰度
This commit is contained in:
@@ -40,7 +40,7 @@ export const wssFun: WebSocketListenerFun = async (req, res) => {
|
|||||||
}
|
}
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const userApp = ws.data.userApp;
|
const userApp = ws.data.userApp;
|
||||||
logger.debug('message', data, ' userApp=', userApp);
|
// logger.debug('message', data, ' userApp=', userApp);
|
||||||
const wsMessage = wsProxyManager.get(userApp);
|
const wsMessage = wsProxyManager.get(userApp);
|
||||||
if (wsMessage) {
|
if (wsMessage) {
|
||||||
wsMessage.sendResponse(data);
|
wsMessage.sendResponse(data);
|
||||||
|
|||||||
@@ -84,23 +84,27 @@ class WsMessage {
|
|||||||
pathname: this.id ? `/${this.user}/v1/${shortAppId}` : '',
|
pathname: this.id ? `/${this.user}/v1/${shortAppId}` : '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
async sendData(data: any, context?: any, opts?: { timeout?: number }) {
|
async sendData(data: any, context?: any, opts?: { timeout?: number }): Promise<{ code: number, data?: any, message?: string }> {
|
||||||
if (this.ws.readyState !== WebSocket.OPEN) {
|
if (this.ws.readyState !== WebSocket.OPEN) {
|
||||||
return { code: 500, message: 'WebSocket is not open' };
|
return { code: 500, message: 'WebSocket is not open' };
|
||||||
}
|
}
|
||||||
const timeout = opts?.timeout || 10 * 6 * 1000; // 10 minutes
|
const timeout = opts?.timeout || 10 * 6 * 1000; // 10 minutes
|
||||||
const id = nanoid();
|
const id = nanoid();
|
||||||
|
const msg = { path: data?.path, key: data?.key, id: data?.id };
|
||||||
|
if (msg?.key?.includes('stream')) {
|
||||||
|
// @ts-ignore
|
||||||
|
msg.base64 = data?.base64 ?? true;
|
||||||
|
}
|
||||||
const message = JSON.stringify({
|
const message = JSON.stringify({
|
||||||
id,
|
id,
|
||||||
type: 'proxy',
|
type: 'proxy',
|
||||||
data: {
|
data: {
|
||||||
message: data,
|
message: { ...data, ...msg },
|
||||||
context: context || {},
|
context: context || {},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
logger.info('ws-proxy sendData', message);
|
// logger.info('ws-proxy sendData', message);
|
||||||
this.ws.send(message);
|
this.ws.send(message);
|
||||||
const msg = { path: data?.path, key: data?.key, id: data?.id };
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const timer = setTimeout(() => {
|
const timer = setTimeout(() => {
|
||||||
console.log('ws-proxy sendData timeout', msg);
|
console.log('ws-proxy sendData timeout', msg);
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const path = searchParams.get('path');
|
const path = searchParams.get('path');
|
||||||
|
const origin = searchParams.get('origin');
|
||||||
if (!path) {
|
if (!path) {
|
||||||
// 显示前端页面
|
// 显示前端页面
|
||||||
const html = await fetch(`${baseProxyUrl}/root/router-studio/index.html`).then(res => res.text());
|
const html = await fetch(`${baseProxyUrl}/root/router-studio/index.html`).then(res => res.text());
|
||||||
@@ -92,9 +93,31 @@ export const UserV1Proxy = async (req: IncomingMessage, res: ServerResponse, opt
|
|||||||
res.end(JSON.stringify({ message: '应用没有鉴权' }));
|
res.end(JSON.stringify({ message: '应用没有鉴权' }));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// console.log('proxy sendData', { path, key, data, userAppKey });
|
||||||
const value = await client.sendData(message, {
|
const value = await client.sendData(message, {
|
||||||
state: { tokenUser: omit(loginUser.tokenUser, ['oauthExpand']) },
|
state: { tokenUser: omit(loginUser.tokenUser, ['oauthExpand']) },
|
||||||
});
|
});
|
||||||
|
if (value?.code === 200 && value?.data?.base64 && value?.data?.content) {
|
||||||
|
// 如果是文件流,同时没有 origin 参数,说明是查看文件的请求,直接返回文件内容
|
||||||
|
if (!origin) {
|
||||||
|
// 如果是base64数据
|
||||||
|
type Base64Data = {
|
||||||
|
filename: string;
|
||||||
|
base64: boolean;
|
||||||
|
dir?: string;
|
||||||
|
name: string;
|
||||||
|
contentType: string;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
const base64Data = value.data as Base64Data;
|
||||||
|
res.writeHead(200, {
|
||||||
|
'Content-Type': base64Data.contentType || 'application/octet-stream'
|
||||||
|
});
|
||||||
|
const buffer = Buffer.from(base64Data.content, 'base64');
|
||||||
|
res.end(buffer);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (value) {
|
if (value) {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
res.end(JSON.stringify(value));
|
res.end(JSON.stringify(value));
|
||||||
@@ -109,6 +132,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse, opts?: {
|
|||||||
const isGet = req.method === 'GET';
|
const isGet = req.method === 'GET';
|
||||||
// 获取所有的管理员的应用列表
|
// 获取所有的管理员的应用列表
|
||||||
const { ids, infoList } = wsProxyManager.getIdsInfo(user + '--');
|
const { ids, infoList } = wsProxyManager.getIdsInfo(user + '--');
|
||||||
|
const url = new URL(req.url || '', 'http://localhost');
|
||||||
if (isGet) {
|
if (isGet) {
|
||||||
const html = await fetch(`${baseProxyUrl}/root/v1-manager/index.html`).then(res => res.text());
|
const html = await fetch(`${baseProxyUrl}/root/v1-manager/index.html`).then(res => res.text());
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||||
|
|||||||
@@ -308,7 +308,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
username: loginUser?.tokenUser?.username || '',
|
username: loginUser?.tokenUser?.username || '',
|
||||||
password: password,
|
password: password,
|
||||||
});
|
});
|
||||||
console.log('checkPermission', checkPermission, 'loginUser:', loginUser, password)
|
// console.log('checkPermission', checkPermission, 'loginUser:', loginUser, password)
|
||||||
if (!checkPermission.success) {
|
if (!checkPermission.success) {
|
||||||
return createNotFoundPage('no permission');
|
return createNotFoundPage('no permission');
|
||||||
}
|
}
|
||||||
@@ -342,7 +342,7 @@ export const handleRequest = async (req: http.IncomingMessage, res: http.ServerR
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('appFile', appFile, appFileUrl, isExist);
|
// console.log('appFile', appFile, appFileUrl, isExist);
|
||||||
// console.log('isExist', isExist);
|
// console.log('isExist', isExist);
|
||||||
if (!appFile) {
|
if (!appFile) {
|
||||||
const [indexFilePath, etag] = indexFile.split('||');
|
const [indexFilePath, etag] = indexFile.split('||');
|
||||||
|
|||||||
Reference in New Issue
Block a user