基於docker搭建ngrok私服

經常開發微信的同學們,肯定會經常用到內網穿透~

大部分人選擇網上尋找各種現成的,比如ngrok官網、natapp、sunny-ngrok或者花生殼之類的。但是世界上沒有免費的午餐,要不就是收費,要不就是免費但是偶爾會出現連接失敗的問題(當然大多數時間是沒有問題的)。

偶然,正在測試微信的某些功能,但是正在使用的ngrok連接失敗了。導致測試無法進行,最終萌生出自己搭建一個ngrok服務器的想法。

閒話少說,直接上乾貨:

備料

要有云服務器一臺;

公網域名一個,可以是一級域名也可以是二級域名,我這裏使用二級域名ngrok.mydomain.com進行舉例。

解析域名

    A記錄 解析ngrok.mydomain.com到服務器ip

    CNAME 解析*.ngrok.mydomain.com 到 ngrok.mydomain.com

準備環境

    雲服務器安裝docker環境, 具體方法見 傳送門, 這裏不展開講

   新建一個文件夾,內建兩個文件 Dockerfile 和 build.sh

    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
ENV DOMAIN=ngrok.mydomain.com
EXPOSE 80
EXPOSE 443
EXPOSE 4443
VOLUME [ "/ngrok" ]
CMD [ "sh", "-c", "/ngrok/bin/ngrokd -domain ${DOMAIN}"]

build.sh 這裏主要是用於生成自建證書,並且編譯出服務器端ngrokd和各個平臺下的客戶端,  生成的文件都存在於容器內的 /ngrok/bin下

export NGROK_DOMAIN="ngrok.mydomain.com"
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

構建並且啓動鏡像

#構建鏡像
docker build -t ngrok .

#啓動鏡像, 爲了不影響服務器上其他的服務,這裏不佔用80和443端口,同樣默認的4443端口也使用了nginx代理
docker run -itd --name=ngrok -p880:80 -p8443:443 -p4444:4443 ngrok

配置訪問代理

安裝nginx後,修改其配置:

修改/etc/nginx/nginx.conf,  在http標籤同級,添加tcp端口代理

#這裏純粹是爲了記錄如何配置tcp代理,如果嫌麻煩,也可以直接在啓動docker容器的時候 直接暴露4443端口
stream {
    server {
        listen 4443;
        proxy_pass localhost:4444;
    }
}

  添加vhost,   centos下:  vi /etc/nginx/conf.d/ngrok.conf

