{"id":13826010,"url":"https://github.com/oyyd/nysocks","last_synced_at":"2025-04-15T11:52:17.743Z","repository":{"id":80920334,"uuid":"107007129","full_name":"oyyd/nysocks","owner":"oyyd","description":"Nysocks binds kcp and libuv to provide an aggressive tcp tunnel in nodejs.","archived":false,"fork":false,"pushed_at":"2018-09-27T14:36:31.000Z","size":20272,"stargazers_count":81,"open_issues_count":1,"forks_count":21,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-05T23:31:43.458Z","etag":null,"topics":["kcp","libuv","nodejs","nysocks","proxy","socks5","tunnel"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oyyd.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-10-15T11:42:05.000Z","updated_at":"2023-12-19T22:16:55.000Z","dependencies_parsed_at":"2023-04-12T17:47:45.587Z","dependency_job_id":null,"html_url":"https://github.com/oyyd/nysocks","commit_stats":{"total_commits":222,"total_committers":3,"mean_commits":74.0,"dds":0.009009009009009028,"last_synced_commit":"464c408ea45ceca576f04267bb168a70c5a29847"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fnysocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fnysocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fnysocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oyyd%2Fnysocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oyyd","download_url":"https://codeload.github.com/oyyd/nysocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249067556,"owners_count":21207392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kcp","libuv","nodejs","nysocks","proxy","socks5","tunnel"],"created_at":"2024-08-04T09:01:30.734Z","updated_at":"2025-04-15T11:52:17.723Z","avatar_url":"https://github.com/oyyd.png","language":"C++","funding_links":[],"categories":["C++","C++ (225)","\u003ca id=\"01e6651181d405ecdcd92a452989e7e0\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"9d6789f22a280f5bb6491d1353b02384\"\u003e\u003c/a\u003e隧道\u0026\u0026穿透"],"readme":"# nysocks\n\n[![npm-version](https://img.shields.io/npm/v/nysocks.svg?style=flat-square)](https://www.npmjs.com/package/nysocks) [![travis-ci](https://travis-ci.org/oyyd/nysocks.svg)](https://travis-ci.org/oyyd/nysocks)\n\n[English](./doc/README_EN.md)\n\nNysocks是nodejs上利用[libuv](https://github.com/libuv/libuv)和[kcp](https://github.com/skywind3000/kcp)实现的代理工具。\n\n- 特性\n  - 通过kcp，它在经常发生丢包的网络环境下也能有较好的表现。\n  - 支持[SOCKS5](https://www.ietf.org/rfc/rfc1928.txt)和[SS](https://shadowsocks.org/en/spec/Protocol.html)协议。\n  - 加密传输数据。\n\n对Linode上Tokyo 2, JP机房的测试（经常会发生10%的丢包）：\n\n**一般ss代理:**\n\u003cbr/\u003e\n\u003cimg src=\"https://cdn.rawgit.com/oyyd/nysocks/fa173e5c/imgs/tcp.png\" width=\"475\" /\u003e\n\n**nysocks“fast2”模式:**\n\u003cbr/\u003e\n\u003cimg src=\"https://cdn.rawgit.com/oyyd/nysocks/fa173e5c/imgs/fast2.png\" width=\"475\" /\u003e\n\n## 工作方式\n\n你需要一台可以连接到的服务器来代理网络请求。\n\n**对于SOCKS协议**\n\n![work-socks](https://cdn.rawgit.com/oyyd/nysocks/faae8240/imgs/work.png)\n\n**对于SS协议**\n\n![work-ss](https://cdn.rawgit.com/oyyd/nysocks/dev/imgs/work-ss.png)\n\n目前支持ss协议的客户端较较多，因此你可以直接在服务器上同时启动一个ss协议的client和server来支持这些设备。但直接连接服务器上的client会没办法发挥kcp的优势。\n\n**protocol(unstable):**\n\n**NOTE:** 在nysocks v2.0.0中协议发生了改变，请确保客户端和服务器的版本都在该版本之上.\n\n```\n+-------+-----+-----+---------+---------+--------+------------+\n|  kcp  | ver | cmd |  nonce  | mux.cmd | mux.id | mux.length |\n+-------+-----+-----+---------+---------+--------+------------+\n|  24   |  1  |  1  |    8    |    1    |    2   |     4      |\n+-------+-----+-----+---------+---------+--------+------------+\n```\n\n## 安装\n\n`node \u003e= v6.x`\n\n**请确保[node-gyp](https://github.com/nodejs/node-gyp#installation)可用**。\n\n```\nnpm i nysocks -g\n```\n\n## 使用\n\n#### 1. 创建server\n\n在你的服务器上，执行`server`命令:\n\n```\nnysocks server -p 20000 -k YOUR_PASSWORD -m fast\n```\n\n#### 2 创建client\n\n在你的客户端上，执行`client`命令连接到你的`server`上。之后`client`就可以对本地的其他应用提供代理服务了：\n\n```\nnysocks client -a YOUR_SERVER_HOST -p 20000 -k YOUR_PASSWORD -m fast\n```\n\n执行完上述命令后，当你的`client`显示`SOCKS5 service is listening on 1080`后，你就可以[利用SOCKS5协议](https://github.com/oyyd/nysocks#how-to-utilize-the-socks5-service)来进行代理了。nysocks的client同时会提供一个[PAC](https://en.wikipedia.org/wiki/Proxy_auto-config)（默认端口`8090`）文件服务来帮助你的应用确定哪些请求不需要转发。\n\n**SS协议client**\n\n你可以开启ss协议的client来代替SOCKS5协议：\n\n```\nnysocks client -a YOUR_SERVER_HOST -p 20000 -k YOUR_PASSWORD -m fast --client_protocol SS --ss_password YOUR_SS_PASSWORD --ss_method aes-128-cfb\n```\n\n关于nysocks中ss协议的实现，可以直接参考[encryptsocks](https://github.com/oyyd/encryptsocks) 中ssServer的实现。\n\n#### 3. 用`config.json`文件\n\n类似与ss，你可以将配置数据储存在`config.json`文件中，像[这里](#config)，以避免繁琐的命令行参数：\n\n```\nnysocks client -c config.json\n```\n\n#### 4. 开启守护进程\n\n在实际使用时，你可以利用`-d`选项让client和server运行在守护进程下，以便在发生问题以后自动重启：\n\n```\nnysocks client -d restart -c config.json\n```\n\nnysock使用[pm2](http://pm2.keymetrics.io/)进行进程守护，你也可以通过nysocks命令行直接操作pm2，如查看正在运行的进程：\n\n```\nnysocks pm -- ls\n```\n\n清除所有的进程：\n\n```\nnysocks pm -- kill\n```\n\n#### 5. 查看其他配置项\n\n你可以直接在命令行工具上查看所有可用的配置项:\n\n```\nnysocks -h\n```\n\n## 可用配置\n\n```\nnysocks \u003ccommand\u003e\n\nCommands:\n  nysocks server  Start a tunnel server.\n  nysocks client  Start a tunnel client.\n  nysocks pm      Alias of PM2(process manager) CLI that you can use to check or\n                  operate processes directly.\n\nOptions:\n  --version                Show version number                         [boolean]\n  --config, -c             The path of a json file that describe your\n                           configuration.\n  --daemon, -d             Run with a daemon(pm2): start, stop, restart.\n  --daemon_status, -s      Show daemoned(pm2) processes status\n  --mode, -m               Like kcptun: normal, fast, fast2, fast3.\n  --password, -k           The passowrd/key for the encryption of transmissio.\n  --socket_amount          The amount of connections to be created for each\n                           client (default: 10)\n  --server_addr, -a        The host of your server.\n  --server_port, -p        The port of your server.\n  --client_protocol, --cp  The protocol that will be used by clients: SS, SOCKS\n                           (default: SOCKS)\n  --socks_port             Specify the local port for SOCKS service (default:\n                           1080)\n  --ss_port                Specify the local port for ssServer service (default:\n                           8083)\n  --ss_password            Specify the key for the encryption of ss\n  --ss_method              Specify the method of the encryption for ss (default:\n                           aes-128-cfb)\n  --log_path               The file path for logging. If not set, will log to\n                           the console.\n  --log_memory             Log memory info.\n  --log_conn               Log connections info.\n  --help                   Show help                                   [boolean]\n```\n\n`config.json`的例子:\n\n```json\n{\n  \"serverAddr\": \"YOUR_SERVER_HOST\",\n  \"serverPort\": 20000,\n  \"socketAmount\": 20,\n  \"password\": \"YOUR_PASSWORD\",\n  \"kcp\": {\n    \"sndwnd\": 1024,\n    \"rcvwnd\": 1024,\n    \"nodelay\": 0,\n    \"interval\": 30,\n    \"resend\": 2,\n    \"nc\": 1\n  },\n  \"pac\": {\n    \"pacServerPort\": 8090\n  },\n  \"clientProtocol\": \"SOCKS\",\n  \"SOCKS\": {\n    \"port\": 1080\n  },\n  \"SS\": {\n    \"password\": \"YOUR_SS_PASSWORD\",\n    \"method\": \"aes-128-cfb\",\n    \"serverPort\": 8083,\n  }\n}\n```\n\n### 如何利用SOCKS5服务\n\n大多数操作系统本身支持直接利用SOCKS5进行代理:\n\n![osx-set-proxy](https://cdn.comparitech.com/wp-content/uploads/2017/01/MacOS-Set-proxy.png)\n\nchrome扩展[SwitchyOmega](https://github.com/FelisCatus/SwitchyOmega)可以帮助你的浏览器进行代理。\n\n### 利用ss服务\n\n查看这些[客户端](https://shadowsocks.org/en/download/clients.html)。nysocks中的ss服务相当于一般ss工具中server提供的服务。\n\n### http/https代理\n\n可以参考[hpts](https://github.com/oyyd/http-proxy-to-socks)和其他类似的工具，在SOCKS服务上支持http代理。\n\n## 实现\n\n通过[node-addon(C/CPP)](https://nodejs.org/api/addons.html)来实现（而非纯js）主要是因为:\n\n1. Node在v8.7.0版本之前不支持设置send/recv buffer的大小.\n2. 对于大量的数据传输而言，udp的收发非常频繁，并且我们需要不断修改buffer上的数据。直接在node上进行这类操作所造成的性能消耗对于网络代理工具而言是不可接受的，它会使整体的性能直接下降到不可用的程度。之前纯脚本的实现可以参考这里[here](https://github.com/oyyd/kcp-node/)。\n\n**但addon在部署上相比node脚本而言显得十分困难**，主要有以下几点：\n  1. addon需要编译，编译本身对运行环境有较多要求。对于部分window环境来说，需要额外安装一些依赖。\n  2. prebuild的形式要求开发人员具备在多环境下编译的能力，并能持续稳定跟上node版本的迭代，支持新的node版本（N-API/ABI可能会减轻这一问题）。\n  3. 要在其他node环境上（如electron）上使用addon基本上都需要额外的支持。也更难使用在非Node的js运行环境上（RN，web, chrome-extension）。\n\n## 传输加密\n\n目前默认使用`aes_256_cbc`进行加密，不可配置。\n\n## 已知问题\n\n- 不支持ipv6\n\n## 参考\n\n- [kcptun](https://github.com/xtaci/kcptun) - A Secure Tunnel Based On KCP with N:M Multiplexing\n- [kcp](https://github.com/skywind3000/kcp) - A Fast and Reliable ARQ Protocol\n- [C++ and Node.js Integration](http://scottfrees.com/ebooks/nodecpp/)\n\n## LICENSE\n\nBSD\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foyyd%2Fnysocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foyyd%2Fnysocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foyyd%2Fnysocks/lists"}