-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
刘朋
committed
Oct 23, 2024
1 parent
919b6cd
commit d83e62f
Showing
6 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<boolean> { | ||
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<number> { | ||
let port = startPort; | ||
while (!(await isPortAvailable(port))) { | ||
port++; | ||
} | ||
return port; | ||
} | ||
|
||
|
||
export default startProxyServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters