ngrok的作用和原理可以网上自行搜索查阅。
环境如下:
服务器 aliyun centos7.2
域名 ithinkcry.cn
客户端 windows 和 树莓派
安装步骤如下:
目录
Go
安装go环境 yum install go
查看go安装情况
[root@jiba ~]# go version
go version go1.11.5 linux/amd64
官方要求go的版本必须是1.1+
Git
下载ngrok源码到服务器上(确保服务器上已经安装git)
[root@jiba zzj]# cd ngrokcode/
[root@jiba ngrokcode]# git clone https://github.com/inconshreveable/ngrok.git
Cloning into 'ngrok'...
remote: Enumerating objects: 2732, done.
remote: Total 2732 (delta 0), reused 0 (delta 0), pack-reused 2732
Receiving objects: 100% (2732/2732), 665.38 KiB | 124.00 KiB/s, done.
Resolving deltas: 100% (1298/1298), done.
OpenSSL
使用openssl 生成ngrok的证书
我的域名是ithinck.cn,我是用的ngrok地址是ngrok.ithinkcry.cn,所以此处domain的地址是ngrok.ithinkcry.cn
[root@jiba ngrok]# pwd
/home/zzj/ngrokcode/ngrok
[root@jiba ngrok]# openssl
OpenSSL> genrsa -out base.key 2048
Generating RSA private key, 2048 bit long modulus
................................................................+++
...........................................+++
e is 65537 (0x10001)
OpenSSL> req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=ngrok.ithinkcry.cn" -out base.pem
OpenSSL> genrsa -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................................................+++
........................+++
e is 65537 (0x10001)
OpenSSL> req -new -key server.key -subj "/CN=ngrok.ithinkcry.cn" -out server.csr
OpenSSL> x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt
Signature ok
subject=/CN=ngrok.ithinkcry.cn
Getting CA Private Key
OpenSSL> exit
查看生成的证书文件 base.key base.pem base.srl server.crt server.csr server.key
[root@jiba ngrok]# pwd
/home/zzj/ngrokcode/ngrok
[root@jiba ngrok]# ll
total 56
drwxr-xr-x 4 root root 4096 Sep 16 10:46 assets
-rw-r--r-- 1 root root 1679 Sep 16 10:58 base.key
-rw-r--r-- 1 root root 1119 Sep 16 11:03 base.pem
-rw-r--r-- 1 root root 17 Sep 16 11:04 base.srl
drwxr-xr-x 2 root root 4096 Sep 16 10:46 contrib
-rw-r--r-- 1 root root 199 Sep 16 10:46 CONTRIBUTORS
drwxr-xr-x 2 root root 4096 Sep 16 10:46 docs
-rw-r--r-- 1 root root 551 Sep 16 10:46 LICENSE
-rw-r--r-- 1 root root 1433 Sep 16 10:46 Makefile
-rw-r--r-- 1 root root 2725 Sep 16 10:46 README.md
-rw-r--r-- 1 root root 1001 Sep 16 11:04 server.crt
-rw-r--r-- 1 root root 903 Sep 16 11:04 server.csr
-rw-r--r-- 1 root root 1679 Sep 16 11:03 server.key
drwxr-xr-x 3 root root 4096 Sep 16 10:46 src
复制生成的证书到assets文件夹下
ngrok通过bindata将ngrok源码目录下的assets目录(资源文件)打包到可执行文件(ngrokd和ngrok)中 去,assets/client/tls和assets/server/tls下分别存放着用于ngrok和ngrokd的默认证书文件,我们需要将它们替换成我们自己生成的:(因此这一步务必放在编译可执行文件之前)
[root@jiba ngrok]# pwd
/home/zzj/ngrokcode/ngrok
[root@jiba ngrok]# cp base.pem assets/client/tls/ngrokroot.crt
cp: overwrite ‘assets/client/tls/ngrokroot.crt’? y
[root@jiba ngrok]# cp server.crt assets/server/tls/snakeoil.crt
cp: overwrite ‘assets/server/tls/snakeoil.crt’? y
[root@jiba ngrok]# cp server.key assets/server/tls/snakeoil.key
cp: overwrite ‘assets/server/tls/snakeoil.key’? y
编译ngrok
服务端:
在ngrok目录下执行如下命令,编译ngrokd:
[root@jiba ngrok]# pwd
/home/zzj/ngrokcode/ngrok
[root@jiba ngrok]# make release-server
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/...
bin/go-bindata -nomemcopy -pkg=assets -tags=release \
-debug=false \
-o=src/ngrok/server/assets/assets_release.go \
assets/server/...
go get -tags 'release' -d -v ngrok/...
github.com/inconshreveable/mousetrap (download)
github.com/rcrowley/go-metrics (download)
Fetching https://gopkg.in/inconshreveable/go-update.v0?go-get=1
Parsing meta tags from https://gopkg.in/inconshreveable/go-update.v0?go-get=1 (status code 200)
get "gopkg.in/inconshreveable/go-update.v0": found meta tag get.metaImport{Prefix:"gopkg.in/inconshreveable/go-update.v0", VCS:"git", RepoRoot:"https://gopkg.in/inconshreveable/go-update.v0"} at https://gopkg.in/inconshreveable/go-update.v0?go-get=1
gopkg.in/inconshreveable/go-update.v0 (download)
github.com/kardianos/osext (download)
github.com/kr/binarydist (download)
Fetching https://gopkg.in/inconshreveable/go-update.v0/check?go-get=1
Parsing meta tags from https://gopkg.in/inconshreveable/go-update.v0/check?go-get=1 (status code 200)
get "gopkg.in/inconshreveable/go-update.v0/check": found meta tag get.metaImport{Prefix:"gopkg.in/inconshreveable/go-update.v0", VCS:"git", RepoRoot:"https://gopkg.in/inconshreveable/go-update.v0"} at https://gopkg.in/inconshreveable/go-update.v0/check?go-get=1
get "gopkg.in/inconshreveable/go-update.v0/check": verifying non-authoritative meta tag
Fetching https://gopkg.in/inconshreveable/go-update.v0?go-get=1
Parsing meta tags from https://gopkg.in/inconshreveable/go-update.v0?go-get=1 (