参考

1、服务端安装

1.1、必要条件

(1)服务器,用来搭建ngrok的服务器,必须有公网ip,并且可以正常访问(本次测试使用的是Ubuntu 16.04 64位)。
(2)域名,用来生成访问域名。

1.2、安装工具和依赖

  1. cd /usr/local
  2. wget https://studygolang.com/dl/golang/go1.11.linux-amd64.tar.gz
  3. tar -zxvf go1.11.linux-amd64.tar.gz
  4. vim /etc/profile
  5. export GOROOT=/usr/local/go
  6. export PATH=$PATH:$GOROOT/bin
  7. export GOARCH=amd64 # 注意这个坑
  8. source /etc/profile
  1. apt-get install build-essential mercurial git

1.3、下载源码

cd ~
git clone https://github.com/inconshreveable/ngrok.git

1.4、生成证书

注意:NGROK_DOMAIN需要换成自己的域名

  1. cd ngrok
  2. mkdir ssl
  3. cd ssl
  4. export NGROK_DOMAIN="limengshuai.top"
  5. openssl genrsa -out rootCA.key 2048
  6. openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
  7. openssl genrsa -out device.key 2048
  8. openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
  9. openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
  10. cp rootCA.pem ../assets/client/tls/ngrokroot.crt
  11. cp device.crt ../assets/server/tls/snakeoil.crt
  12. 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配置文件


在当前目录建一个上述配置文件

  1. server_addr: "limengshuai.top:4443"
  2. trust_host_root_certs: false
  3. tunnels:
  4. ssh:
  5. remote_port: 1122
  6. proto:
  7. tcp: 22
  8. ftp:
  9. remote_port: 20
  10. proto:
  11. tcp: 20
  12. ftp2:
  13. remote_port: 21
  14. proto:
  15. tcp: 21
  16. http:
  17. subdomain: www
  18. proto:
  19. http: 80
  20. https: 443

2.3、启动

启动指定通道
ngrok -config ngrok.cfg start ftp
ngrok -config ngrok.cfg start http

启动所有通道
ngrok -config ngrok.cfg start-all
image.png

2.4、编写脚本

  1. @echo on
  2. ngrok -config=ngrok.cfg -proto=tcp 192.100.3.22:22
  3. REM ngrok -config=ngrok.cfg -log=ngrok.log -subdomain=yjc 192.100.3.205:8081
  4. REM ngrok -config ngrok.cfg start-all

2.5、帮助

  1. Usage: ngrok.exe [OPTIONS] <local port or address>
  2. Options:
  3. -authtoken string
  4. Authentication token for identifying an ngrok.com account
  5. -config string
  6. Path to ngrok configuration file. (default: $HOME/.ngrok)
  7. -hostname string
  8. Request a custom hostname from the ngrok server. (HTTP only) (requires CNAME of your DNS)
  9. -httpauth string
  10. username:password HTTP basic auth creds protecting the public tunnel endpoint
  11. -log string
  12. Write log messages to this file. 'stdout' and 'none' have special meanings (default "none")
  13. -log-level string
  14. The level of messages to log. One of: DEBUG, INFO, WARNING, ERROR (default "DEBUG")
  15. -proto string
  16. The protocol of the traffic over the tunnel {'http', 'https', 'tcp'} (default: 'http+https') (default "http+https")
  17. -subdomain string
  18. Request a custom subdomain from the ngrok server. (HTTP only)
  19. Examples:
  20. ngrok 80
  21. ngrok -subdomain=example 8080
  22. ngrok -proto=tcp 22
  23. ngrok -hostname="example.com" -httpauth="user:password" 10.0.0.1
  24. Advanced usage: ngrok [OPTIONS] <command> [command args] [...]
  25. Commands:
  26. ngrok start [tunnel] [...] Start tunnels by name from config file
  27. ngork start-all Start all tunnels defined in config file
  28. ngrok list List tunnel names from config file
  29. ngrok help Print help
  30. ngrok version Print ngrok version
  31. Examples:
  32. ngrok start www api blog pubsub
  33. ngrok -log=stdout -config=ngrok.yml start ssh
  34. ngrok start-all
  35. ngrok version