Nodejs 如何给代理加一层 socks5 代理方式
Nodejs 如何给代理加一层 socks5 代理方式
使用 electron 写的客户端 , 需要用到一个代理的功能,目前是直接使用 http 代理,设置远程的 proxy-server 地址即可。 走的是 http 、https 代理,代码在最下面。
######### ############
本地客户端 -proxy-> proxy server -> 指定网站
######### ############
希望在本地增加一个 socks5
的代理层,数据转换后通过 http\websockets 到 proxy server, 类似这样
######### ########### ############
本地客户端 -socks5-> local proxy -proxy-> proxy server -> 指定网站
######### ########### ############
求赐教!!
服务器端代理的源码
const http = require('http')
const net = require('net')
const port = 1080
function buildHeaders(headers) {
const arr = []
for (const [k, v] of Object.entries(headers)) {
arr.push(`${k}:${v}\r\n`)
}
return arr.join('')
}
function getHostAndPort(req) {
let host
let port
try {
;[host, port] = new URL(req.url)
} catch (e) {
;[host, port] = req.headers.host.split(’:’)
} finally {
if (!port) {
port = 80
}
}
console.log(host, port)
return [host, port]
}
const server = http.createServer((req, res) => {
const [host, port] = getHostAndPort(req)
http.get(
{
port,
host,
path: req.url
},
response => {
response.pipe(res)
}
)
})
server.on(‘upgrade’, (req, res, head) => {
const [host, port] = getHostAndPort(req)
const client = net.connect({
port,
host
})
client.on(‘connect’, () => {
client.write(GET ${req.url}\r\n
+ buildHeaders(req.headers) + ‘\r\n’)
res.pipe(client)
client.pipe(res)
})
client.on(‘error’, () => {
res.destroy()
})
})
server.on(‘connect’, (req, client, head) => {
const [host, port] = getHostAndPort(req)
const socket = net.connect(port, host, () => {
console.log(‘connect’, port, host, head)
client.write(‘HTTP/1.1 200 Connection Established\r\n’ + ‘Proxy-agent: Node.js-Proxy\r\n’ + ‘\r\n’)
socket.write(head)
socket.pipe(client)
client.pipe(socket)
})
})
server.listen(port, () => console.log(port))
实现一下 socks5 握手协议呗。
有些特定的资源需要 科学, 所以不能直接 SOCKS5 到国外
python3 -m sevent.helpers.proxy2proxy -p 8088 -T socks5 -P 10.10.10.10:8088
-p 8088 为本地 socks5 代理端口
-P 10.10.10.10:8088 远程 socks5 服务器
一行命令行解决,但是不支持 socks5 需要用户名密码校验的啊
python3 -m sevent.helpers.proxy2proxy -p 8088 -T http -P 10.10.10.10:8088
远端时 http 代理也行,只是也不支持添加用户名密码。。
运行在 electron ,客户的机器上不一定有 python3 ,只能是 nodejs, 另外代理的方式也不对,我是希望通过 http/https/websocket 来做本地与远程的数据通信
反正 electron 这么大,再加一个二进制 v2ray 也没啥事
有试过加 v2ray ,用 child_process 线程启动,很容易闪退。
v2ray 、xray 不就支持多层。
也许可以在 https://gist.github.com/longbill/d321fc371e6dda9acf40decdb923c048 基础上改改。 你要么需要把整个 socks5 交互的报文头传过去,要么自己设计一个小的协议。socks5 本身还是挺简单的,实现一个不支持验证的 Socks5 Server 看上面的 gist 就差不多了。
但是估计你无论如何也需要一个远程的 ProxyServer:
本地访问->本地 socks->本地 websocks->远程 websocks->远程的 ProxyServer->目标网站
否则是无法连目标网站的。
感谢回复
以前用过 socks5 ,也是直接连到远程的 socks5 server ,但是某些站点在海外,容易触发防火墙拦截
所以现在改成 http/https 的方式直接
难点是 socks5 如何 跟本地 交互,把 socks5 的包改成 http 的包。没有网络编程经验,难度有点大 。
https://github.com/oyyd/http-proxy-to-socks#readme
http-proxy-to-socks 这个 node 包的作用就是监听 http 然后转到 socket5
可以参考一下
socks5 的代码也容易实现,难的是 如果 socks5 to websocket 并且可以 websocket to socks5
在 Node.js 中为代理添加 SOCKS5 代理支持,你可以使用 socks5-http-client
或 socks-proxy-agent
等库。以下是使用 socks-proxy-agent
的示例代码,该库可以方便地将 SOCKS5 代理集成到 HTTP/HTTPS 请求中。
首先,你需要安装 socks-proxy-agent
库:
npm install socks-proxy-agent
然后,你可以使用以下代码来配置 HTTP 和 HTTPS 请求通过 SOCKS5 代理:
const http = require('http');
const https = require('https');
const SocksProxyAgent = require('socks-proxy-agent');
// SOCKS5 代理配置
const proxyConfig = {
host: 'your-socks5-proxy-host',
port: yourSocks5ProxyPort, // 替换为你的 SOCKS5 代理端口
};
// 创建 SOCKS5 代理代理实例
const agent = new SocksProxyAgent(proxyConfig);
// 示例 HTTP 请求
const options = {
hostname: 'example.com',
port: 80,
path: '/',
method: 'GET',
agent: agent,
};
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); });
res.on('end', () => { console.log('No more data in response.'); });
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
req.end();
此代码展示了如何通过 SOCKS5 代理发送 HTTP 请求。类似地,你可以使用 https
模块发送 HTTPS 请求,只需将 http
替换为 https
即可。