From d83e62f5953bf15ddf239f4decfa834f10c380f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=9C=8B?= Date: Wed, 23 Oct 2024 18:00:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=8E=BB=E9=99=A4=E8=B5=B0=E5=8A=BF?= =?UTF-8?q?=E5=9B=BE=E7=95=8C=E9=9D=A2=E7=9A=84=E5=B9=BF=E5=91=8A=E5=BC=B9?= =?UTF-8?q?=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/extension.ts | 4 ++ src/webview/proxyService/proxyConfig.ts | 20 ++++++++ src/webview/proxyService/proxyService.ts | 64 ++++++++++++++++++++++++ src/webview/stockTrend.ts | 5 +- src/webview/stockTrendPic.ts | 4 +- 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/webview/proxyService/proxyConfig.ts create mode 100644 src/webview/proxyService/proxyService.ts diff --git a/package.json b/package.json index c407fef9..13c39b6b 100644 --- a/package.json +++ b/package.json @@ -798,6 +798,7 @@ "cheerio": "1.0.0-rc.6", "compare-versions": "^3.6.0", "entities": "2.2.0", + "http-proxy-middleware": "^3.0.3", "iconv-lite": "^0.6.2", "lodash": "4.17.21", "moment": "^2.29.4", diff --git a/src/extension.ts b/src/extension.ts index d165c159..dfb41046 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -29,6 +29,7 @@ import { StatusBar } from './statusbar/statusBar'; import { cacheStocksRemindData } from './webview/leekCenterView'; import { cacheFundAmountData, updateAmount } from './webview/setAmount'; import { cacheStockPriceData, updateStockPrice } from './webview/setStockPrice'; +import { startProxyServer } from './webview/proxyService/proxyService'; let loopTimer: NodeJS.Timer | null = null; let binanceLoopTimer: NodeJS.Timer | null = null; @@ -216,6 +217,9 @@ export function activate(context: ExtensionContext) { // register command registerCommandPaletteEvent(context, statusBar); + // start local proxy server + startProxyServer(); + // Telemetry Event telemetry.sendEvent('activate'); } diff --git a/src/webview/proxyService/proxyConfig.ts b/src/webview/proxyService/proxyConfig.ts new file mode 100644 index 00000000..78c5cede --- /dev/null +++ b/src/webview/proxyService/proxyConfig.ts @@ -0,0 +1,20 @@ +// 定义端口号变量,初始值为 undefined +let PORT: number | undefined = undefined; + +/** + * 设置端口号 + */ +export function setPort(port: number) { + PORT = port; +} + +/** + * 获取代理Host + */ +export function getEastmoneyHost() { + if(PORT === undefined) { + return `https://quote.eastmoney.com`; + } else { + return `http://localhost:${PORT}`; + } +} \ No newline at end of file diff --git a/src/webview/proxyService/proxyService.ts b/src/webview/proxyService/proxyService.ts new file mode 100644 index 00000000..97e0a899 --- /dev/null +++ b/src/webview/proxyService/proxyService.ts @@ -0,0 +1,64 @@ + +import { setPort } from './proxyConfig'; +const http = require('http'); +const { createProxyMiddleware } = require('http-proxy-middleware'); + +// 启动 quote.eastmoney.com 的代理服务器 +export async function startProxyServer() { + const PORT = await findAvailablePort(7100); // 从7100端口开始寻找 + + const server = http.createServer((req: any, res: any) => { + const proxy = createProxyMiddleware({ + target: 'https://quote.eastmoney.com', + changeOrigin: true, + onProxyReq: (proxyReq: { setHeader: (arg0: string, arg1: string) => void; }, req: any) => { + // 设置 User-Agent 和 Cookie + proxyReq.setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'); + // 设置 fullscreengg cookie解决页面频繁弹出广告 + proxyReq.setHeader('Cookie', 'fullscreengg=1'); + }, + onError: (err: any, req: any, res: { writeHead: (arg0: number, arg1: { 'Content-Type': string; }) => void; end: (arg0: string) => void; }) => { + console.error('Proxy error:', err); + res.writeHead(500, { 'Content-Type': 'text/plain' }); + res.end('Something went wrong while proxying the request.'); + }, + }); + proxy(req, res); + }); + + server.listen(PORT, () => { + const address = server.address(); + const port = typeof address === 'string' ? 0 : address?.port; + + if (port) { + setPort(port); // 设置端口号 + console.log(`Proxy server running at http://localhost:${port}`); + } + console.log(`Proxy server running at http://localhost:${PORT}`); + }); +} +/** + * 检测端口是否可用 + */ +function isPortAvailable(port: number): Promise { + return new Promise((resolve) => { + const server = http.createServer(); + server.listen(port, () => { + server.close(() => resolve(true)); + }).on('error', () => resolve(false)); + }); + } + + /** + * 找到未占用的端口 + */ + async function findAvailablePort(startPort = 7100): Promise { + let port = startPort; + while (!(await isPortAvailable(port))) { + port++; + } + return port; + } + + +export default startProxyServer; \ No newline at end of file diff --git a/src/webview/stockTrend.ts b/src/webview/stockTrend.ts index 581a2ce5..00eeb1c8 100644 --- a/src/webview/stockTrend.ts +++ b/src/webview/stockTrend.ts @@ -2,6 +2,7 @@ import { ViewColumn } from 'vscode'; import ReusedWebviewPanel from './ReusedWebviewPanel'; import stockTrendPic from './stockTrendPic'; import globalState from '../globalState'; +import { getEastmoneyHost } from './proxyService/proxyConfig'; function stockTrend(code: string, name: string, stockCode: string) { if (['0dji', '0ixic', '0inx'].includes(code)) { @@ -24,7 +25,7 @@ function stockTrend(code: string, name: string, stockCode: string) { } let mcid = market + '.' + code.substr(1); - let url = `https://quote.eastmoney.com/basic/full.html?mcid=${mcid}`; + let url = `${getEastmoneyHost()}/basic/full.html?mcid=${mcid}`; if (!!globalState.kLineChartSwitch) { if ( @@ -33,7 +34,7 @@ function stockTrend(code: string, name: string, stockCode: string) { stockCode.indexOf('sz399') !== 0 ) { // 沪深股票详情地址可查看盘前盘后指数、买五卖五、筹码分布 - url = `https://quote.eastmoney.com/basic/h5chart-iframe.html?code=${code.substr(1)}&market=${market}`; + url = `${getEastmoneyHost()}/basic/h5chart-iframe.html?code=${code.substr(1)}&market=${market}`; } } diff --git a/src/webview/stockTrendPic.ts b/src/webview/stockTrendPic.ts index cf8777f1..25409765 100644 --- a/src/webview/stockTrendPic.ts +++ b/src/webview/stockTrendPic.ts @@ -1,6 +1,6 @@ import { ViewColumn } from 'vscode'; import ReusedWebviewPanel from './ReusedWebviewPanel'; - +import { getEastmoneyHost } from './proxyService/proxyConfig'; function stockTrendPic(code: string, name: string, stockCode: string) { const panel = ReusedWebviewPanel.create( 'stockTrendPicWebview', @@ -59,7 +59,7 @@ function stockTrendPic(code: string, name: string, stockCode: string) { panel.webview.html = panel.webview.html = `

「${name}」趋势图、K线图

- 网页全屏查看>> + 网页全屏查看>>

实时走势图