一、前提条件
一台有公网ip的服务器,一个域名(二级也行),能正确解析到服务器
二、准备工作
安装依赖
yum -y install gcc git build-essential golang mercurial
三、搭建Ngrok
下载源码、生成证书、替换证书
git clone https://github.com/tutumcloud/ngrok.git ngrok
# 定义ngrok使用的域名
NGROK_DOMAIN="ngrok.bcsytv.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
# 替换证书
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key
编译ngrok服务端
# Linux系统GOOS=linux,64位系统GOARCH=amd64,32位系统GOARCH=386
# 当前系统可用go env查看
GOOS=linux GOARCH=amd64
make release-server
启动服务端
./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="ngrok.bcsytv.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"
注册为服务,建立服务文件 /usr/lib/systemd/system/ngrok.service 将如下代码写入(根据自己的目录进行替换)
[Unit]
Description=Share local port(s) with ngrok
Documentation=https://ngrok.com/docs
After=syslog.target network.target
[Service]
PrivateTmp=true
Type=simple
ExecStart=/root/ngrok/bin/ngrokd -tlsKey=/root/ngrok/assets/server/tls/snakeoil.key -tlsCrt=/root/ngrok/assets/server/tls/snakeoil.crt -domain=ngrok.bcsytv.com -httpAddr=:8081 -httpsAddr=:8082 -tunnelAddr=:8083
ExecStop=/usr/bin/killall ngrok
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
# 设置开机启动
systemctl enable ngrok.service
systemctl start ngrok.service
systemctl stop ngrok.service
systemctl status ngrok.service
编译客户端
# Windows x64
GOOS=windows GOARCH=amd64 make release-client
# mac
GOOS=darwin GOARCH=amd64 make release-client
文章评论