Last active
December 10, 2025 07:09
-
-
Save shibeta/1d10bac7cba1ab49a62f53abe2f5ec36 to your computer and use it in GitHub Desktop.
Node.JS 编写的简易 HTTP 代理服务器,用于检查流量是否通过了代理
This file contains hidden or 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
| const http = require("http"); | |
| const net = require("net"); | |
| const url = require("url"); | |
| const proxyPort = 8888; // 你的代理端口 | |
| const server = http.createServer((req, res) => { | |
| // 处理普通 HTTP 请求 | |
| res.writeHead(200, { "Content-Type": "text/plain" }); | |
| res.end("Proxy acts as a tunnel for HTTPS/TCP"); | |
| }); | |
| // 处理 HTTPS/TCP 的 CONNECT 方法 | |
| server.on("connect", (req, clientSocket, head) => { | |
| const { port, hostname } = url.parse(`//${req.url}`, false, true); | |
| console.log(`[Proxy Log] 收到连接请求: ${hostname}:${port}`); | |
| const serverSocket = net.connect(port || 80, hostname, () => { | |
| clientSocket.write( | |
| "HTTP/1.1 200 Connection Established\r\n" + | |
| "Proxy-agent: Node.js-Proxy\r\n" + | |
| "\r\n" | |
| ); | |
| serverSocket.write(head); | |
| serverSocket.pipe(clientSocket); | |
| clientSocket.pipe(serverSocket); | |
| }); | |
| serverSocket.on("error", (err) => { | |
| console.error(`[Proxy Log] 目标连接错误 (${hostname}):`, err.message); | |
| clientSocket.end(); | |
| }); | |
| clientSocket.on("error", (err) => { | |
| console.error(`[Proxy Log] 客户端连接错误:`, err.message); | |
| serverSocket.end(); | |
| }); | |
| }); | |
| server.listen(proxyPort, () => { | |
| console.log(`验证代理服务器已启动,端口: ${proxyPort}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment