centos7 搭建自己的ngrok服務

可以顺利链接上了,细节问题在于启动服务器的时候的链接点,正确的服务端命令为:

bin/ngrokd -domain="ngrok.coodata.cn" -httpAddr=":80" -httpsAddr=":8081" -tunnelAddr=":4443"
#-domain后面你上面写的域名  -httpAddr http端口  -httpsAddr https 端口 
#-tunnerlAddr 客户端端口,连接端口

客户端的接口要4443,之前就是弄错了接口,导致reconneting,另外也需要在安全组里面把对应的端口打开,还有在防火墙里开发对应的端口

1.前提準備

需要一台有域名解析的雲服務器,此處用的是centos7系統

2.安裝基礎環境

安裝git和gcc(用於下載ngrok源代碼)
yum install gcc -y
yum install git -y

3.安裝go語言環境

yum install -y mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386

4.檢查環境是否安裝好

git --version
go version

5.在雲服務器搭建ngrok服務

下載ngork源代碼
git clone https://github.com/inconshreveable/ngrok.git
生成證書
cd ngrok
#把coodata.cn改為自己的域名
export NGROK_DOMAIN="ngrok.coodata.cn"
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=ngrok.coodata.cn" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=ngrok.coodata.cn" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
替換新生成的證書,執行下面命令後輸入’y’ 回車
cp rootCA.pem assets/client/tls/ngrokroot.crt

cp device.crt assets/server/tls/snakeoil.crt

cp device.key assets/server/tls/snakeoil.key

6.編譯生成ngrokd(服務端)

#輸入後需要等待一段時間
GOOS=linux GOARCH=amd64 make release-server
#生成在 ~/ngrok/bin/目錄中

7.編譯生成ngrok(客戶端)

windows客户端
#還是在ngrok這個目錄,輸入以下命令,然後等待完成
GOOS=windows GOARCH=amd64 make release-client
#生成的目錄在 ~/ngrok/bin/windows_amd64目錄中
mac客户端
GOOS=darwin GOARCH=amd64 make release-server release-client
linux客户端
GOOS=linux GOARCH=amd64 make release-server release-client
#如果无效,可能是环境配置问题,centos系统可以用一开始生成的ngrok做客户端

8.用SSH Secure Shell Client工具(或scp等)

windows下載

將~/ngrok/bin/windows_amd64/中的文件下載到本地windows下,如D:\ngrok

mac下載
#由於文件不大,用了scp,如果說找不到文件位置,把後面的~去掉即可
scp root@192.168.3.121:~/ngrok/bin/windwos_amd64/   ngrok.exe ~/User/用戶名/ngrok.exe

spc語法
也可以使用iTerm 配置更加方便下載

9.在D:/ngrok中新建文件,改名為ngrok.cfg

server_addr: "ngrok.coodata.cn:4443"
trust_host_root_certs: false
tunnels:
    http:
        subdomain: "www"
        proto:
            http: "80"

    https:
        subdomain: "www"
        proto:
            https: "443"

    ssh:
        remote_port: 2222
        proto:
            tcp: "22"

    mstsc:
            remote_port: 52222      
            proto:
            tcp: "192.168.0.107:3389"

10.開啟遠程服務

#如果開啟不了,很可能是和nginx衝突了,關閉掉即可
cd ngrok
sbin/ngrokd -domain="ngrok.coodata.cn" -httpAddr=":80" -httpsAddr=":8081" -tunnelAddr=":4443" &

11.開啟客戶機服務

windows 启动
#cd到目錄後,如果online,则成功,如果出现reconnecting的问题,多数是接口不对,或者安全组和防火墙
ngrok.exe -config ngrok.cfg -subdmain test 3000
mac启动
./ngrok -config=ngrok.cfg -subdomain=webname 3000