内网穿透 —— ngrok 服务器搭建

你需要:

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

安装步骤:

  1. 安装 go 环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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/
  2. 编译 ngrok

    1
    2
    3
    4
    5
    cd /usr/local/
    git clone https://github.com/inconshreveable/ngrok.git
    export GOPATH=/usr/local/ngrok/
    export NGROK_DOMAIN="域名"
    cd ngrok
  3. 为域名生成证书

    1
    2
    3
    4
    5
    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
  4. 把证书文件拷贝到指定位置

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    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位)
  7. 编译客户端

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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
  8. 客户端配置文件示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server_addr: "域名:4443"
    trust_host_root_certs: false
    tunnels:
    ssh:
    remote_port: 23333 (远程端口)
    proto:
    tcp: 22 (本地端口)
    servicename:
    proto:
    http: 23325 (本地端口)
  9. 服务端启动

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

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

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

    1
    2
    3
    4
    5
    gopkg.in/inconshreveable/go-update.v0 (download)卡住不动

    原因:git版本过低导致

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




参考资料: ngrok服务器搭建步骤

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×