前言

想在家访问公司的 redmine 等私有服务及自我学习研究

本来 ngrok 自带的就够用了,不过好像最近被河蟹了,索性自己搭一个

要搭建自己的ngrok服务需要一台带有独立IP的服务器

下载&编译

  • 安装依赖

    1
    
    $ sudo yum install golang git golang-pkg-*
  • 下载ngrok

    1
    2
    
    $ git clone git@github.com:inconshreveable/ngrok.git
    $ cd ngrok
  • 根据域名生成自签名

    1
    2
    3
    4
    5
    
    $ openssl genrsa -out ngrokroot.key 2048
    $ openssl req -new -x509 -nodes -key ngrokroot.key -days 10000 -subj "/CN=t.hambut.com" -out ngrokroot.pem
    $ openssl genrsa -out server.key 2048
    $ openssl req -new -key server.key -subj "/CN=t.hambut.com" -out server.csr
    $ openssl x509 -req -in server.csr -CA ngrokroot.pem -CAkey ngrokroot.key -CAcreateserial -days 10000 -out server.crt
  • 会生成6个证书文件

    1
    2
    
    $ ls ngrokroot.* server.*
    ngrokroot.key  ngrokroot.pem  ngrokroot.srl  server.crt  server.csr  server.key
  • 替换原始证书

    1
    2
    3
    
    $ cp ngrokroot.pem assets/client/tls/ngrokroot.crt
    $ cp server.crt assets/server/tls/snakeoil.crt
    $ cp server.key assets/server/tls/snakeoil.key
  • 修改源代码中库引用的错误

由于google code的关闭,所以我们要把作者代码中的库引用地址修改一下 修改 src/ngrok/log/logger.go 文件

1
log "code.google.com/p/log4go" 改为 log "github.com/keepeye/log4go"
  • 开始编译

    1
    2
    3
    
    $ sudo make release-server release-client
    $ ls bin/
    go-bindata  ngrok  ngrokd
  • 交叉编译

    • #### windows
1
2
3
4
$ cd /usr/lib/golang/src/
$ sudo GOOS=windows GOARCH=386 CGO_ENABLED=0 ./make.bash
$ cd -
$ sudo GOOS=windows GOARCH=386 make release-client
  • #### arm
1
2
3
4
$ cd /usr/lib/golang/src/
$ sudo GOOS=linux GOARCH=arm CGO_ENABLED=0 ./make.bash
$ cd -
$ sudo GOOS=linux GOARCH=arm make release-client

配置&使用 ngrok

  • ### 设置域名解析

把域名的A指向到服务器上,如果需要很多2级域名用* 泛解析到服务器上即可

t -> A ip *.t -> A ip

  • ### 启用ngrok 服务端
1
2
3
4
5
6
7
$ cd bin
$ ./ngrokd -domain="t.hambut.com" -httpAddr=":8080" -httpsAddr=":4433" -tunnelAddr=":4443"
[14:59:42 CST 2016/03/04] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
[14:59:42 CST 2016/03/04] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
[14:59:42 CST 2016/03/04] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8080
[14:59:42 CST 2016/03/04] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:4433
[14:59:42 CST 2016/03/04] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443
  • ### 启用ngrok 客户端

ngrok文件复制到客户端机器上给执行权限

1
chmod +x ngrok

创建一个配置文件 config.yml,编辑以下内容并保存

1
2
server_addr: t.hambut.com:4443
trust_host_root_certs: false
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ ./ngrok -config=debug.yml 80

ngrok                                                                                                                                                                                                                       (Ctrl+C to quit)
                                                                                                                                                                                                                                            
Tunnel Status                 online                                                                                                                                                                                                        
Version                       1.7/1.7                                                                                                                                                                                                       
Forwarding                    http://1e5c41a4.t.hambut.com:8080 -> 127.0.0.1:80                                                                                                                                                             
Forwarding                    https://1e5c41a4.t.hambut.com:8080 -> 127.0.0.1:80                                                                                                                                                            
Web Interface                 127.0.0.1:4040                                                                                                                                                                                                
# Conn                        3                                                                                                                                                                                                             
Avg Conn Time                 7.42ms 

文献参考:

https://github.com/inconshreveable/ngrok/blob/master/docs/SELFHOSTING.md http://www.tahoroom.com/archives/11329.html