准备工作

  • 安装docker
  • 域名解析

需要在你的域名提供商处增加两条A记录解析到你的服务器,比如ngrok.example.com*.ngrok.example.com

目录结构

  • Dockerfile
FROM golang:1.7.1-alpine
ADD build.sh /
RUN apk add --no-cache git make openssl
RUN git clone https://github.com/inconshreveable/ngrok.git --depth=1 /ngrok
RUN sh /build.sh
EXPOSE 8081
VOLUME [ "/ngrok" ]
CMD [ "/ngrok/bin/ngrokd"]
  • build.sh
export NGROK_DOMAIN="ngrok.lylinux.net"
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 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

make release-server
GOOS=linux GOARCH=386 make release-client
GOOS=linux GOARCH=amd64 make release-client
GOOS=windows GOARCH=386 make release-client
GOOS=windows GOARCH=amd64 make release-client
GOOS=darwin GOARCH=386 make release-client
GOOS=darwin GOARCH=amd64 make release-client
GOOS=linux GOARCH=arm make release-client
  • start.sh

docker run -it  -p 8081:8081 -p 4443:4443 -v /root/docker/ngrok/bin:/root/ngrok/bin/ -d ngrok /ngrok/bin/ngrokd -domain="ngrok.example.com" -httpAddr=":8081"

构建镜像

docker build -t ngrok

启动镜像

./start.sh

客户端生成

上述命令执行完成之后,会输出容器id,比如我的是447314d83bfd。可以使用:docker inspect 447314d83bfd命令来查看其详细信息,另外在Mounts节点可以看到挂载信息,如下图:

在Source目录中的bin目录中可以找到编译出来的二进制客户端文件,找到我们需要执行的客户端对应平台就可以在客户端连接了。

启动客户端

把编译生成ngrok.exe发送到window机器,新建ngrok.cfg,追加下面的内容

 server_addr: "ngrok.example.com:4443"
 trust_host_root_certs: false

使用cmd执行:

ngrok -subdomain="vv" -config="ngrok.cfg" 8080

nginx配置

server {
        server_name ngrok.example.com *.ngrok.example.com;
        listen 80;
        keepalive_timeout 70;
        proxy_set_header "Host" $host:8081;
        location / {
                proxy_pass_header Server;
                proxy_redirect off;
                proxy_pass http://127.0.0.1:8081;
        }
        access_log off;
        log_not_found off;
}

成功

参考资料:
https://www.lylinux.net/artic...
https://www.jianshu.com/p/bac...

03-05 15:02