内网穿透工具(替代 ngrok 和花生壳)

2019 年 11 月 8 日
 lswang

简介

由于 IPv4 资源的有限性,目前大部分的本地电脑都是没有公网 IP。所以想要访问自己的本地服务(ssh、http、vnc、NAS、智能家居接口回调 - 比如天猫精灵自定义语义),需要有个服务来进行内网穿透。 目前有一些工具比如花生壳、ngrok 等。但是,

  1. 花生壳是收费的;
  2. ngrok 第二版也开始收费(第一版据说有一些严重 bug)。

所以,需要一款开源的方案( 当然还需要一个固定的公网 IP >_<)。

解决方案

本人用 netty 写了一个 tcp 的穿透工具叫 Tcp Through,直接下载可用。安装方式下面有介绍

该项目分 server 端和 client 端,想要看详细信息的话,可以去 github 上查看

这个工具有以下一些比较实用的功能

  1. 支持 http api 管理服务端,还支持 命令行来管理(自己写的 python 库,自己感觉还挺炫)
  2. 支持安全模式。只有在信任列表的 IP 地址才可以访问 client (防止攻击的最好方式)
  3. 支持速度限制,功能已经测试可用。只是目前在代码中没有限速,可自己修改代码,具体代码在 server 库中的 OuterServer 中,注释中有说明。
  4. client 可设定不允许 server 控制 (client 端的 isRemoteManage 参数)
  5. 支持查看实时速率(不需要用总量来计算)
  6. 管理通道进行了 SSL 加密,防止注册信息被抓包
  7. 数据平面和控制平面分离,提高性能(mac 下测速能达到 10Gb/s+)。
  8. 管理平面和控制平面都进行了安全性校验,不正确的连接会被 kill 掉,拒绝攻击。
  9. 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 端口来访问内网的 localhost22端口

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 来查看

参数说明:

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>

说明:

21415 次点击
所在节点    程序员
105 条回复
lswang
2019 年 11 月 25 日
@easing 一个 client 可以支持多个转发
lswang
2019 年 11 月 25 日
@wslzy007 是的,没有最好,只有最适合自己
lswang
2019 年 11 月 25 日
@easing 一个 client 可以同时起多个
ChangHaoWei
2019 年 12 月 11 日
@jaybing926 sshd config GateWay yes

。。。没有限制的!
yiheweigui
2024 年 11 月 5 日

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.ih06.com/t/617484

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX