centos的基础环境
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
go安装
//请下载合适自己的go语言包
wget https://studygolang.com/dl/golang/go1.18.3.linux-amd64.tar.gz
tar -C /usr/local -xvf go1.18.3.linux-amd64.tar.gz
mkdir /opt/gopath
vim /etc/profile
//添加以下内容:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/opt/gopath
//刷新环境:
source /etc/profile
//检测是否安装成功go
go version
下载ngrok源码包
mkdir /ngrok
cd /ngrok
git clone https://github.com/inconshreveable/ngrok.git
生成证书
cd /ngrok
mkdir cert
cd cert
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=myngork.com" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=myngork.com" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
覆盖原本证书
yes|cp rootCA.pem /ngrok/ngrok/assets/client/tls/ngrokroot.crt
yes|cp device.crt /ngrok/ngrok/assets/server/tls/snakeoil.crt
yes|cp device.key /ngrok/ngrok/assets/server/tls/snakeoil.key
编译生成ngrok
go env //查看环境
GOOS=linux GOARCH=amd64 make release-server
运行测试
./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="myngrok.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"
#参数说明:
#-domain 访问ngrok是所设置的服务地址生成证书时那个
#-httpAddr http协议端口 默认为80
#-httpsAddr https协议端口 默认为443 (可配置https证书)
#-tunnelAddr 通道端口 默认4443
客户端编译和使用
GOOS=windows GOARCH=amd64 make release-client
#编译成功后会在ngrok/bin/下面生成一个windows_amd64目录下面有ngrok.exe
#Linux 平台 32 位系统:GOOS=linux GOARCH=386
#Linux 平台 64 位系统:GOOS=linux GOARCH=amd64
#Windows 平台 32 位系统:GOOS=windows GOARCH=386
#Windows 平台 64 位系统:GOOS=windows GOARCH=amd64
#MAC 平台 32 位系统:GOOS=darwin GOARCH=386
#MAC 平台 64 位系统:GOOS=darwin GOARCH=amd64
#ARM 平台:GOOS=linux GOARCH=arm
Ngrok服务端报错Failed to read message: remote error: tls: bad certificate 处理
生成证书用下面方式,注意替换域名,如果openssl报错,那么建议升级到最新版的ssl:
openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=ikuju.com" -days 5000 -out rootCA.pem
openssl genrsa -out device.key 2048
openssl req -new -key device.key -subj "/CN=ikuju.com" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 10000 -extfile <(printf "subjectAltName=DNS:ikuju.com,DNS:ikuju.com")
然后重新编译服务端和客户端,一般就可以正常连接了