如何给代理加一层 socks5 代理方式

2022-07-23 14:14:07 +08:00
 humbass

使用 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))

5510 次点击
所在节点    Node.js
13 条回复
binux
2022-07-23 14:44:25 +08:00
实现一下 socks5 握手协议呗。
v23x
2022-07-23 15:15:02 +08:00
没有什么比 socks5 更简单的了...

不会超过 10 行代码
humbass
2022-07-23 16:23:42 +08:00
@v23x 有些特定的资源需要 科学, 所以不能直接 SOCKS5 到国外
sujin190
2022-07-23 16:38:31 +08:00
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 需要用户名密码校验的啊
sujin190
2022-07-23 16:41:05 +08:00
python3 -m sevent.helpers.proxy2proxy -p 8088 -T http -P 10.10.10.10:8088

远端时 http 代理也行,只是也不支持添加用户名密码。。
humbass
2022-07-23 17:07:36 +08:00
@sujin190 运行在 electron ,客户的机器上不一定有 python3 ,只能是 nodejs, 另外代理的方式也不对,我是希望通过 http/https/websocket 来做本地与远程的数据通信
shynome
2022-07-23 19:09:29 +08:00
反正 electron 这么大,再加一个二进制 v2ray 也没啥事
humbass
2022-07-23 19:12:31 +08:00
@shynome 有试过加 v2ray ,用 child_process 线程启动,很容易闪退。
ThirdFlame
2022-07-23 20:35:16 +08:00
v2ray 、xray 不就支持多层。
linuxyz
2022-07-24 21:47:40 +08:00
也许可以在 https://gist.github.com/longbill/d321fc371e6dda9acf40decdb923c048 基础上改改。 你要么需要把整个 socks5 交互的报文头传过去,要么自己设计一个小的协议。socks5 本身还是挺简单的,实现一个不支持验证的 Socks5 Server 看上面的 gist 就差不多了。

但是估计你无论如何也需要一个远程的 ProxyServer:

本地访问->本地 socks->本地 websocks->远程 websocks->远程的 ProxyServer->目标网站

否则是无法连目标网站的。
humbass
2022-07-24 21:57:53 +08:00
@linuxyz 感谢回复

以前用过 socks5 ,也是直接连到远程的 socks5 server ,但是某些站点在海外,容易触发防火墙拦截
所以现在改成 http/https 的方式直接

难点是 socks5 如何 跟本地 交互,把 socks5 的包改成 http 的包。没有网络编程经验,难度有点大 。
3825995121
2022-07-26 13:49:58 +08:00
https://github.com/oyyd/http-proxy-to-socks#readme
http-proxy-to-socks 这个 node 包的作用就是监听 http 然后转到 socket5
可以参考一下
humbass
2022-07-26 14:04:31 +08:00
@3825995121 socks5 的代码也容易实现,难的是 如果 socks5 to websocket 并且可以 websocket to socks5

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/868192

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX