准备工作
购买阿里云服务器ECS
- 系统:centos 7.6
- 用户:root
- 在阿里云的控制台配置安全组:放开80 443 4443端口
- 域名解析
- n.huangying.site到39.100.89.86
- *.n.huangying.site到39.100.89.86
搭建步骤
# 安装git
git yum install git
# 安装go
wget https://dl.google.com/go/go1.14.15.linux-amd64.tar.gz
# 删除旧版本
rm -rf /usr/local/go
# 解压
tar -zxvf go1.14.15.linux-amd64.tar.gz -C /usr/local
# 配置环境变量 在 /etc/profile后面加入
echo "export GOROOT=/usr/local/go" >> /etc/profile
echo "export PATH=\$PATH:\$GOROOT/bin" >> /etc/profile
# 使环境变量生效
source /etc/profile
# 查看go是否安装正确
go version # 打印: go version go1.14.15 linux/amd64
# go配置国内代理
go env -w GOPROXY=https://goproxy.cn,direct
# go临时配置GO111MODULE=off
go env -w GO111MODULE=off
# export GO111MODULE=off
export NGROK_DOMAIN="n.huangying.site"
git clone https://gitee.com/OtherCopy/ngrok.git
cd ngrok
# 为域名生成证书
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
# copy生成的证书到指定目录,编译需要
\cp -f rootCA.pem assets/client/tls/ngrokroot.crt
\cp -f server.crt assets/server/tls/snakeoil.crt
\cp -f server.key assets/server/tls/snakeoil.key
# 生成客户端配置文件
mkdir bin
echo server_addr: "$NGROK_DOMAIN:4443" > bin/ngrok.cfg
echo trust_host_root_certs: false>> bin/ngrok.cfg
# 采用国内gitee镜像,提升编译速度
git clone -- https://gitee.com/mirrors/log4go.git src/github.com/alecthomas/log4go
git clone -- https://gitee.com/ngrok-install/websocket.git src/github.com/gorilla/websocket
git clone -- https://gitee.com/ngrok-install/go-vhost.git src/github.com/inconshreveable/go-vhost
git clone -- https://gitee.com/ngrok-install/mousetrap.git src/github.com/inconshreveable/mousetrap
git clone -- https://gitee.com/ngrok-install/go-bindata.git src/github.com/jteeuwen/go-bindata
git clone -- https://gitee.com/mirrors_addons/osext.git src/github.com/kardianos/osext
git clone -- https://gitee.com/ngrok-install/binarydist.git src/github.com/kr/binarydist
git clone -- https://gitee.com/GoLibs/go-runewidth.git src/github.com/mattn/go-runewidth
git clone -- https://gitee.com/ngrok-install/termbox-go.git src/github.com/nsf/termbox-go
git clone -- https://gitee.com/mirrors/go-metrics.git src/github.com/rcrowley/go-metrics
#linux server
GOOS=linux GOARCH=amd64 make release-server
#linux client
GOOS=linux GOARCH=amd64 make release-client
#window client
GOOS=windows GOARCH=amd64 make release-client
# 进入bin目录,启动ngrok服务端
./ngrokd -log=stdout -domain="n.huangying.site" -httpAddr=":80"
# 下载客户端和配置文件到自己的电脑上
./ngrok -config=./ngrok.cfg -subdomain=seu 8080
# 注册为服务自启动
# 复制文件 ngrokd 到 /opt/ngrok/下
# 在 /etc/lib/systemd/system下新建文件ngrokd.service(名字一定要叫xxx.service),写入以下内容
[Unit]
Description=ngrokd
After=network.target
[Service]
ExecStart=/opt/ngrok/ngrokd -log=/opt/ngrok/log -domain=n.huangying.site
ExecStop=/usr/bin/killall ngrokd
Restart=always
RestartSec=10
Type=simple
[Install]
WantedBy=multi-user.target
# 重新加载服务
systemctl daemon-reload
# 启动服务
systemctl start ngrokd.service
# 关闭服务
systemctl stop ngrokd.service
# 使服务开机自启动
systemctl enable ngrokd.service
# 查看服务状态
systemctl status ngrokd.servcie
可能遇到的问题
1. 报错:malformed module path "ngrok": missing dot in first path element
修改go环境:go env -w GO111MODULE=off 或者 export GO111MODULE=off
-
报错:go get timeout 超时
用国内代理,go env -w GOPROXY=https://goproxy.cn,direct
3. 运行客户端报错:you may only specify one port to tunnel to on the command line got 4
不要使用PowerShell,使用cmd
4. 连接失败 ,设置阿里云服务器的安全组,打开4443端口
Failed to read message: remote error: tls: bad certificate
5. 客户端连接服务端时,显示签名错误
5.1. 可能是域名写错了
5.2. go版本过高,降低go版本到1.5以下