自建ngrok服务

准备

安装GOLANG

GOLANG下载1.4.3(1.4.3在编译时简化很多,推荐使用)版本的安装包
使用tar -C /usr/local -xzf go1.4.3.linux-386.tar.gz安装到/usr/local/go目录下
并添加环境变量

1
2
3
echo 'export GOROOT=/usr/local/go'>> ~/.bashrc
echo 'export PATH=$PATH:$GOROOT/bin'>> ~/.bashrc
source $HOME/.bashrc

输入命令go是否有帮助

域名解析

增加域名解析:

A记录 tunnel.自己的域名 IP地址
A记录 *.tunnel.自己的域名 IP地址

环境配置

1
yum install mercurial git bzr subversion

端口

保证443,4443端口没有被占用

编译安装

下载&&配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd $HOME && git clone https://github.com/inconshreveable/ngrok.git
echo 'export GOPATH=$HOME/ngrok'>> ~/.bashrc
source $HOME/.bashrc

## export NGROK_DOMAIN="tunnel.mydomain.com"
export NGROK_DOMAIN="tunnel.自己的域名"

cd ngrok

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
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key

交叉编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## do_make 没有验证
## 但是流程是这样的
## GOOS支持windows,linux,darwin
## GOARCH支持386,amd64,arm

## cd /usr/local/go/src/
## GOOS=windows GOARCH=amd64 CGO_ENABLED=0 ./make.bash
## cd ~/ngrok
## GOOS=windows GOARCH=amd64 make release-server release-client

function do_make(){
GOOS=$1
GOARCH=$2
cd /usr/local/go/src/
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 ./make.bash
cd ~/ngrok
GOOS=${GOOS} GOARCH=${GOARCH} make release-server release-client
}

do_make linux 386
do_make linux amd64
do_make windows amd64
do_make windows 386
do_make darwin amd64

启动

服务端启动

bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":8000"

客户端启动

编辑ngrok.cfg文件

1
2
3
4
5
6
7
8
9
10
11
12
server_addr: "ngrok.自己的域名:4443"
trust_host_root_certs: false

tunnels:
ssh:
remote_port: 40002
proto:
tcp: ":22"
web:
subdomain: aaa
proto:
http: ":80"

ngrok –config=ngrok.cfg start ssh web

参考文档

  1. http://www.tuicool.com/articles/ZraURrq
  2. http://www.sunnyos.com/article-show-48.html