ps: 这边文章,一大半是抄的,一小半是自己踩坑的经验
设备环境:
服务端:ubuntu 16.04
客户端:mac os
阿里云解析
搭建步骤:
- 1. 下载安装go语言,git相关内容,不做赘述,直接上命令:
sudo apt-get install build-essential golang mercurial git
- 2. Clone ngrok代码:
git clone https://github.com/inconshreveable/ngrok.git
- 3. 为base域名生成自签名证书
ngrok需要一个域名作为base域名,ngrok会为客户端分配base域名的子域名。例如:ngrok的base域名为tunnel.simile.com,客户端即可被分配子域名test.tunnel.simile.com。
使用ngrok官方服务时,base域名是ngrok.com,并且使用默认的SSL证书。现在自建ngrok服务器,所以需要重新为自己的base域名生成证书。
# 进入ngrok目录
cd ngrok
#为base域名tunnel.simile.com生成证书
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=tunnel.simile.com" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=tunnel.simile.com" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
需要注意的地方:
1. 一定要注意对应的base域名
2. 报错:ngrok Cannot open file:../crypto/rand/randfile.c:88,解决方法:https://blog.csdn.net/cin_ie/article/details/114141898
执行完上述命令,正常情况下,该目录会多出device.crt、device.csr、device.key、rootCA.key、rootCA.pem、rootCA.srl六个文件,用它们来替换默认的证书文件即可。默认的证书文件在“./assets/client/tls”和“./assets/server/tls/”目录中
#替换默认的证书文件
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key
- 4. 开始编译ngrok
编译服务器端ngrokd
#编译ngrokd(服务器端)
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 main.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 main.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 (status code 200)
Fetching https://gopkg.in/yaml.v1?go-get=1
Parsing meta tags from https://gopkg.in/yaml.v1?go-get=1 (status code 200)
get "gopkg.in/yaml.v1": found meta tag main.metaImport{Prefix:"gopkg.in/yaml.v1", VCS:"git", RepoRoot:"https://gopkg.in/yaml.v1"} at https://gopkg.in/yaml.v1?go-get=1
gopkg.in/yaml.v1 (download)
github.com/inconshreveable/go-vhost (download)
github.com/alecthomas/log4go (download)
github.com/nsf/termbox-go (download)
github.com/mattn/go-runewidth (download)
github.com/gorilla/websocket (download)
go install -tags 'release' ngrok/main/ngrokd
- 5. 写一个服务端启动文件,simile不是simile
vim startup.sh
#进入后增加如下内容
sudo ./bin/ngrokd -domain="tunnel.simile.com" -httpAddr=":80"
- 6. 保存后赋个执行权限
sudo chmod +x startup.sh
- 7. 执行一下试试呗,如果出现如下内容即表示服务端启动成功鸟。。。
➜ ngrok git:(master) ✗ ./startup.sh
[20:56:55 CST 2018/10/29] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
[20:56:55 CST 2018/10/29] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:80
[20:56:55 CST 2018/10/29] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443
[20:56:55 CST 2018/10/29] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443
[20:56:55 CST 2018/10/29] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
- 8. 编译客户端ngrok
注意:这一步还是在ubuntu服务器上操作哦,不是在你的客户端电脑上操作
#在ngrok目录
#编译mac os客户端
GOOS=darwin GOARCH=amd64 make release-client
#编译windows客户端
#GOOS=windows GOARCH=amd64 make release-client
- 9. 配置客户端
- 9.1 执行完上面操作,会在ngrok的bin目录下,生成一个目录darwin_amd64,这里面有个ngrok文件,把这个文件下载到客户端电脑上
- 9.2 在客户端电脑与ngrok的同级目录,建立ngrok配置文件与启动脚本,我的内容如下,仅供参考
by @ BigYellowdeMacBook-Pro in ~/app/ngrok [21:02:44]
$ ls -lh
total 23632
-rwxr-xr-x 1 by staff 11M Oct 29 18:48 ngrok
-rw-r--r-- 1 by staff 102B Oct 29 20:12 ngrok.cfg
-rwxr-xr-x 1 by staff 155B Oct 29 20:35 start.sh
- 9.3 编辑ngrok.cfg, simile不是simile
#配置文件ngrok.cfg的内容
# 4443是ngrok服务端提供的客户端连接端口
# tunnel.simile.com是服务端配置是,输入的domain
# 并且切记:tunnel.simile.com一定要在公网注册,可以解析到, 具体信息参考后面的阿里云解析截图
server_addr: "tunnel.simile.com:4443"
trust_host_root_certs: false
- 9.4 编辑start.sh
# 启动ngrok客户端
# 注意:如果不加参数-subdomain=test,将会随机自动分配子域名。
# 9999是我电脑的web服务端口,通过ngrok映射到test.tunnel.simile.com的80端口上
# test.tunnel.simile.com该域名也需要注册到公网上,看后面的截图
./ngrok -config=ngrok.cfg -subdomain=test 9999
- 9.5 接下来,给start.sh赋个可执行权限,启动它就ok了。
sudo chmod +x start.sh
./start.sh
启动成功会有如下内容, simile不是simile
ngrok (Ctrl+C to quit)
Tunnel Status online
Version 1.7/1.7
Forwarding http://test.tunnel.simile.com -> 127.0.0.1:9999
Forwarding https://test.tunnel.simile.com -> 127.0.0.1:9999
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
备注:阿里云解析截图:
写完了,希望能帮助到大家。
然后,没事别访问上文提到的域名,那个域名我改了之后的,嘿嘿