2018-03-19
ngrok簡介
ngrok是一個開源的內(nèi)網(wǎng)穿透服務(wù)(1.7之前的版本),通過反向代理實現(xiàn)端口間的映射,使得內(nèi)網(wǎng)服務(wù)(內(nèi)網(wǎng)中所有的機器)能夠通過外網(wǎng)IP/域名進行訪問(將請求轉(zhuǎn)發(fā)至指定機器,內(nèi)網(wǎng)中安裝一個客戶端即可)。
必備條件
一臺有外網(wǎng)IP的可提供服務(wù)的服務(wù)器(用來運行ngrok服務(wù)端)
GO語言環(huán)境
開放防火墻端口
ngrok服務(wù)端
GitHub上面提供的是服務(wù)端的源碼,客戶端是在指定配置之后編譯生成的。
安裝GO語言環(huán)境
安裝ngrok,編譯生成客戶端
git clone https://github.com/inconshreveable/ngrok.git
cd ngrok
配置證書信息,以生成專屬的客戶端(可寫入shell腳本執(zhí)行)
NGROK_DOMAIN="abc.com" #換成你的域名(綁定外網(wǎng)IP的域名)
openssl genrsa -out base.key 2048
openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=$NGROK_DOMAIN" -out base.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 base.pem -CAkey base.key -CAcreateserial -days 10000 -out server.crt
cp base.pem assets/client/tls/ngrokroot.crt
編譯生成客戶端和服務(wù)端(有針對不同系統(tǒng)生成的客戶端,自行搜索)
make release-server release-client
編譯生成的執(zhí)行文件放在ngrok/bin下,ngrok為客戶端,ngrokd為服務(wù)端。
- ngrok-server參數(shù)說明
-httpAddr # http端口(通過外網(wǎng)IP或域名加上這個端口進行http訪問內(nèi)網(wǎng))
-httpsAddr #同http,只是這個是https
-tunnelAddr #隧道端口,內(nèi)網(wǎng)和外網(wǎng)建立的隧道端口,默認為4443(客戶端配置連接的端口)
ngrokd啟動命令
nohup ./bin/ngrokd -tlsKey=server.key -tlsCrt=server.crt -domain="your domain" -httpAddr=":8081" &
#nohup可將進程置入后臺,且ssh連接斷開不受影響
netstat -tpln|grep ngrokd #查看進程端口狀態(tài)
ngrok客戶端
將客戶端從外網(wǎng)服務(wù)器上面下載至內(nèi)網(wǎng)機器
在內(nèi)網(wǎng)機器上寫一個配置文件ngrok.cfg
該配置文件可一次性轉(zhuǎn)發(fā)多個端口,建議按這種格式,配置文件為yaml語法,所有縮進需要使用空格
server_addr: ngrok.abc.com:4443
trust_host_root_certs: false
tunnels:
ssh:
remote_port: 1122
proto:
tcp: 22
shadowsock:
emote_port: 1088
proto:
tcp: 1188
ftp:
remote_port: 24
proto:
tcp: 24
http:
subdomain: gitlab
proto:
http: 80
https: 172.3.2.1:443
```
#### 啟動特定的隧道
```
./ngrok -config ngrok.cfg start ssh ftp
```
#### 啟用配置文件中所有的隧道
```
./ngrok -config ngrok.cfg start-all
```
參考文章
http://www.lxweimin.com/p/69b4f10ad954
https://imququ.com/post/self-hosted-ngrokd.html
https://morongs.github.io/2016/12/28/dajian-ngrok/