1 介紹
ngrok是內(nèi)網(wǎng)穿透工具挂谍,支持tcp/upd協(xié)議酝惧。
官網(wǎng):https://ngrok.com/
源碼:https://github.com/inconshreveable/ngrok
2 使用官網(wǎng)的域名
從https://ngrok.com/ 下載ngrok ,然后注冊一個賬號并獲取官方分配一個密鑰
密鑰獲取方式:
使用cmd 到ngrok.exe的目錄,命令: ngrok -authtoken 密鑰 -subdomain 二級域名 端口
訪問http://aaa.ngrok.com就可以了值漫。
訪問https://ngrok.com/dashboard 可以管理密鑰澳腹。
不過指定二級域名需要付費,免費的每次客戶端重啟后都會生成新的隨機二級域名杨何。
3 安裝ngrok server
os: Ubuntu 16
## 安裝go
sudo apt-get remove golang-go
sudo mkdir /soft
cd /soft
sudo wget https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.11.2.linux-amd64.tar.gz
echo 'export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export NGROK_DOMAIN="potens.top"' | sudo tee -a /etc/profile
source /etc/profile
## 安裝ngrok
git clone https://github.com/inconshreveable/ngrok.git ngrok
cd ngrok
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
penssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
4 編譯server和client端
#win服務(wù)端
GOOS=windows GOARCH=386 make release-server
#win客戶端
GOOS=windows GOARCH=386 make release-client
#linux服務(wù)端(bin/linux_386/ngrokd)
GOOS=linux GOARCH=386 make release-server
#linux客戶端(路徑 bin/linux_386/ngrok)
GOOS=linux GOARCH=386 make release-client
如果編譯報以下錯酱塔,安裝最新的go和配置GOPATH
package context: unrecognized import path "context" (import path does not begin with hostname)
package net/http/httptrace: unrecognized import path "net/http/httptrace" (import path does not begin with hostname)
Makefile:8: recipe for target 'deps' failed
make: *** [deps] Error 1
編譯成功后在ngrok目錄下的bin目錄。
6 server端啟動
apt install screen
screen -S ngrok
sudo ./ngrokd -domain $NGROK_DOMAIN
# 安裝ctrl+ A+D 就在后臺運行了 ; screen -ls 查看列表; screen -r ngrok 回到對應(yīng)的名稱的前臺
7 客戶端使用
把編譯生成ngrok.exe發(fā)送到window機器危虱,新建ngrok.cfg,追加下面的內(nèi)容
server_addr: "potens.top:4443"
trust_host_root_certs: false
使用cmd執(zhí)行:
ngrok.exe -subdomain hello -proto=http -config=ngrok.cfg 3000
linux客戶端同理羊娃, 只需要把ngrok.exe換成ngrok
執(zhí)行報錯,配置的server_addr的域名需要和上面server中配置的域名一致
#client報錯
Tunnel Status reconnecting
Version 1.7/
Web Interface 127.0.0.1:4040
Conn 0
Avg Conn Time 0.00ms
#server端報錯
[20:33:07 CST 2018/11/19] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [pub:77b2576c] New connection from 185.10.68.26:34410
[20:33:07 CST 2018/11/19] [DEBG] (ngrok/log.(*PrefixLogger).Debug:79) [pub:77b2576c] Found hostname in request
[20:33:07 CST 2018/11/19] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [pub:77b2576c] No tunnel found for hostname
[20:33:07 CST 2018/11/19] [DEBG] (ngrok/log.(*PrefixLogger).Debug:79) [pub:77b2576c] Closing
8 客戶端綁定多種方式
綁定client的3000端口映射到http|https://hello.potens.top
./ngrok -subdomain hello -proto=http -config=ngrok.cfg 3000
綁定client的3000端口映射到tcp://hello.potens.top
./ngrok -subdomain hello -proto=tcp -config=ngrok.cfg 3000
如果想一次性轉(zhuǎn)發(fā)多個端口或者想指定遠(yuǎn)程的對應(yīng)端口埃跷,需要完善ngrok.cfg
server_addr: ngrok.moonwalker.me:4443
trust_host_root_certs: false
tunnels:
ssh:
remote_port: 1122
proto:
tcp: 22
ss:
emote_port: 8388
proto:
tcp: 8388
ftp:
remote_port: 20
proto:
tcp: 20
ftp2:
remote_port: 21
proto:
tcp: 21
http:
subdomain: www
proto:
http: 80
https: 443