你需要:

  1. 一台有公网 IP 的主机
  2. 一个一级域名
  3. 主机上安装有 git

安装步骤:

  1. 安装 go 环境

1、安装go get工具:

    yum install mercurial bzr subversion

2、Centos/Linux下源码安装golang:
    
    wget https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz
    tar -C /usr/local -xzf go1.4.1.linux-amd64.tar.gz
    mkdir $HOME/go
    echo 'export GOROOT=/usr/local/go' >> ~/.bashrc 
    echo 'export GOPATH=$HOME/go' >> ~/.bashrc 
    echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> ~/.bashrc 
    source $HOME/.bashrc 

3、go的命令需要做软连接到/usr/bin

    ln -s /usr/local/go/bin/* /usr/bin/
  1. 编译 ngrok

cd /usr/local/
git clone https://github.com/inconshreveable/ngrok.git
export GOPATH=/usr/local/ngrok/
export NGROK_DOMAIN="域名"
cd ngrok
  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 server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
  1. 把证书文件拷贝到指定位置

cp rootCA.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
  1. 国内的服务器需要改

vim /usr/local/ngrok/src/ngrok/log/logger.go
log "github.com/keepeye/log4go"   
  1. 编译服务端

cd /usr/local/go/src  

GOOS=linux GOARCH=386 ./make.bash(32)
GOOS=linux GOARCH=amd64 ./make.bash(64)    

cd /usr/local/ngrok/

GOOS=linux GOARCH=386 make release-server(32 )
GOOS=linux GOARCH=amd64 make release-server(64位)
  1. 编译客户端

MAC OS 64位操作系统(32位系统把amd64改成386)

cd /usr/local/go/src
GOOS=darwin GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=darwin GOARCH=amd64 make release-client

Windows 64位操作系统(32位系统把amd64改成386)

cd /usr/local/go/src
GOOS=windows GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=windows GOARCH=amd64 make release-client
  1. 客户端配置文件示例

server_addr: "域名:4443"
trust_host_root_certs: false
tunnels:
    ssh:
        remote_port: 23333 (远程端口)
        proto:
            tcp: 22 (本地端口)
    servicename:
        proto:
            http: 23325 (本地端口)
  1. 服务端启动

/usr/local/ngrok/bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":80"
  1. 客户端使用

ngrok -config=ngrok.cfg -subdomain aaa 80 (服务未写入配置文件)

ngrok -config=ngrok.cfg start servicename (服务已写入配置文件)
  1. 错误分析

gopkg.in/inconshreveable/go-update.v0 (download)卡住不动

原因:git版本过低导致

解决办法:卸载自带的git1.7.1,安装git2.2.1并将git添加到环境变量中


#### 参考资料: [ngrok服务器搭建步骤](https://www.jianshu.com/p/b254547b9fe5)