目录
前言
ngrok 编译服务器端、客户端,然后搭建内网映射
一、搭建环境
Ubuntu18.04 公网ip 域名 golang git
golang和git的安装就不在此处给出了。
二、使用步骤
1.切换工作路径
cd /home/user/
2.克隆代码
git clone https://github.com/inconshreveable/ngrok.git
3.打开ngrok文件夹
cd ngrok
4.更改域名
export NGROK_DOMAIN="yourdomain"
5.为域名生成证书
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
6.拷贝证书到指定位置
cp rootCA.pem assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
7.编译软件
make release-server release-client
GOOS=windows GOARCH=amd64 make release-client
到此已经完成大部分的工作了,1-7步骤都是在远程服务器完成,其实这个没有任何限制,最后ngrokd放在公网主机,ngrok放在内网主机
8.公网主机上创建ngrokd服务
vim /etc/systemd/system/ngrokd.service
粘贴一下内容:
[Unit]
Description=ngrokd
[Service]
ExecStart=/bin/bash /home/user/ngrokd/ngrokd.sh
[Install]
WantedBy=multi-user.target
ngrokd.sh的内容如下:
ngrokd -tlsKey="../assets/server/tls/snakeoil.key" -tlsCrt="../assets/server/tls/snakeoil.crt" -domain="yourdomain" -httpAddr=":6060" -httpsAddr=":6061" -tunnelAddr=":6062"
sudo chmod 777 /etc/systemd/system/ngrokd.service
sudo systemctl enable ngrokd.service
systemctl daemon-reload
sytemctl ngrokd start
9.内网创建ngrok服务
vim /etc/systemd/system/ngrok.service
粘贴内容如下:
[Unit]
Description=ngrok
[Service]
ExecStart=/bin/bash /home/user/ngrok/ngrok.sh
[Install]
WantedBy=multi-user.target
ngrok.sh内容如下:
#!/bin/bash
cd /home/user/
ngrok -config home/user/.ngrok -log ngrok.log start ssh
.ngrok的内容如下:
server_addr: yourdomian:6062
trust_host_root_certs: false
tunnels:
ssh:
remote_port: 12345
proto:
tcp: 22
sudo chmod 777 /etc/systemd/system/ngrokd.service
sudo systemctl enable ngrokd.service
sudo systemctl daemon-reload
service ngrok star
总结
觉得不够详细可以详见一下链接