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

简介

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

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

因此,须要一款开源的方案。git

解决方案

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

该项目分server端和client端,想要看详细信息的话,能够去github上查看
server端: https://github.com/longshengwang/tcpthrough-server
client端: https://github.com/longshengwang/tcpthrough-clientapi

这个工具备如下一些比较实用的功能安全

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

参数说明:网络

  • -c 表示client是否能够被 server 控制(是否能够增长被代理的服务)
  • -u 客户端的名称,也是惟一标识,server下的client name不能够重复
  • -a 表示被代理的端口只能够被信任的主机访问 (如何增长被信任的主机,下面的命令行会有提到)
  • -f 表示能够把全部的参数都写入文件,而后用 -f 指向该文件

文件模板以下ssh

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上的链接信息tcp

➜ ~ 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 地址
相关文章
相关标签/搜索