server {
        listen 80;
        listen [::]:80;

        keepalive_timeout 70;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name ngrok.mydomain.com *.ngrok.mydomain.com;
        location / {
            proxy_pass http://localhost:880/;
            proxy_redirect off;
            proxy_connect_timeout 1;
            proxy_send_timeout 120;
            proxy_read_timeout 120;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #千萬不能帶80,後面我會告訴你爲何
            #proxy_set_header Host  $http_host:80;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
}


server {
        listen 443;
        listen [::]:443;

        server_name ngrok.mydomain.com *.ngrok.mydomain.com;

        root /var/www/html;
        index index.html;

        location / {
            proxy_pass https://localhost:8443/;
            proxy_redirect off;
            proxy_connect_timeout 1;
            proxy_send_timeout 120;
            proxy_read_timeout 120;

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #千萬不能帶443,後面我會告訴你爲何
            #proxy_set_header Host  $http_host:443;
            proxy_set_header Host  $http_host;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";
        }
}

檢查nginx的配置項,沒問題即可加載

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# nginx -s reload

訪問 http://ngrok.mydomain.com,提示 tunel not found 即表示服務端已經就緒

配置客戶端

這裏需要特別說明一下:

   構建鏡像的時候,服務器端和客戶端一併生成了,並且客戶端內已經內置了使用的證書,所以千萬要注意: “服務器端和客戶端是一一對應的,一旦服務器端的證書變了,就需要重新取新構建好的客戶端”​​​

通用客戶端並不能連上使用自建證書的服務器端,獲取客戶端的方法如下:

查看docker內文件的所在目錄:

 docker inspect ngrok | grep volume

可以看到在服務器上的目錄是 /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data,

# ls -l /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data
total 64
drwxr-xr-x 4 root root 4096 Jan 31 22:16 assets
drwxr-xr-x 8 root root 4096 Jan 31 22:16 bin
drwxr-xr-x 2 root root 4096 Jan 31 22:16 contrib
-rw-r--r-- 1 root root  199 Jan 31 21:58 CONTRIBUTORS
-rw-r--r-- 1 root root  993 Jan 31 21:58 device.crt
-rw-r--r-- 1 root root  899 Jan 31 21:58 device.csr
-rw-r--r-- 1 root root 1675 Jan 31 21:58 device.key
drwxr-xr-x 2 root root 4096 Jan 31 22:16 docs
-rw-r--r-- 1 root root  551 Jan 31 21:58 LICENSE
-rw-r--r-- 1 root root 1433 Jan 31 21:58 Makefile
drwxr-xr-x 9 root root 4096 Jan 31 22:16 pkg
-rw-r--r-- 1 root root 2725 Jan 31 21:58 README.md
-rw-r--r-- 1 root root 1679 Jan 31 21:58 rootCA.key
-rw-r--r-- 1 root root 1111 Jan 31 21:58 rootCA.pem
-rw-r--r-- 1 root root   17 Jan 31 21:58 rootCA.srl
drwxr-xr-x 5 root root 4096 Jan 31 22:16 src

客戶端就在bin目錄下,選擇對應的端安裝下載到本地即可

# ls -l bin
total 21120
drwxr-xr-x 2 root root     4096 Jan 31 22:16 darwin_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 darwin_amd64
-rwxr-xr-x 1 root root  2655629 Jan 31 21:58 go-bindata
drwxr-xr-x 2 root root     4096 Jan 31 22:16 linux_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 linux_arm
-rwxr-xr-x 1 root root 10775527 Jan 31 21:59 ngrok
-rwxr-xr-x 1 root root  8134110 Jan 31 21:59 ngrokd
drwxr-xr-x 2 root root     4096 Jan 31 22:16 windows_386
drwxr-xr-x 2 root root     4096 Jan 31 22:16 windows_amd64

下載客戶端可以用scp,也可以用sz

#scp方式
scp root@服務器IP:/var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data ./

#sz方式,需要服務器上安裝了lrzsz(支持yum和apt直接安裝)
sz /var/lib/docker/volumes/e9328cb1210de68e221337a47ba8f474515bd301070bb0021943f7815963d70d/_data/bin/windows_amd64/ngrok.exe

在下載到的客戶端同級別的目錄下,新建一個文件 config.cfg,內容如下:

server_addr: "ngrok.mydomain.com:4443"
trust_host_root_certs: false
#這裏面可以直接配置,也可以不配置,啓動的時候再傳入要用的端口
tunnels:
    node:
        proto:
            https: 3000
        subdomain: node
    #http-wx:
    #    proto:
    #        http: 3000
    #    subdomain: wx
    #http:
    #    proto:
    #        http: 80
    #    subdomain: pi
    #https:
    #    proto:
    #        https: 443
    #    subdomain: web
    #ssh:
    #    remote_port: 5000
    #    proto:
    #        tcp: 22

啓動客戶端:

#方法一,啓動預設配置
#ngrok.exe -config=config.cfg -log a.log start-all 


#方法二,啓動臨時端口,自動分配子域名
#ngrok.exe -config=config.fg -proto http 3000

#啓動臨時端口, 指定子域名 node.ngrok.mydomain.com
#ngrok.exe -subdomain node -config=config.fg -proto http 3000

正常情況下,看到這個界面,就已經可以愉快滴玩耍了。

當然也不排除有問題的可能,因爲我也踩了一個大坑:訪問外網地址一直提示 

Tunnel xxx.ngrok.mydomain not found

具體情況見另外一篇帖子(傳送門)。

發佈了20 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章