Code Monkey home page Code Monkey logo

nysocks's Introduction

nysocks

npm-version travis-ci

English

Nysocks是nodejs上利用libuvkcp实现的代理工具。

  • 特性
    • 通过kcp,它在经常发生丢包的网络环境下也能有较好的表现。
    • 支持SOCKS5SS协议。
    • 加密传输数据。

对Linode上Tokyo 2, JP机房的测试(经常会发生10%的丢包):

一般ss代理:

nysocks“fast2”模式:

工作方式

你需要一台可以连接到的服务器来代理网络请求。

对于SOCKS协议

work-socks

对于SS协议

work-ss

目前支持ss协议的客户端较较多,因此你可以直接在服务器上同时启动一个ss协议的client和server来支持这些设备。但直接连接服务器上的client会没办法发挥kcp的优势。

protocol(unstable):

NOTE: 在nysocks v2.0.0中协议发生了改变,请确保客户端和服务器的版本都在该版本之上.

+-------+-----+-----+---------+---------+--------+------------+
|  kcp  | ver | cmd |  nonce  | mux.cmd | mux.id | mux.length |
+-------+-----+-----+---------+---------+--------+------------+
|  24   |  1  |  1  |    8    |    1    |    2   |     4      |
+-------+-----+-----+---------+---------+--------+------------+

安装

node >= v6.x

请确保node-gyp可用

npm i nysocks -g

使用

1. 创建server

在你的服务器上,执行server命令:

nysocks server -p 20000 -k YOUR_PASSWORD -m fast

2 创建client

在你的客户端上,执行client命令连接到你的server上。之后client就可以对本地的其他应用提供代理服务了:

nysocks client -a YOUR_SERVER_HOST -p 20000 -k YOUR_PASSWORD -m fast

执行完上述命令后,当你的client显示SOCKS5 service is listening on 1080后,你就可以利用SOCKS5协议来进行代理了。nysocks的client同时会提供一个PAC(默认端口8090)文件服务来帮助你的应用确定哪些请求不需要转发。

SS协议client

你可以开启ss协议的client来代替SOCKS5协议:

nysocks 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

关于nysocks中ss协议的实现,可以直接参考encryptsocks 中ssServer的实现。

3. 用config.json文件

类似与ss,你可以将配置数据储存在config.json文件中,像这里,以避免繁琐的命令行参数:

nysocks client -c config.json

4. 开启守护进程

在实际使用时,你可以利用-d选项让client和server运行在守护进程下,以便在发生问题以后自动重启:

nysocks client -d restart -c config.json

nysock使用pm2进行进程守护,你也可以通过nysocks命令行直接操作pm2,如查看正在运行的进程:

nysocks pm -- ls

清除所有的进程:

nysocks pm -- kill

5. 查看其他配置项

你可以直接在命令行工具上查看所有可用的配置项:

nysocks -h

可用配置

nysocks <command>

Commands:
  nysocks server  Start a tunnel server.
  nysocks client  Start a tunnel client.
  nysocks pm      Alias of PM2(process manager) CLI that you can use to check or
                  operate processes directly.

Options:
  --version                Show version number                         [boolean]
  --config, -c             The path of a json file that describe your
                           configuration.
  --daemon, -d             Run with a daemon(pm2): start, stop, restart.
  --daemon_status, -s      Show daemoned(pm2) processes status
  --mode, -m               Like kcptun: normal, fast, fast2, fast3.
  --password, -k           The passowrd/key for the encryption of transmissio.
  --socket_amount          The amount of connections to be created for each
                           client (default: 10)
  --server_addr, -a        The host of your server.
  --server_port, -p        The port of your server.
  --client_protocol, --cp  The protocol that will be used by clients: SS, SOCKS
                           (default: SOCKS)
  --socks_port             Specify the local port for SOCKS service (default:
                           1080)
  --ss_port                Specify the local port for ssServer service (default:
                           8083)
  --ss_password            Specify the key for the encryption of ss
  --ss_method              Specify the method of the encryption for ss (default:
                           aes-128-cfb)
  --log_path               The file path for logging. If not set, will log to
                           the console.
  --log_memory             Log memory info.
  --log_conn               Log connections info.
  --help                   Show help                                   [boolean]

config.json的例子:

