1、准备
一台云服务器(本文ECS),一个域名(二级域名也可以),并且域名正确解析到云服务器
2、安装环境
安装gcc和git(下载ngrok源码)
yum install gcc -y
yum install git -y
yum install -y mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386
yum update -y nss curl libcurl
yum -y install gcc automake autoconf libtool make
设置go环境
cd /usr/local
wget https://dl.google.com/go/go1.11.linux-amd64.tar.gz
tar -zxvf go1.11.linux-amd64.tar.gz
环境变量(vim /etc/profile)
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
export NGROK_DOMAIN=ngrok.your.com
生效
source /etc/profile
3、下载ngrok
返回git的上层目录:cd ..
git clone https://github.com/inconshreveable/ngrok.git#不是所有者使用HTTPS方式或者SSH方式
如果报错:git clone: fatal: Unable to find remote helper for 'https'
由于Git configure配置时没有设定--with-curl --with-expat 造成
CentOS使用yum安装curl开发相关库后重新配置编译git即可:
命令:yum install libcurl-devel 随后cd到git目录make && make install等等...
4、生成自签名证书,
在生成证书时需要一个解析到服务器上的主域名,以"zxfngrok.zxiaofan.com"为例:
cd ngrok
NGROK_DOMAIN="zxfngrok.zxiaofan.com"
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
将新生成的证书,替换掉assets/client/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
5、编译ngrok服务端:
cd到ngrok目录
//指定环境变量位64位linux版本
GOOS=linux GOARCH=amd64 #如果是32位系统,这里GOARCH=386
make release-server
如果报错:-bash: make: command not found
因为安装系统的时候使用的是最小化mini安装,系统没有安装make、vim等常用命令,
直接yum安装即可:yum -y install gcc automake autoconf libtool make
编译会很慢(依赖git),耐心等待
这里会等很久:GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindata
提速方案:翻墙or更改hosts(vim /etc/hosts,maybe需要reboot)
如果报错:fatal: unable to access 'https://github.com/alecthomas/log4go/'
或其他下载失败,多半是由于GFW,重来一次获取就OK了。
正常情况下,在ngrok/bin/下会有ngrokd文件,这就是我们的服务端了。
6、编译客户端(go >= 1.6):
cd到ngrok目录
(1)若是客户端使用与服务器相同系统可直接使用make release-client指令。
(2)若是不同系统,则需要通过go中的make.bash手工编译出跨编译工具。
在$GOROOT/src目录下找到make.bash文件,通过设置GOOS和GOARCH来完成生成跨编译工具:
#linux
make release-client
#windows
GOOS=windows GOARCH=amd64 make release-client
#mac
GOOS=darwin GOARCH=amd64 make release-client
正常情况下,在cd bin/windows_amd64/下,有刚生成的ngrok.exe,下载到windows备用。
编译客户端ngrok(不同系统不同的命令如下)
32位linux客户端: GOOS=linux GOARCH=386 make release-client
64位linux客户端: GOOS=linux GOARCH=amd64 make release-client
32位windows客户端: GOOS=windows GOARCH=386 make release-client
64位windows客户端: GOOS=windows GOARCH=amd64 make release-client
32位mac平台客户端:GOOS=darwin GOARCH=386 make release-client
64位mac平台客户端:GOOS=darwin GOARCH=amd64 make release-client
ARM平台linux客户端: GOOS=linux GOARCH=arm make release-client
7、使用
启动服务端:cd 到服务端目录ngrok/,可以指定证书,域名,端口号来启动:
./bin/ngrokd -tlsKey=device.key -tlsCrt=device.crt -domain="csdnnrgok.zxiaofan.com" -httpAddr=":8234" -httpsAddr=":8334" -tunnelAddr=":443"
如需后台运行,指令改为:nohup./bin/ngrokd -tlsKey=device.key -tlsCrt=device.crt -domain="csdnnrgok.zxiaofan.com" -httpAddr=":8234" -httpsAddr=":8334" -tunnelAddr=":443"&
如果需要开机自启动,参考另一篇博客《CentOS 7将linux服务加入系统启动systemd service》
可新建start.sh放在ngrok目录,欲启动ngrok,执行该脚本即可。
-----
# start.sh
cd /root/ngrok/
nohup bin/ngrokd -tlsKey=device.key -tlsCrt=device.crt -domain=zxfngrok.zxiaofan.com -httpAddr=:8234-httpsAddr=:8334-tunnelAddr=:443 &
-----
如果执行命令后出现>,则可能命令有误,比如域名缺少引号等。
如果出现panic: open device.crt: no such file or directory,则指令有误,修改device.crt路径即可。
出现以下内容则启动成功:
[00:46:10 CST 2016/12/30] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
[00:46:10 CST 2016/12/30] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds
[00:46:10 CST 2016/12/30] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8234
[00:46:10 CST 2016/12/30] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:8334
[00:46:10 CST 2016/12/30] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:443
启动客户端:
注意:server_addr的端口应和服务端启动命令tunnelAddr的一致。
将ngrok.exe放在D:\ngrok,并且在D:\ngrok编辑配置文件ngrok.cfg
server_addr: "zxfngrok.zxiaofan.com:443"
trust_host_root_certs: false
打开windows控制台win+r ,进入D:\ngrok,执行(或做成bat文件)
./ngrok -config=ngrok.cfg -log=ngrok.log -subdomain=test 8080
亦可做其他定义:
server_addr: "zxfngrok.zxiaofan.com:4443"
trust_host_root_certs: false
tunnels:
ssh:
remote_port: 11111
proto:
tcp: "127.0.0.1:22"
mstsc:
remote_port: 22222
proto:
tcp: "127.0.0.1:3389"
web:
subdomain: "test"
proto:
http: 80
远程桌面:ngrok -config=ngrok.cfg start ssh web mstsc
出现以下内容则客户端启动成功:
Tunnel Status online
Version 1.7/1.7
Forwarding http://test.zxfngrok.zxiaofan.com:8234-> 127.0.0.1:80
Forwarding tcp://zxfngrok.zxiaofan.com:11111 -> 127.0.0.1:22
Forwarding tcp://zxfngrok.zxiaofan.com:22222 -> 127.0.0.1:3389
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
注意:
1、客户端ngrok.cfg中server_addr后域名必须严格与-domain以及证书中的NGROK_BASE_DOMAIN相同,
否则Server端就会出现如下错误日志:Failed to read message: remote error: bad certificate
2、如果客户端注册相同的域名,将会失败,提示已被注册。