注:本文基于Sunny大大的教程“搭建屬于自己的ngrok服務(wù)器”改編博杖,適合Debian系統(tǒng)使用
文章鏈接:http://www.sunnyos.com/article-show-48.html
1.編譯環(huán)境安裝
檢查并安裝 gcc郭膛, make 和 git
apt-get install gcc
apt-get install make
apt-get install git
下載go最新版本
進(jìn)入網(wǎng)頁https://www.golangtc.com/download
32bit系統(tǒng) 對應(yīng) 386版本
64bit系統(tǒng) 對應(yīng) amd64版本
右鍵對應(yīng)版本的“本地下載”按鈕,復(fù)制鏈接地址
我下載的是1.9.2的linux 32位版本
wget https://www.golangtc.com/static/go/1.9.2/go1.9.2.linux-386.tar.gz
解壓并移到/usr/loacl/下
tar -zxvf go1.9.2.linux-386.tar.gz
mv go /usr/local/
由于編譯GO 1.6版本以上的需要依賴GO 1.4版本的二進(jìn)制文件
還需要下載go1.4.2版本
百度網(wǎng)盤:https://pan.baidu.com/s/1pL0Ca4V#list/path=%2Fgo
解壓go1.4.2到root文件夾下
cd /root/
tar -zxvf go1.4.2.linux-386.tar.gz
mv go go1.4
2.獲取并編譯ngrok
cd /usr/local/
git clone https://github.com/inconshreveable/ngrok.git
添加環(huán)境變量
假設(shè)ngrok使用的域名為ngrok.luziqi.cn
vi /etc/profile
//在最下加入以下兩條內(nèi)容
GOPATH=/usr/local/ngrok/
NGROK_DOMAIN="ngrok.luziqi.com"
//
source /etc/profile
為域名生成證書并拷貝到指定位置
cd ngrok
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 assets/client/tls/ngrokroot.crt
cp server.crt assets/server/tls/snakeoil.crt
cp server.key assets/server/tls/snakeoil.key
如果服務(wù)器在國內(nèi)梅尤,需要修改文件
vim /usr/local/ngrok/src/ngrok/log/logger.go
//第五行改成
log "github.com/keepeye/log4go"
編譯服務(wù)端(linux 32bit)
cd /usr/local/go/src
GOOS=linux GOARCH=386 ./make.bash
cd /usr/local/ngrok/
GOOS=linux GOARCH=386 make release-server
編譯客戶端(windows 64bit)
cd /usr/local/go/src
GOOS=windows GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=windows GOARCH=amd64 make release-client
編譯好的內(nèi)容存放在/usr/local/ngrok/bin下
3.運(yùn)行ngrok
運(yùn)行服務(wù)端
/usr/local/ngrok/bin/ngrokd -domain="$NGROK_DOMAIN" -httpAddr=":80"
配置和運(yùn)行客戶端
在ngrok.exe的所在目錄下創(chuàng)建ngrok.cfg文件,編輯一下內(nèi)容
假設(shè)我服務(wù)器下的變量$NGROK_DOMAIN為ngrok.luziqi.com稠诲,則
server_addr: "ngrok.luziqi.com:4443"
trust_host_root_certs: false
還可以加入以下內(nèi)容定義tunnel方便使用
tunnels:
ssh:
remote_port: 8022
proto:
tcp: 192.168.2.211:22
mc:
remote_port: 8200
proto:
tcp: 25565
http:
subdomain: www
proto:
http: 80
打開cmd舵匾,cd到ngrok.exe所在目錄
映射http端口80,子域名為blog
ngrok.exe -config=ngrok.cfg -subdomain=blog 80
映射tcp端口8888
ngrok.exe -config=ngrok.cfg -proto=tcp 8888
使用tunnel參數(shù)映射
ngrok.exe -config=ngrok.cfg start mc