0%

Ubuntu16.04搭建ngrok服务

Ubuntu16.04搭建ngrok服务

安装依赖:

sudo apt-get install build-essential golang mercurial git

如果你的 Ubuntu版本是14.04及以下,apt-get install golang安装的golang是1.2版本的,而ngrok要求golang版本大于1.3,导致编译客户端时报错:

src/github.com/gorilla/websocke... undefined: sync.Pool
make: * [client] Error 2

参考DigitalOcean给的教程手动安装golang:

sudo curl -O https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz
sudo tar -zxvf go1.6.linux-amd64.tar.gz
sudo mv go /usr/local
echo "export PATH=\$PATH:/usr/local/go/bin" >> ~/.profile
source ~/.profile

编译ngrok服务端与客户端

github获取ngrok源码

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

生成并替换源码里默认的ssl证书

cd ngrok
export NGROK_DOMAIN="ngrok.ichenfei.com"
openssl genrsa -out base.key 2048
openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=$NGROK_DOMAIN" -out base.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt

cp base.pem assets/client/tls/ngrokroot.crt

编译服务端:

sudo make release-server

编译客户端

Windows 64位:
sudo GOOS=darwin GOARCH=amd64 make release-client

Windows 32位:
sudo GOOS=darwin GOARCH=386 make release-client

mac osx 64位:
sudo GOOS=darwin GOARCH=amd64 make release-client

linux:
sudo GOOS=linux GOARCH=amd64 make release-client

编译完成后再ngrok/bin目录会出现一个类似于的windows_amd64文件夹,里面有客户端 cd bin/windows_amd64 && sz ngrox 保存到本机

运行ngrok服务:

tmux
sudo ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="ngrok.ichenfei.com" -httpAddr=":8000" -httpsAddr=":8001" -tunnelAddr=":4000"

-domain: 指定域名,需要与生成证书使用的域名一致
-httpAddr: 指定http端口(需要是一个未被占用的端口,可随意指定)
-httpsAddr: 指定https端口
-tunnelAddr ngrok通道的端口号,这个端口是Ngrok用来通信的,所以这个端口在服务器上和客户端上设置必须要对应才可以正常的链接,默认不填写是4443

客户端运行

写一个简单的配置文件,随意命名如 ngrok.cfg

server_addr: ngrok.ichenfei.com:4443
trust_host_root_certs: false

指定子域名、要转发的协议和端口,以及配置文件,运行客户端:

ngrok.exe -config=ngrok.cfg -subdomain=test -proto=http 80

-config 指定配置文件
-proto 指定协议
-subdomain 指定子域名 (需要对域名做泛解析)

没有在服务端指定的端口会随意选择一个端口转发 ngrok会随意选择一个端口对本机的22端口进行转发 如果想一次性转发多个端口或者想指定远程的对应端口,需要完善ngrok.cfg

server_addr: ngrok.domain.com:4000
trust_host_root_certs: false
tunnels:
 ssh:
  remote_port: 1122
  proto:
   tcp: 22
 ss:
  emote_port: 1080
  proto:
   tcp: 1080
 ftp:
  remote_port: 20
  proto:
   tcp: 20
 http:
  subdomain: www
  proto:
   http: 80
   https: 192.168.240.3:8080

启动特定的转发tunnel:

./ngrok -config ngrok.cfg start ssh

当然也可以将所有配置全部转发
./ngrok -config ngrok.cfg start-all

ngrok转发http协议 ngrok转发http协议1 Ngrok内网穿透 由于ngrok可以转发所有tcp协议,所以22,3389,3306等端口也可以通过ngrok转发到公网,使用-proto=tcp指定tcp协议

./ngrok -config=ngrok.cfg -proto=tcp 22

如果不搭建自己的ngrok服务,也可以到ngork.com下载官方提供的ngrok客户端。ngrok官网提供的服务基于ngrok 2.x(未开源),使用github源码搭建的服务器基于ngrok 1.x,用法有很大不同,可以参考给的EXAMPLES使用

EXAMPLES:
    ngrok http 80                    # secure public URL for port 80 web server
    ngrok http -subdomain=baz 8080   # port 8080 available at baz.ngrok.io
    ngrok http foo.dev:80            # tunnel to host:port instead of localhost
    ngrok tcp 22                     # tunnel arbitrary TCP traffic to port 22
    ngrok tls -hostname=foo.com 443  # TLS traffic for foo.com to port 443
    ngrok start foo bar baz          # start tunnels from the configuration file