generated from tailored/router-template
	fix: 添加rewriteCookieDomain
This commit is contained in:
		| @@ -4,7 +4,6 @@ import fs from 'fs'; | ||||
| import { checkFileExists, createDir } from '../file/index.ts'; | ||||
| import { ProxyInfo } from '../proxy/proxy.ts'; | ||||
|  | ||||
| export const kevisualUrl = 'https://kevisual.xiongxiao.me'; | ||||
| const configDir = createDir(path.join(homedir(), '.config/envision')); | ||||
| export const configPath = path.join(configDir, 'assistant-config.json'); | ||||
| export const appConfigPath = path.join(configDir, 'assistant-app-config.json'); | ||||
| @@ -51,8 +50,8 @@ export const setConfig = (config?: AssistantConfig) => { | ||||
|   if (!config) { | ||||
|     return assistantConfig; | ||||
|   } | ||||
|   assistantConfig = config; | ||||
|   fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); | ||||
|   assistantConfig = { ...assistantConfig, ...config }; | ||||
|   fs.writeFileSync(configPath, JSON.stringify(assistantConfig, null, 2)); | ||||
|   return assistantConfig; | ||||
| }; | ||||
| type AppConfig = { | ||||
| @@ -71,9 +70,11 @@ export const getAppConfig = (): AppConfig => { | ||||
|   return JSON.parse(fs.readFileSync(appConfigPath, 'utf8')); | ||||
| }; | ||||
|  | ||||
| export const setAppConfig = (config: AppConfig) => { | ||||
|   fs.writeFileSync(appConfigPath, JSON.stringify(config, null, 2)); | ||||
|   return config; | ||||
| export const setAppConfig = (config: AppConfig) => {   | ||||
|   const _config = getAppConfig(); | ||||
|   const _saveConfig = { ..._config, ...config }; | ||||
|   fs.writeFileSync(appConfigPath, JSON.stringify(_saveConfig, null, 2)); | ||||
|   return _saveConfig; | ||||
| }; | ||||
|  | ||||
| export const addAppConfig = (app: any) => { | ||||
|   | ||||
							
								
								
									
										27
									
								
								assistant-module/src/https/cookie-rewrite.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								assistant-module/src/https/cookie-rewrite.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| export function rewriteCookieDomain(cookie: string, domainRewrite: string | Record<string, string>) { | ||||
|   if (!domainRewrite) return cookie; | ||||
|  | ||||
|   // 解析 Cookie 的属性 | ||||
|   const parts = cookie.split(';').map((part) => part.trim()); | ||||
|   const nameValue = parts[0]; | ||||
|   const attributes = parts.slice(1); | ||||
|  | ||||
|   // 查找并替换 Domain 属性 | ||||
|   const newAttributes = attributes.map((attr) => { | ||||
|     if (attr.startsWith('Domain=')) { | ||||
|       const originalDomain = attr.slice(7); // 去掉 "Domain=" | ||||
|       let newDomain = domainRewrite; | ||||
|  | ||||
|       // 如果 domainRewrite 是对象,根据映射关系替换 | ||||
|       if (typeof domainRewrite === 'object') { | ||||
|         newDomain = domainRewrite[originalDomain] || originalDomain; | ||||
|       } | ||||
|  | ||||
|       return `Domain=${newDomain}`; | ||||
|     } | ||||
|     return attr; | ||||
|   }); | ||||
|  | ||||
|   // 重新组合 Cookie | ||||
|   return [nameValue, ...newAttributes].join('; '); | ||||
| } | ||||
| @@ -1,6 +1,6 @@ | ||||
| import http from 'http'; | ||||
| import https from 'https'; | ||||
|  | ||||
| import { rewriteCookieDomain } from '../https/cookie-rewrite.ts'; | ||||
| import { ProxyInfo } from './proxy.ts'; | ||||
| export const defaultApiProxy = [ | ||||
|   { | ||||
| @@ -29,7 +29,8 @@ export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1', '/ | ||||
| }; | ||||
|  | ||||
| export const apiProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => { | ||||
|   const _u = new URL(req.url, `${proxyApi.target}`); | ||||
|   const { target } = proxyApi; | ||||
|   const _u = new URL(req.url, `${target}`); | ||||
|   console.log('proxyApi', req.url, _u.href); | ||||
|   // 设置代理请求的目标 URL 和请求头 | ||||
|   let header: any = {}; | ||||
| @@ -40,16 +41,16 @@ export const apiProxy = (req: http.IncomingMessage, res: http.ServerResponse, pr | ||||
|   const headers = Object.keys(req.headers).filter((item) => item && item.toLowerCase() !== 'host'); | ||||
|   headers.forEach((item) => { | ||||
|     if (item.toLowerCase() === 'origin') { | ||||
|       header.origin = new URL(proxyApi.target).origin; | ||||
|       header.origin = new URL(target).origin; | ||||
|       return; | ||||
|     } | ||||
|     if (item.toLowerCase() === 'referer') { | ||||
|       header.referer = new URL(req.url, proxyApi.target).href; | ||||
|       header.referer = new URL(req.url, target).href; | ||||
|       return; | ||||
|     } | ||||
|     header[item] = req.headers[item]; | ||||
|   }); | ||||
|   const options = { | ||||
|   const options: http.RequestOptions = { | ||||
|     host: _u.hostname, | ||||
|     path: req.url, | ||||
|     method: req.method, | ||||
| @@ -57,7 +58,7 @@ export const apiProxy = (req: http.IncomingMessage, res: http.ServerResponse, pr | ||||
|       ...header, | ||||
|     }, | ||||
|   }; | ||||
|   console.log('options', JSON.stringify(options, null, 2)); | ||||
|   // console.log('options', JSON.stringify(options, null, 2)); | ||||
|   if (_u.port) { | ||||
|     // @ts-ignore | ||||
|     options.port = _u.port; | ||||
| @@ -65,6 +66,12 @@ export const apiProxy = (req: http.IncomingMessage, res: http.ServerResponse, pr | ||||
|   const httpProxy = _u.protocol === 'https:' ? https : http; | ||||
|   // 创建代理请求 | ||||
|   const proxyReq = httpProxy.request(options, (proxyRes) => { | ||||
|     // Modify the 'set-cookie' headers using rewriteCookieDomain | ||||
|     if (proxyRes.headers['set-cookie']) { | ||||
|       proxyRes.headers['set-cookie'] = proxyRes.headers['set-cookie'].map((cookie) => | ||||
|         rewriteCookieDomain(cookie, 'localhost') | ||||
|       ); | ||||
|     } | ||||
|     // 将代理服务器的响应头和状态码返回给客户端 | ||||
|     res.writeHead(proxyRes.statusCode, proxyRes.headers); | ||||
|     // 将代理响应流写入客户端响应 | ||||
|   | ||||
| @@ -35,7 +35,7 @@ export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, p | ||||
|     if (ext === '.html') { | ||||
|       maxAge = 0; | ||||
|     } | ||||
|     let sendFilePath = filePath.replace(rootPath + '/', ''); | ||||
|     let sendFilePath = path.relative(rootPath, filePath); | ||||
|     const file = send(req, sendFilePath, { | ||||
|       root: rootPath, | ||||
|       maxAge, | ||||
|   | ||||
| @@ -1,6 +1,12 @@ | ||||
| export type ProxyInfo = { | ||||
|   path?: string; | ||||
|   /** | ||||
|    * 目标地址 | ||||
|    */ | ||||
|   target?: string; | ||||
|   /** | ||||
|    * 类型 | ||||
|    */ | ||||
|   type?: 'static' | 'dynamic' | 'minio'; | ||||
|   /** | ||||
|    * 首要文件,比如index.html, 设置了首要文件,如果文件不存在,则访问首要文件 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user