ai_trade_assistance/vite.config.ts
dongzp 1c1f370d59 fix(ws): proxy Baidu WebSocket via Vite to bypass Origin 403
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-22 10:01:23 +08:00

57 lines
1.8 KiB
TypeScript

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
const BAIDU_ORIGIN = 'https://gushitong.baidu.com'
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
server: {
proxy: {
/**
* WebSocket must be registered before `/baidu` so it is not swallowed.
* Baidu rejects non-baidu Origins (localhost → 403); rewrite Origin here.
*/
'/finance-ws': {
target: 'wss://finance-ws.pae.baidu.com',
changeOrigin: true,
ws: true,
rewrite: (path) => path.replace(/^\/finance-ws/, '') || '/',
configure: (proxy) => {
const setBaiduHeaders = (proxyReq: { setHeader: (k: string, v: string) => void }) => {
proxyReq.setHeader('Origin', BAIDU_ORIGIN)
proxyReq.setHeader('Referer', `${BAIDU_ORIGIN}/`)
}
proxy.on('proxyReq', setBaiduHeaders)
proxy.on('proxyReqWs', setBaiduHeaders)
},
},
'/baidu': {
target: 'https://finance.pae.baidu.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/baidu/, ''),
headers: {
Referer: `${BAIDU_ORIGIN}/`,
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
},
},
'/jqka': {
target: 'https://fupage.10jqka.com.cn',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/jqka/, ''),
},
'/deepseek': {
target: 'https://api.deepseek.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/deepseek/, ''),
},
},
},
})