简介
由于 IPv4 资源的有限性,目前大部分的本地电脑都是没有公网 IP。所以想要访问自己的本地服务(ssh、http、vnc、NAS、智能家居接口回调 - 比如天猫精灵自定义语义),需要有个服务来进行内网穿透。 目前有一些工具比如花生壳、ngrok 等。但是,
- 花生壳是收费的;
- ngrok 第二版也开始收费(第一版据说有一些严重 bug)。
所以,需要一款开源的方案( 当然还需要一个固定的公网 IP >_<)。
解决方案
本人用 netty 写了一个 tcp 的穿透工具叫 Tcp Through,直接下载可用。安装方式下面有介绍
该项目分 server 端和 client 端,想要看详细信息的话,可以去 github 上查看
- server 端:
https://github.com/longshengwang/tcpthrough-server - client 端:
https://github.com/longshengwang/tcpthrough-client
这个工具有以下一些比较实用的功能
- 支持 http api 管理服务端,还支持 命令行来管理(自己写的 python 库,自己感觉还挺炫)
- 支持安全模式。只有在信任列表的 IP 地址才可以访问 client (防止攻击的最好方式)
- 支持速度限制,功能已经测试可用。只是目前在代码中没有限速,可自己修改代码,具体代码在 server 库中的 OuterServer 中,注释中有说明。
- client 可设定不允许 server 控制 (client 端的 isRemoteManage 参数)
- 支持查看实时速率(不需要用总量来计算)
- 管理通道进行了 SSL 加密,防止注册信息被抓包
- 数据平面和控制平面分离,提高性能(mac 下测速能达到 10Gb/s+)。
- 管理平面和控制平面都进行了安全性校验,不正确的连接会被 kill 掉,拒绝攻击。
- Server 可以增加密码校验,不允许其他的 client 注册。Server 启动时候加上 -s 参数。
备注: 如果是公网虚拟机(阿里云或者腾讯云),记得要开放相应的端口
Server 安装和运行
# 下载并解压 > wget https://github.com/longshengwang/tcpthrough-server/releases/download/v1.0/server-1.0.zip > unzip server-1.0.zip > cd server-1.0 # 启动 server 端,会占用 3 个端口,用途分别是 控制面(默认 9000)、数据面(9009)、http 服务(8080)。可以使用 --help 查看如何设定 > bin/server 注: 详细参数可以通过
bin/server --help来查看
下面的例子是通过 服务器端的 333 端口来访问内网的 localhost 的 22端口
Client 安装和运行
# 下载并解压 > wget https://github.com/longshengwang/tcpthrough-client/releases/download/client-1.0/client-1.0.zip > unzip client-1.0.zip > cd client-1.0 # 启动 client 端, 下面的字段表示:通过服务器上的 333 端口,可以访问客户端的 localhost:22 服务(这里是 ssh 服务) > bin/client -u my_home -s <server ip> -p 333 -l localhost:22 -p 333 -c true -a true 注: 详细参数可以通过
bin/client --help来查看
参数说明:
-c表示 client 是否可以被 server 控制(是否可以增加被代理的服务)-u客户端的名称,也是唯一标识,server 下的 client name 不可以重复-a表示被代理的端口只可以被信任的主机访问 (如何增加被信任的主机,下面的命令行会有提到)-f表示可以把所有的参数都写入文件,然后用 -f 指向该文件 文件模板如下
name=wls_home password=wo_shi_server_password remote_host=192.168.122.20 remote_data_port=9009 remote_manager_port=9000 local_host=192.168.122.20 local_port=22 remote_proxy_port=2222 is_remote_manage=true 命令行
可以通过 python 的一个库(自己写的),来查看和管理 server 上的连接信息
~ pip install tcpth.cmd ~ tcpthcmd # 如果 server 上的 http 端口不是默认的 8080,可以后面加上 -p <port> Welcome to use the tcp through. tcpthrough> help tcpthrough> help list -- get all registration get <name> -- get special name information monitor [<name>] -- monitor the information, refresh on 2s register add <name> <localhost:port> <proxy port> -- add registration register delete <name> <proxy port> -- delete registration trust add <name> [<proxy port>] <trusted ip> -- add trust ip trust delete <name> [<proxy port>] <trusted ip> -- delete trust ip trust get <name> [<proxy port>] -- get trust ip tcpthrough> tcpthrough> list # 在命令行下,列是对齐的,拷贝过来之后就不对齐了 Name | Local Service | Proxy Port | Out Conn Count | Remote Managed | Security | Write Speed | Read Speed -------------------------------------------------------------------------------------------------------------- my_home | localhost:22 | 333 | 0 | true | true | 0KB/s | 0KB/s tcpthrough> 说明:
- 可以通过
exit或者ctrl + d或者ctrl + c退出命令行模式 - 如果客户端允许服务端控制,那么可以通过 register 命令来进行添加和删除
- 如果客户端开启安全模式
-a, 那么只有通过trust add <your-client-name> <your-ip>来添加信任的 IP 地址 - monitor 是可以 每个 2s 打印一次 server 端的连接信息
- register add 中的 localhost 可以是 client 网络中的其他主机的 IP 地址

