之前写了一篇使用docker镜像进行安装ngrok的文章,但docker镜像安装的不带认证,修改起来极其不便。于是就有了这篇文章,centos7安装ngrok
先按照这篇文章:https://blog.csdn.net/sunhuwh/article/details/107971820
将第一步第二步完成
安装gcc和git
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
git clone https://github.com/sunhuwh/ngrok.git
如果报错: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等等…
cd ngrok
NGROK_DOMAIN="ngrok.xxxx.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
vi /etc/ngrok-secrets
修改下方的user和pass即可,上面的#username和password留着
# username password
user pass
# 64位
GOOS=linux GOARCH=amd64 make release-server
# 32位
GOOS=linux GOARCH=386 make release-server
cd ngrok目录
# linux32
GOOS=linux GOARCH=386 make release-client
# linux64
GOOS=linux GOARCH=amd64 make release-client
# win32
GOOS=windows GOARCH=386 make release-client
# win64
GOOS=windows GOARCH=amd64 make release-client
#mac32
GOOS=darwin GOARCH=386 make release-client
#mac64
GOOS=darwin GOARCH=amd64 make release-client
ngrok根目录下
./bin/ngrokd -tlsKey=device.key -tlsCrt=device.crt -domain="ngrok.xxx.com" -httpAddr=":3313" -httpsAddr=":3339" -tunnelAddr=":4443"
目录在ngrok根目录下bin文件夹中
创建配置文件ngrok.cfg
最需要注意的是要将服务端设置的认证,在这里配置好auth_token: user:pass
server_addr: "ngrok.xxxx.com:4443"
trust_host_root_certs: false
auth_token: user:pass
tunnels:
http:
proto:
http: 5432
subdomain: ngrok
tcp12345:
remote_port: 4445
proto:
tcp: 12345
4443这个端口是
5432是代理的本地http端口
12345代理的本地tcp端口
subdomain 二级域名
创建ngrokStart.bat文件
ngrok -config=ngrok.cfg -log=ngrok.log start-all
在这里,tcp的端口很奇怪,我无论指定什么,ngrok服务端那边就会开通什么端口。
怀疑,是因为只要连上4443端口,然后就会将tcp端口注册过去,由ngrok服务端发起TCP连接后,客户端再持续向服务端发送心跳连接,保证不断。