ngrok编译
安装golang
官方golang安装文档:https://golang.google.cn/doc/install
配置国内源
go env -w GOPROXY=https://goproxy.cn,direct
关掉GO111MODULE
go env -w GO111MODULE=off
配置访问github
proxy_host=$1 # 192.168.126.173
proxy_port=$1 # 7890
export https_proxy=http://${proxy_host}:${proxy_port}
export http_proxy=http://${proxy_host}:${proxy_port}
export all_proxy=socks5://${proxy_host}:${proxy_port}
下载源码
git clone https://github.com/inconshreveable/ngrok.git
生成证书
# 设置域名
export NGROK_DOMAIN="new.liuyunuo.cn"
# 生成CA私钥
openssl genrsa -out rootCA.key 2048
# 使用CA私钥,生成CA自签证书
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
# 使用CA证书和CA私钥,给设备证书请求签名,生成设备证书
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
拷贝证书
拷贝证书到ngrok项目中(证书会编译到项目中)
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key
编译make
make release-server release-client
使用
有证书问题
修改源码忽略证书
diff --git a/src/ngrok/client/debug.go b/src/ngrok/client/debug.go
index 749fcf0..83ecbb5 100644
--- a/src/ngrok/client/debug.go
+++ b/src/ngrok/client/debug.go
@@ -1,3 +1,4 @@
+//go:build !release// +build !releasepackage client
diff --git a/src/ngrok/client/release.go b/src/ngrok/client/release.go
index 4c4b117..ba21a19 100644
--- a/src/ngrok/client/release.go
+++ b/src/ngrok/client/release.go
@@ -1,3 +1,4 @@
+//go:build release// +build releasepackage client
diff --git a/src/ngrok/client/tls.go b/src/ngrok/client/tls.go
index 63a707a..f2a305d 100644
--- a/src/ngrok/client/tls.go
+++ b/src/ngrok/client/tls.go
@@ -31,5 +31,5 @@ func LoadTLSConfig(rootCertPaths []string) (*tls.Config, error) {pool.AddCert(certs[0])}- return &tls.Config{RootCAs: pool}, nil
+ return &tls.Config{RootCAs: pool, InsecureSkipVerify: true}, nil}
diff --git a/src/ngrok/client/update_debug.go b/src/ngrok/client/update_debug.go
index e001b6a..064c25c 100644
--- a/src/ngrok/client/update_debug.go
+++ b/src/ngrok/client/update_debug.go
@@ -1,3 +1,4 @@
+//go:build !release && !autoupdate// +build !release,!autoupdatepackage client
diff --git a/src/ngrok/client/update_release.go b/src/ngrok/client/update_release.go
index 2971731..09bc941 100644
--- a/src/ngrok/client/update_release.go
+++ b/src/ngrok/client/update_release.go
@@ -1,3 +1,4 @@
+//go:build release || autoupdate// +build release autoupdatepackage client
diff --git a/src/ngrok/server/tls.go b/src/ngrok/server/tls.go
index e59545b..ada52c0 100644
--- a/src/ngrok/server/tls.go
+++ b/src/ngrok/server/tls.go
@@ -36,6 +36,7 @@ func LoadTLSConfig(crtPath string, keyPath string) (tlsConfig *tls.Config, err e}tlsConfig = &tls.Config{
+ ClientAuth: tls.NoClientCert,Certificates: []tls.Certificate{cert},}
测试
可以工作
编译脚本参考
脚本只是编译,源码需要自己根据git diff 记录修改哦
#!/bin/bashset -e
set -x
CURRENT_PATH=$(cd $(dirname $0) && pwd)# 设置域名
NGROK_DOMAIN="new.liuyunuo.cn"# 设置中国代理
go env -w GOPROXY=https://goproxy.cn,direct# 关闭Module模式
go env -w GO111MODULE=off mkdir -p certs
cd certs# 生成CA私钥
openssl genrsa -out rootCA.key 2048
# 使用CA私钥,生成CA自签证书
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=${NGROK_DOMAIN}" -days 3650 -out rootCA.pem
# 生成设备私钥
openssl genrsa -out device.key 2048
# 使用设备私钥,生成设备证书请求
openssl req -new -key device.key -subj "/CN=${NGROK_DOMAIN}" -out device.csr
# 使用CA证书和CA私钥,给设备证书请求签名,生成设备证书
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 3650cd ${CURRENT_PATH}cp certs/rootCA.pem assets/client/tls/ngrokroot.crt
cp certs/device.crt assets/server/tls/snakeoil.crt
cp certs/device.key assets/server/tls/snakeoil.keyGOOS=linux GOARCH=amd64 make release-server
mkdir -p ngrok_server_linux_amd64
cp ./bin/ngrokd ./ngrok_server_linux_amd64/cat << 'EOF' > "./ngrok_server_linux_amd64/start.sh"
#!/bin/bashscriptPath=$(cd $(dirname $0) && pwd)
cd ${scriptPath}
./ngrokd_wdt.sh 2>&1 >/dev/null &EOFcat << EOF > "./ngrok_server_linux_amd64/ngrokd_wdt.sh"
#!/bin/bashscriptPath=\$(cd \$(dirname \$0) && pwd)
cd \${scriptPath}for ((;;))
doecho "restart ngrokd" >> ngrokd.log./ngrokd -domain="${NGROK_DOMAIN}" -httpAddr=":16080" -httpsAddr=":16443" -tunnelAddr=":4443"sleep 1
doneEOFchmod a+x ./ngrok_server_linux_amd64/*
tar -zcvf ngrok_server_linux_amd64.tgz ngrok_server_linux_amd64GOOS=linux GOARCH=amd64 make release-client
mkdir -p ngrok_client_linux_amd64
cp ./bin/ngrok ./ngrok_client_linux_amd64/cat << 'EOF' > "./ngrok_client_linux_amd64/start.sh"
#!/bin/bashscriptPath=$(cd $(dirname $0) && pwd)
cd ${scriptPath}
./ngrok_wdt.sh 2>&1 >/dev/null &EOFcat << 'EOF' > "./ngrok_client_linux_amd64/ngrok_wdt.sh"
#!/bin/bashscriptPath=$(cd $(dirname $0) && pwd)
cd ${scriptPath}for ((;;))
doecho "restart ngrok" >> ngrok.log./ngrok -config ngrok.cfg -log=stdout -log-level="DEBUG" start sshsleep 1
doneEOFcat << EOF > "./ngrok_client_linux_amd64/ngrok.cfg"
server_addr: "${NGROK_DOMAIN}:4443"
trust_host_root_certs: false
tunnels:ssh:proto:tcp: 22remote_port: 15022EOFchmod a+x ./ngrok_client_linux_amd64/*
tar -zcvf ngrok_client_linux_amd64.tgz ngrok_client_linux_amd64GOOS=linux GOARCH=arm64 make release-client
mkdir -p ngrok_client_linux_arm64
cp ./bin/linux_arm64/ngrok ./ngrok_client_linux_arm64/cat << 'EOF' > "./ngrok_client_linux_arm64/start.sh"
#!/bin/bashscriptPath=$(cd $(dirname $0) && pwd)
cd ${scriptPath}
./ngrok_wdt.sh 2>&1 >/dev/null &EOFcat << 'EOF' > "./ngrok_client_linux_arm64/ngrok_wdt.sh"
#!/bin/bashscriptPath=$(cd $(dirname $0) && pwd)
cd ${scriptPath}for ((;;))
doecho "restart ngrok" >> ngrok.log./ngrok -config ngrok.cfg -log=stdout -log-level="DEBUG" start sshsleep 1
doneEOFcat << EOF > "./ngrok_client_linux_arm64/ngrok.cfg"
server_addr: "${NGROK_DOMAIN}:4443"
trust_host_root_certs: false
tunnels:ssh:proto:tcp: 22remote_port: 15022EOFchmod a+x ./ngrok_client_linux_arm64/*
tar -zcvf ngrok_client_linux_arm64.tgz ngrok_client_linux_arm64