因爲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地址