因为IPv4资源的有限性,目前大部分的本地电脑都是没有公网IP。因此想要访问本身的本地服务(ssh、http、vnc、NAS、智能家居接口回调 - 好比天猫精灵自定义语义),须要有个服务来进行内网穿透。
目前有一些工具好比花生壳、ngrok等。可是,python
因此,须要一款开源的方案。git
本人用netty写了一个tcp的穿透工具叫 Tcp Through
,直接下载可用。安装方式下面有介绍github
该项目分server端和client端,想要看详细信息的话,能够去github上查看
server端: https://github.com/longshengwang/tcpthrough-server
client端: https://github.com/longshengwang/tcpthrough-client
api
这个工具备如下一些比较实用的功能安全
备注: 若是是公网虚拟机(阿里云或者腾讯云),记得要开放相应的端口
# 下载并解压 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
端口服务器
# 下载并解压 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
退出命令行模式-a
, 那么只有经过 trust add <your-client-name> <your-ip>
来添加信任的IP地址