1、服务端安装
1.1、必要条件
(1)服务器,用来搭建ngrok的服务器,必须有公网ip,并且可以正常访问(本次测试使用的是Ubuntu 16.04 64位)。
(2)域名,用来生成访问域名。
1.2、安装工具和依赖
cd /usr/local
wget https://studygolang.com/dl/golang/go1.11.linux-amd64.tar.gz
tar -zxvf go1.11.linux-amd64.tar.gz
vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOARCH=amd64 # 注意这个坑
source /etc/profile
apt-get install build-essential mercurial git
1.3、下载源码
cd ~
git clone https://github.com/inconshreveable/ngrok.git
1.4、生成证书
注意:NGROK_DOMAIN需要换成自己的域名
cd ngrok
mkdir ssl
cd ssl
export NGROK_DOMAIN="limengshuai.top"
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 device.key 2048
openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
cp rootCA.pem ../assets/client/tls/ngrokroot.crt
cp device.crt ../assets/server/tls/snakeoil.crt
cp device.key ../assets/server/tls/snakeoil.key
1.5、生成服务端和客户端
服务端GOOS=linux GOARCH=amd64 make release-server
window客户端GOOS=windows GOARCH=amd64 make release-client
linux客户端GOOS=linux GOARCH=amd64 make release-client
1.6、启动服务器
nohup ./bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":8000" -httpsAddr=":8443" >log/ngrok.log &
- -httpAddr=”:8000” http服务的访问端口 默认80
- -httpsAddr=”:8443” https服务的访问端口 默认443
- -tunnelAddr=”:4443” 客户端连接服务端的端口 默认4443
注意:上述端口需要通过防火墙
2、客户端使用
2.1、将生成的客户端复制到windows电脑中
2.2、创建一个ngrok.cfg配置文件
在当前目录建一个上述配置文件
server_addr: "limengshuai.top:4443"
trust_host_root_certs: false
tunnels:
ssh:
remote_port: 1122
proto:
tcp: 22
ftp:
remote_port: 20
proto:
tcp: 20
ftp2:
remote_port: 21
proto:
tcp: 21
http:
subdomain: www
proto:
http: 80
https: 443
2.3、启动
启动指定通道
ngrok -config ngrok.cfg start ftp
ngrok -config ngrok.cfg start http
启动所有通道ngrok -config ngrok.cfg start-all
2.4、编写脚本
@echo on
ngrok -config=ngrok.cfg -proto=tcp 192.100.3.22:22
REM ngrok -config=ngrok.cfg -log=ngrok.log -subdomain=yjc 192.100.3.205:8081
REM ngrok -config ngrok.cfg start-all
2.5、帮助
Usage: ngrok.exe [OPTIONS] <local port or address>
Options:
-authtoken string
Authentication token for identifying an ngrok.com account
-config string
Path to ngrok configuration file. (default: $HOME/.ngrok)
-hostname string
Request a custom hostname from the ngrok server. (HTTP only) (requires CNAME of your DNS)
-httpauth string
username:password HTTP basic auth creds protecting the public tunnel endpoint
-log string
Write log messages to this file. 'stdout' and 'none' have special meanings (default "none")
-log-level string
The level of messages to log. One of: DEBUG, INFO, WARNING, ERROR (default "DEBUG")
-proto string
The protocol of the traffic over the tunnel {'http', 'https', 'tcp'} (default: 'http+https') (default "http+https")
-subdomain string
Request a custom subdomain from the ngrok server. (HTTP only)
Examples:
ngrok 80
ngrok -subdomain=example 8080
ngrok -proto=tcp 22
ngrok -hostname="example.com" -httpauth="user:password" 10.0.0.1
Advanced usage: ngrok [OPTIONS] <command> [command args] [...]
Commands:
ngrok start [tunnel] [...] Start tunnels by name from config file
ngork start-all Start all tunnels defined in config file
ngrok list List tunnel names from config file
ngrok help Print help
ngrok version Print ngrok version
Examples:
ngrok start www api blog pubsub
ngrok -log=stdout -config=ngrok.yml start ssh
ngrok start-all
ngrok version