使用 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))
补充:
本地访问->本地socks->本地websocks->远程websocks->目标网站
试验过直接用socks,容易被GFW截断,部分指定的站点在海外
|  |      1binux      2022-07-23 14:44:25 +08:00 实现一下 socks5 握手协议呗。 | 
|      2v23x      2022-07-23 15:15:02 +08:00 没有什么比 socks5 更简单的了... 不会超过 10 行代码 | 
|  |      4sujin190      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 需要用户名密码校验的啊 | 
|  |      5sujin190      2022-07-23 16:41:05 +08:00 python3 -m sevent.helpers.proxy2proxy -p 8088 -T http -P 10.10.10.10:8088 远端时 http 代理也行,只是也不支持添加用户名密码。。 | 
|      6humbass OP @sujin190 运行在 electron ,客户的机器上不一定有 python3 ,只能是 nodejs, 另外代理的方式也不对,我是希望通过 http/https/websocket 来做本地与远程的数据通信 | 
|  |      7shynome      2022-07-23 19:09:29 +08:00 via Android 反正 electron 这么大,再加一个二进制 v2ray 也没啥事 | 
|      9ThirdFlame      2022-07-23 20:35:16 +08:00 v2ray 、xray  不就支持多层。 | 
|  |      10linuxyz      2022-07-24 21:47:40 +08:00 也许可以在 https://gist.github.com/longbill/d321fc371e6dda9acf40decdb923c048 基础上改改。 你要么需要把整个 socks5 交互的报文头传过去,要么自己设计一个小的协议。socks5 本身还是挺简单的,实现一个不支持验证的 Socks5 Server 看上面的 gist 就差不多了。 但是估计你无论如何也需要一个远程的 ProxyServer: 本地访问->本地 socks->本地 websocks->远程 websocks->远程的 ProxyServer->目标网站 否则是无法连目标网站的。 | 
|      11humbass OP @linuxyz 感谢回复 以前用过 socks5 ,也是直接连到远程的 socks5 server ,但是某些站点在海外,容易触发防火墙拦截 所以现在改成 http/https 的方式直接 难点是 socks5 如何 跟本地 交互,把 socks5 的包改成 http 的包。没有网络编程经验,难度有点大 。 | 
|      123825995121      2022-07-26 13:49:58 +08:00 https://github.com/oyyd/http-proxy-to-socks#readme   http-proxy-to-socks 这个 node 包的作用就是监听 http 然后转到 socket5 可以参考一下 | 
|      13humbass OP @3825995121 socks5 的代码也容易实现,难的是 如果 socks5 to websocket 并且可以 websocket to socks5 |