{
  "serverAddr": "YOUR_SERVER_HOST",
  "serverPort": 20000,
  "socketAmount": 20,
  "password": "YOUR_PASSWORD",
  "kcp": {
    "sndwnd": 1024,
    "rcvwnd": 1024,
    "nodelay": 0,
    "interval": 30,
    "resend": 2,
    "nc": 1
  },
  "pac": {
    "pacServerPort": 8090
  },
  "clientProtocol": "SOCKS",
  "SOCKS": {
    "port": 1080
  },
  "SS": {
    "password": "YOUR_SS_PASSWORD",
    "method": "aes-128-cfb",
    "serverPort": 8083,
  }
}

如何利用SOCKS5服务

大多数操作系统本身支持直接利用SOCKS5进行代理:

osx-set-proxy

chrome扩展SwitchyOmega可以帮助你的浏览器进行代理。

利用ss服务

查看这些客户端。nysocks中的ss服务相当于一般ss工具中server提供的服务。

http/https代理

可以参考hpts和其他类似的工具,在SOCKS服务上支持http代理。

实现

通过node-addon(C/CPP)来实现(而非纯js)主要是因为:

  1. Node在v8.7.0版本之前不支持设置send/recv buffer的大小.
  2. 对于大量的数据传输而言,udp的收发非常频繁,并且我们需要不断修改buffer上的数据。直接在node上进行这类操作所造成的性能消耗对于网络代理工具而言是不可接受的,它会使整体的性能直接下降到不可用的程度。之前纯脚本的实现可以参考这里here

但addon在部署上相比node脚本而言显得十分困难,主要有以下几点:

  1. addon需要编译,编译本身对运行环境有较多要求。对于部分window环境来说,需要额外安装一些依赖。
  2. prebuild的形式要求开发人员具备在多环境下编译的能力,并能持续稳定跟上node版本的迭代,支持新的node版本(N-API/ABI可能会减轻这一问题)。
  3. 要在其他node环境上(如electron)上使用addon基本上都需要额外的支持。也更难使用在非Node的js运行环境上(RN,web, chrome-extension)。

传输加密

目前默认使用aes_256_cbc进行加密,不可配置。

已知问题

  • 不支持ipv6

参考

LICENSE

BSD

nysocks's People

Contributors

nizihabi avatar oyyd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nysocks's Issues

npm install -g nysocks遇错,怎么解决?

root@vps:~# npm install -g nysocks
npm WARN registry Unexpected warning for https://registry.npmjs.org/: Miscellaneous Warning EINTEGRITY: sha1-QFUCsAfzGcP0cXXER0UnMA8qta0= integrity checksum failed when using sha1: wanted sha1-QFUCsAfzGcP0cXXER0UnMA8qta0= but got sha512-zr6QQnzLt3Ja0t0XI8gws2kn7zV2p0l/D3kreNvS6hFZhVU5g+uY/30l42jbgt0XGcNBEmBDGJR71J692V92tA==. (260 bytes)
npm WARN registry Using stale package data from https://registry.npmjs.org/ due to a request error during revalidation.
/root/node-v9.5.0-linux-x64/bin/nysocks -> /root/node-v9.5.0-linux-x64/lib/node_modules/nysocks/bin/nysocks

[email protected] install /root/node-v9.5.0-linux-x64/lib/node_modules/nysocks
node-gyp rebuild

sh: 1: node-gyp: Permission denied
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/nysocks/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] install: node-gyp rebuild
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-02-15T05_24_21_444Z-debug.log
root@vps:~#

网络情况正常,但客户端断开后,无法连接上服务端

作者你好,谢谢你的nysocks,效果不错。
只是遇到这问题:一段时间闲置后/客户端机器休眠恢复后,就无法使用了。偶尔服务端和客户端重开后恢复。但大多数情况下,需要更换端口再启动才能恢复。
表现是重启的话到 “PAC service is listening on 8090” 这里就不能继续了,socks5那行出不来。
或者就是一直 “connecting...”

请问我该如何进一步排查问题,找出原因所在?
谢谢 :)

Mux 这个抽象的概念主要设计**

嗨,您好!
最近拜读了您的工程nysocks代码,很佩服。代码中有一些没搞明白,想请教一下。
1.代码中 三个概念,Mux, Sess, Conn。我的理解是 每个客户端一个 Conn 对应,但是Mux怎么理解呢?Sess 是基于 Conn的吗?设计**是什么呢?
nodejs代码 回调太多,我不能理解这三个概念。能说说吗?谢谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.