內網穿透工具(替代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 地址
相關文章
相關標籤/搜索