ngrok目前只開源1.x版本,最新的是1.7,使用下來發現比花生殼內網版要穩定和好用得多,重要的是ngrok支持mac和Linux平臺,很是好用。先前參考JerryQu的博文搭建 ngrok 服務實現內網穿透進行部署發現被幾個地方卡住研究了好些天才得以解決,所以在此把經驗教訓分享出來,作爲參考博文的補充。謝謝JerryQu的分享。
我使用的環境是CentOS6,安裝ngrok需要新版的git、Golang和其他一些組件,下面是完整的部署過程。
我的域名是yourdomain.com,爲ngrok新開了二級域名ngrok.yourdomain.com,然後新增*.ngrok的泛解析。
#安裝perl-ExtUtils-MakeMaker以及mercurial yum install -y perl-ExtUtils-MakeMaker mercurial
#ngrok基於go語言開發,需要安裝Golang cd /data/download wget https://storage.googleapis.com/golang/go1.4.2.linux-386.tar.gz tar zxf go1.4.2.linux-386.tar.gz mv /data/download/go /data/ ln -s /data/go/bin/* /usr/bin/ export GOROOT="/data/go/"
安裝最新版的git,請勿yum安裝,yum最新才1.7.1,後面make ngrok時候會有問題,見ps。
wget -O git.zip https://github.com/git/git/archive/master.zip autoconf ./configure make && makeinstall ln -s /usr/local/bin/git /usr/bin/
開始安裝ngrok
cd /data git clone https://github.com/inconshreveable/ngrok.git ngrok cd ngrok openssl genrsa -out base.key 2048 openssl req -new -x509 -nodes -key base.key -days 10000 -subj "/CN=ngrok.yourdomain.com" -out base.pem openssl genrsa -out server.key 2048 openssl req -new -key server.key -subj "/CN=ngrok.yourdomain.com" -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 make release-server release-client
編譯完成後,你會在bin目錄下得到兩個程序,一個是ngrok客戶端,這個拷到需要穿透的服務器上運行。一個是ngrokd服務端,在本地運行。
服務器端運行命令:
/data/ngrok/bin/ngrokd -tlsKey=/data/ngrok/server.key -tlsCrt=/data/ngrok/server.crt -domain="ngrok.yourdomain.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083" #上面端口都可以改,記得iptables放通響應端口訪問
客戶端運行命令:
vi ngrok.cfg server_addr: ngrok.yourdomain.com:8083 trust_host_root_certs: false chmod +x ./ngrok ./ngrok -subdomain test -proto=http -config=./ngrok.cfg 80 #這樣在外網通過訪問test.ngrok.yourdomain.com就能通到你的內網服務器80端口了,即便沒有公網地址 #通22端口或者其他tcp端口執行下面命令,界面會輸出分配的外網端口,直接用對應工具訪問ngrok.yourdomain.com加上端口就可以了,同樣記得服務端要開相應端口訪問 ./ngrok -proto=tcp -config=./ngrok.cfg 22
PS.我卡在下面下載很久,假如你的git版本太老或者是yum裝的,在編譯ngrok的時候就會一直卡在
gopkg.in/inconshreveable/go-update.v0 (download) #或者卡在 gopkg.in/yaml.v1 (download) #然後裝新的git就可以了
把編譯出來的32位客戶端放在64位上運行時會報錯
/lib/ld-linux.so.2: bad ELF interpreter #解決方法 yum install -y glibc.i686
ngrok功能很強大,我還在研究和學習中,歡迎交流。