1 介绍
ngrok是内网穿透工具,支持tcp/upd协议。
官网:https://ngrok.com/
源码:https://github.com/inconshreveable/ngrok
2 使用官网的域名
从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服务端
GOOS=windows GOARCH=386 make release-server
#win客户端
GOOS=windows GOARCH=386 make release-client
#linux服务端(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 回到对应的名称的前台
7 客户端使用
把编译生成ngrok.exe发送到window机器,新建ngrok.cfg,追加下面的内容
server_addr: "potens.top:4443"
trust_host_root_certs: false
使用cmd执行:
ngrok.exe -subdomain hello -proto=http -config=ngrok.cfg 3000
linux客户端同理, 只需要把ngrok.exe换成ngrok
执行报错,配置的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
如果想一次性转发多个端口或者想指定远程的对应端口,需要完善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
网友评论