概述
阿里云香港服务器centos6.5安装ngrok
一、环境安装
安装
git
# 安装git所需要的依赖包
yum -y install zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker hg wget gcc gcc-c++
# 下载新版本git (版本列表 https://www.kernel.org/pub/software/scm/git/)
cd /usr/local
wget https://www.kernel.org/pub/software/scm/git/git-2.16.1.tar.gz
# 解压
tar zxvf git-2.16.1.tar.gz
# 编译git
cd git-2.16.1
./configure --prefix=/usr/local/git
make
make install
# 创建git的软连接
ln -s /usr/local/git/bin/* /usr/bin/
安装go
# 下载go
wget https://www.golangtc.com/static/go/1.9.2/go1.9.2.linux-amd64.tar.gz
# 解压
tar -zxvf go1.9.2.linux-amd64 /usr/local/
# go的命令需要做软连接到/usr/bin
ln -s /usr/local/go/bin/* /usr/bin/
配置go环境变量
cat > /etc/profile.d/go.sh << EOF
#!/bin/bash
export GOROOT=/usr/local/go
export GOBIN=$GOROOT/bin
export PATH=$PATH:$GOBIN
#(option) export GOPATH=$HOME/go
EOF
# 生效
source /etc/profile
二、 编译ngrok
下载源码
cd /usr/local/
git clone https://github.com/inconshreveable/ngrok.git
环境变量
export GOPATH=/usr/local/ngrok/
export NGROK_DOMAIN="ngrok.xxxxxx.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 server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
拷贝证书覆盖ngrok原来的证书
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
如果是在天朝的服务器需要改,香港或者国外的服务器不需要(未试过)
vim /usr/local/ngrok/src/ngrok/log/logger.go
log "github.com/keepeye/log4go"
编译服务端
cd /usr/local/ngrok/
GOOS=linux GOARCH=amd64 make release-server
go支持交叉编译的平台很多,详见 GO中文文档
编译错误
GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindatabin/go-bindata -nomemcopy -pkg=assets -tags=release
-debug=false
-o=src/ngrok/client/assets/assets_release.go
assets/client/
...make: bin/go-bindata: Command not foundmake: *** [client-assets] Error 127
提示找不到
go-bindata
,需要从
$GOBIN
复制一份,在重新编译
$ make release-server GOOS=linux GOARCH=386
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/...
go install -tags 'release' ngrok/main/ngrokd
go install: cannot install cross-compiled binaries when GOBIN is set
make: *** [server] Error 1
无法交叉编译,需要去掉
$GOBIN
的变量声明
解决以上两个问题
mkdir bin
cp $GOBIN/go-bindata bin/
unset GOBIN
编译客户端
cd /usr/local/ngrok/
GOOS=windows GOARCH=386 make release-client
启动服务器端
Linux x64的服务端位于bin/ngrokd
,因为HTTP服务也是建立在TCP上,所以我一律设置为TCP转发,方便统一管理,另外映射HTTP/HTTPS服务也不要直接占用80/443端口,建议前置一个nginx监听80/443反向代理到ngrok实现端口复用。
/usr/local/ngrok/bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":80"
启动客户端(window)
1.同目录下创建文件ngrok.conf
server_addr: "ngrok.sunnyos.com:4443"
trust_host_root_certs: false
启动
ngrok -config=./ngrok.conf -subdomain=blog 80
ngrok (Ctrl+C to quit)
Tunnel Status online
Version 1.7/1.7
Forwarding http://blog.ngrok.xxxxxx.com -> 127.0.0.1:8080
Forwarding https://blog.ngrok.xxxxxx.com -> 127.0.0.1:8080
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
2.TCP多端口转发
同目录下创建文件ngrok.conf
#证书域名地址
server_addr: "ngrok.xxxxxx.com:4443"
#自行编译无需验证ngrok官网证书
trust_host_root_certs: false
#TCP端口转发
tunnels:
ssh:
remote_port: 10086
proto:
tcp: 8081
http:
remote_port: 10087
proto:
tcp: 8080
运行
ngrok -config=ngrok.conf start ssh http
如下
ngrok (Ctrl+C to quit)
Tunnel Status online
Version 1.7/1.7
Forwarding tcp://ngrok.xxxxxx.com:10086 -> 127.0.0.1:8081
Forwarding tcp://ngrok.xxxxxx.com:10087 -> 127.0.0.1:8080
Web Interface 127.0.0.1:4040
# Conn 0
Avg Conn Time 0.00ms
注意事项
客户端ngrok.cfg中server_addr后的值必须严格与-domain以及证书中的NGROK_BASE_DOMAIN相同,否则Server端就会出现如下错误日志:
New connection from 54.149.100.42:38252
Waiting to read message
Failed to read message: remote error: bad certificate
Closing
参考链接:
https://www.sfantree.com/ngrok-raspberry-cross-nat
https://www.sunnyos.com/article-show-48.html
最后
以上就是默默夕阳为你收集整理的centos 6.5 编译安装ngrok的全部内容,希望文章能够帮你解决centos 6.5 编译安装ngrok所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复