天天看点

ngrok重新搭建记录(内网穿透)

1.安装 git

yum -y install git
           

2.安装 go

yum -y install golang
           

3.下载 ngrok 源码

mkdir ~/ngrok
cd ~/ngrok
git clone https://github.com/inconshreveable/ngrok.git
           

4.导入 ngrok 我们配置的域名

假设我们想要给 ngrok 配置的外网域名是:ngrok.test.com

echo 'export NGROK_DOMAIN=ngrok.test.com' >> ~/.bashrc
source ~/.bashrc
           

5.生成自签名ssl证书

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
// 拷贝证书文件到指定位置 
cp rootCA.pem ngrok/assets/client/tls/ngrokroot.crt
cp server.crt ngrok/assets/server/tls/snakeoil.crt
cp server.key ngrok/assets/server/tls/snakeoil.key
           

6.编译安装 ngrok 客户端和服务端

cd ngrok
           

服务端,我们编译64位 linux 版:

GOOS=linux GOARCH=amd64 make release-server
           

客户端,我们编译64位 mac 和 windows 版:

GOOS=darwin GOARCH=amd64 make release-client
GOOS=windows GOARCH=amd64 make release-client
           

从服务器端下载编译好的客户端到本地

scp [email protected]:~/ngrok/ngrok/bin/darwin_amd64/ngork ./
scp [email protected]:~/ngrok/ngrok/bin/windows_amd64/ngork.exe ./
           

7.启动 ngrokd 服务

./bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":8001" -httpsAddr=":8002"
           

8.客户端连接

// 创建 ngrok.conf 配置文件	
vim ngrok.conf
//添加内容
server_addr: "ngrok.test.com:4443"
trust_host_root_certs: false
// 启动 ngrok 客户端,8000 端口(所以我们需要在本地按端口来配置虚拟主机)
ngrok -config=./ngork.conf -subdomain=local 8000
           

9、设置为系统程序,并后台运行。

服务器在运行ngrok时,如果关闭会话窗口,会导致服务中断,很显然这不是我们想要的结果,我们需要服务不断的在后台运行,当需要的时候在停止。

在/etc/systemd/system/目录下创建服务ngrok.service,内容为

[Unit]
Description=ngrok
After=network.target

[Service]
ExecStart=/root/ngrok/ngrok/bin/ngrokd -tlsKey=/root/ngrok/ngrok/bin/server.key -tlsCrt=/root/ngrok/ngrok/bin/server.crt -domain="ngrok.test.com" -httpAddr=":80" -httpsAddr=":443"

[Install]
WantedBy=multi-user.target
           

这样我们就可以了通过

systemctl start ngrok.service

启动服务

10、域名解析

我们一般配置一个 ‘ngrok’ 的二级域名

ngrok.test.com - 指向到我们 ngrokd 所在的服务器地址

还需要配置一个泛域名

*.ngrok.test.com - 也指向到我们 ngrokd 所在的服务器地址

开放端口

ngrokd 默认在服务器上会监听(以下 3 个端口都可以自定义):

4443 - ngrok 客户端监听地址

80 - http 监听地址

443 - https 监听地址