Centos7搭建Ngrok

来源:互联网 发布:锁机软件下载 编辑:程序博客网 时间:2024/06/05 03:59
搭建环境:阿里云Centos7
1、更新Git(高于1.7即可)
查看当前git版本:git --version
详见另一篇博客《Linux安装最新版git》
2、安装go
CentOS7用户:
yum install -y mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386
其他CentOS用户:
没有Go,只有虚拟机交叉编译,找台CentOS7的服务器编译GO。
检查Go安装成功与否
go version
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备用。

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,执行
./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、如果客户端注册相同的域名,将会失败,提示已被注册。

可用nssm.exe将ngrok注册为服务,开机自启后台运行。详见另一篇博客《nssm注册windows服务》。
有任何问题可到csdn.zxiaofan.com讨论。
参考:
http://www.jianshu.com/p/d0b00d233d09
http://www.cnblogs.com/pwenlee/p/5302880.html

start.sh
0 0