centos7.2上搭建Ngrok服务 置顶!

  wushaohe

    一、环境准备:

    1.git
    2.go语言环境
    

    若已经安装可以直接看第二步

    二、开始搭建Ngrok服务

    1.下载ngrok源码,命令:

    cd /usr/local/src
    git clone https://github.com/inconshreveable/ngrok.git
    

    2.生成证书

    cd ngrok
    #先在这里把NGROK_DOMAIN改为自己的域名,如我的域名为“wushaohe.xin”
    export NGROK_DOMAIN="wushaohe.xin"
    

    2.1、开始生成证书

    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
    

    2.2、将新生成的证书,替换掉assets/client/tls下的证书

    cp rootCA.pem assets/client/tls/ngrokroot.crt
    cp device.crt assets/server/tls/snakeoil.crt
    cp device.key assets/server/tls/snakeoil.key
    

    3.编译生成ngrokd(服务端)

    #这里是交叉编译,linux系统GOOS=linux,64位系统GOARCH=amd64,32位系统GOARCH=386
    #当前系统可用go env查看
    GOOS=linux GOARCH=amd64
    make release-server
    
    编译成功后在当前目录的bin目录下可找到ngrokd文件
    

    注意: 在编译过程中有可能出现如下情况:

      问题1:
          GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindata
          bin/go-bindata -nomemcopy -pkg=assets -tags=release \
      			  -debug=false \
      			  -o=src/ngrok/client/assets/assets_release.go \
      			  assets/client/...
      
      	  make: bin/go-bindata: Command not found
        make: *** [client-assets] Error 127
       解决方法:前往go安装目录的bin目录下找到go-bindata,将他移动到ngrok/bin下 (没有bin,可新建一个)   
    
    

    问题2:

     package code.google.com/p/log4go: Get https://code.google.com/p/log4go/source/checkout?repo=: dial tcp 216.58.197.110:443: i/o timeout
    
     因为google被墙,如果服务器不在墙外或者没有FQ则无法访问到code.google.com.
    
     解决方法:在 ngrok/src/ngrok/log 目录下找到 logger.go 文件,修改其中第4或5行的:
    
        log "code.google.com/p/log4go”为
    
        log "github.com/keepeye/log4go"  
    

    问题3:

     GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindata
     # github.com/jteeuwen/go-bindata
        src/github.com/jteeuwen/go-bindata/toc.go:47: function ends without a return statement
        make: *** [bin/go-bindata] Error 2
     解决办法: https://github.com/inconshreveable/ngrok/issues/237 
    

    进入到/usr/local/src/ngrok 目录下启动服务端:

    ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="$NGROK_DOMAIN"  -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"
    

    4.编译生成ngrok(客户端)– 本次生成windows下的客户端

    为go生成交叉编译环境,执行如下命令:
        cd /usr/local/go/src
        GOOS=windows GOARCH=amd64 ./make.bash
    
    然后进入ngrok么了执行如下命令生成客户端: 
    	cd /usr/local/src/ngrok
    	GOOS=windows GOARCH=amd64 make release-client
    
    成功后在bin目录下看到windows_amd64文件夹,复制到windows电脑上即可启动
    
    在windows_amd64目录下新建一个ngrok.cfg文件,内容如下:
    
    server_addr: "wushaohe.xin:8083"
    	trusthostroot_certs: false
    	tunnels:
    	  http:
    	    subdomain: "www"
    	    proto:
    	      http: "8080"
    
    	  https:
    	    subdomain: "www"
    	    proto:
    	      https: "8091"
    
    	  ssh:
    	    remote_port: 2222
    	    proto:
    	      tcp: "22"
    
    然后启动客户端,我已经把windows_amd64文件夹下载到D盘下,打开window的CMD输入:
    	cd D:\windows_amd64
    	ngrok.exe -config ngrok.cfg start http https ssh
    
    看到下面信息则启动成功:
    
    Tunnel Status                 online
    	Version                       1.7/1.7
    	Forwarding                    http://www.wushaohe.xin:8081 -> 127.0.0.1:8080
    	Forwarding                    https://www.wushaohe.xin:8082 -> 127.0.0.1:8091
    	Forwarding                    tcp://wushaohe.xin:2222 -> 127.0.0.1:22
    	Web Interface                 127.0.0.1:4040
    	# Conn                        0
    	Avg Conn Time                 0.00ms
    

    此时,在浏览器访问http://www.wushaohe.xin:8081,便能通过ngrok服务映射访问到本地的8080端口了,是不是很神奇!