0.1.2 • Published 8 years ago
huanying v0.1.2
幻影 -- Node.js网络库代理工具
命令行工具
安装
$ npm install -g huanying
安装成功后会得到两条命令:
- huanying-server 服务器端
- huanying-client 客户端
可在执行命令时加上--help
参数参考使用帮助,比如:
$ huanying-client --help
使用方法
- 先在远程主机启动服务器端:
huanying-server -k hello -p 4040
- 再在本地启动客户端:
huanying-client -k hello -h remote_host -p 4040 16379:6379 13306:3306
说明:
-k
是通讯密钥,必须保证两边一致,否则无法正常通讯-p
是服务器监听端口-h
是远程服务器地址16379:6379
表示将本地端口16379
映射为远程服务器的6379
端口
以上面为例,执行命令redis-cli -p 16379
即可连接到远程服务器的6379
端口的Redis服务器。
使用 PM2 启动
首先执行以下命令得到huanying-server
的绝对路径:
$ echo $(which huanying-server)
# 如果使用的 shell 是 fish,执行 echo (which huanying-server)
# 一般结果为 /usr/local/bin/huanying-server
然后新建文件huanying-server.yaml
:
apps:
- name: huanying-server
script: /usr/local/bin/huanying-server
instance: 1
args: -k hello -p 4040
exec_mode: fork
然后执行以下命令启动即可:
$ pm2 start huanying-server.yaml
注入网络模块
安装
$ npm install huanying --save
使用方法
需要在项目入口文件第一行执行hook()
注入到net
模块,在此之后的所有符合规则的新网络连接将会被代理:
'use strict';
require('huanying').hook({
key: '这里是通讯密钥',
host: '服务器地址',
// 端口
port: 4040,
// 是否打印调试信息
debug: true,
// 被代理的连接地址(仅当地址在以下列表时才代理),支持通配符*
proxyHosts: [ 'www.baidu.com' ],
// 不代理的连接地址(仅当地址不在以下列表时才代理),支持通配符*
ignoreHosts: [ 'www.qq.com' ],
// 自定义筛选函数
filter(host) {
// 返回 true 表示代理,否则不代理
return true;
},
// 说明:proxyHosts, ignoreHosts, filter三选一,如果都不提供则表示所有请求都会被代理
}, client => {
// 初始化完成后会执行此回调函数
// 在此之后的所有网络连接都会被代理
});
授权
MIT License
Copyright (c) 2016 Zongmin Lei <leizongmin@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.