CentOS 7 搭建ngrok服务器 内网穿透,从此不再需要花生壳

免费资源,服务器

2017-03-18

22628

54

在windows下面编译类似,本人已经在windows下编译成功,只是需要安装MinGW64,这里就只讲CentOS下编译过程

一、前提条件

      一台云服务器,一个域名(二级域名也可以),并且域名正确解析到云服务器

二、提前准备工作

1、安装gcc

yum install gcc

2.安装git

yum install git

3.安装GO语言环境

   到网站https://golang.org/dl/查找最新的版本链接,我的服务器是64位的,当前最新版本是:https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz

   下载:

wget https://storage.googleapis.com/golang/go1.8.linux-amd64.tar.gz

   解压安装:

tar -C /usr/local/ -zxvf go1.8.linux-amd64.tar.gz

  添加环境变量,编辑:vi /etc/profile,在最后添加:

#go lang
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

 使环境变量生效:

source /etc/profile

检查是否安装成功:

go version

 输出:go version go1.8 linux/amd64表示安装成功

三、在服务器上搭建Ngrok服务

  1.下载ngrok源码

cd /usr/local/src
git clone https://github.com/inconshreveable/ngrok.git

2.生成证书

在自生成证书时需要一个解析到服务器上的主域名(二级域名也可以,本人已经验证过),现在以”uboff.com”为例:

cd ngrok

#这里修改为自己的域名
export NGROK_DOMAIN="uboff.com"

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 device.key 2048
openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000

将新生成的证书,替换掉assets/client/tls下的证书

cp rootCA.pem assets/client/tls/ngrokroot.crt
cp device.crt assets/server/tls/snakeoil.crt
cp device.key assets/server/tls/snakeoil.key

 3.编译生成ngrokd(服务端)

#这里是交叉编译,linux系统GOOS=linux,64位系统GOARCH=amd64,32位系统GOARCH=386
#当前系统可用go env查看
GOOS=linux GOARCH=amd64
make release-server

编译成功后在当前目录的bin目录下可找到ngrokd文件

在编译过程中有可能出现如下情况: 

           问题1:

    GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindata

      bin/go-bindata -nomemcopy -pkg=assets -tags=release \

                   -debug=false \

                   -o=src/ngrok/client/assets/assets_release.go \

                   assets/client/...

    make: bin/go-bindata: Command not found

      make: *** [client-assets] Error 127

      解决方法:前往go安装目录的bin目录下找到go-bindata,将他移动到ngrok/bin下 (没有bin,可新建一个)   
         问题2:

         package code.google.com/p/log4go: Get https://code.google.com/p/log4go/source/checkout?repo=: dial tcp 216.58.197.110:443: i/o timeout

         因为google被墙,如果服务器不在墙外或者没有FQ则无法访问到code.google.com.

         解决方法:在 ngrok/src/ngrok/log 目录下找到 logger.go 文件,修改其中第4或5行的:

            log "code.google.com/p/log4go”为

            log "github.com/keepeye/log4go"  


        问题3:

          GOOS="" GOARCH="" go get github.com/jteeuwen/go-bindata/go-bindata
         # github.com/jteeuwen/go-bindata
            src/github.com/jteeuwen/go-bindata/toc.go:47: function ends without a return statement
            make: *** [bin/go-bindata] Error 2
         解决办法: https://github.com/inconshreveable/ngrok/issues/237
     
      启动服务端(/usr/local/src/ngrok目录下)
./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="$NGROK_DOMAIN"  -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"

出现下面信息,启动成功

[14:52:23 CST 2017/03/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified
[14:52:23 CST 2017/03/18] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8081
[14:52:23 CST 2017/03/18] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:8082
[14:52:23 CST 2017/03/18] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:8083
[14:52:23 CST 2017/03/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds

4.编译生成ngrok(客户端)

  我这里生成windows下的客户端
  为go生成交叉编译环境,执行如下命令:
#这里是交叉编译,linux系统GOOS=linux,64位系统GOARCH=amd64,32位系统GOARCH=386
#当前系统可用go env查看
GOOS=windows GOARCH=amd64 make release-client

一般情况下是可以顺利成功编译出windows客户端的

注:如果没有生成windows客户端,则需要先执行如下命令,然后再执行上面命令生成客户端,如果可以生成就跳过此步骤,
     此步骤是我在过了一段时间(1个多月)后回来编译发现没有生成windows客户端,在网上搜索到的解决办法:

cd /usr/local/go/src
GOOS=windows GOARCH=amd64 ./make.bash
cd -

成功会在bin目录下看到windows_amd64文件夹,复制到windows电脑上即可启动

在windows_amd64目录下新建一个ngrok.cfg文件,内容如下:

server_addr: "uboff.com:8083"
trust_host_root_certs: false
tunnels:
  http:
    subdomain: "www"
    proto:
      http: "8090"
      
  https:
    subdomain: "www"
    proto:
      https: "8091"
 
  ssh:
    remote_port: 2222
    proto:
      tcp: "22"

然后就可以启动客户端,我已经把windows_amd64文件夹下载到D盘下,打开CMD输入:

D:
cd D:\windows_amd64
ngrok.exe -config ngrok.cfg start http https ssh

看到下面信息则启动成功:

Tunnel Status                 online
Version                       1.7/1.7
Forwarding                    http://www.uboff.com:8081 -> 127.0.0.1:8090
Forwarding                    https://www.uboff.com:8082 -> 127.0.0.1:8091
Forwarding                    tcp://uboff.com:2222 -> 127.0.0.1:22
Web Interface                 127.0.0.1:4040
# Conn                        0
Avg Conn Time                 0.00ms

打开http://www.uboff.com:8081即可看到成功打开,到此Ngrok服务搭建完成

小额赞助,鼓励博主写出更多好文章

发表评论

全部评论:55条

网友 2017-12-19

强力建议新建一个群号啊!!!!!!!

网友 2017-12-19

引用来之于UBOCK的内容

既然已经连接成功了,应该没关系吧,如果你能正常访问就不要管它了

可是不能访问啊

UBOCK 2017-12-19

引用来之于网友的内容

ngrok (Ctrl+C to quit) Tunnel Status online Version 1.7/1.7 Forwarding http://ngrok.123hello.tk.:8081 -> 127.0.0.1:8090 Forwarding https://ngrok.123hello.tk.:8082 -> 127.0.0.1:8091 Forwarding tcp://:2222 -> 127.0.0.1:22 Web Interface 127.0.0.1:4040 # Conn 0 Avg Conn Time 0.00ms 为什么我的域名后面会有一个点啊,我的ngrok.cfg配置文件里面没有啊

既然已经连接成功了,应该没关系吧,如果你能正常访问就不要管它了

网友 2017-12-19

ngrok (Ctrl+C to quit) Tunnel Status online Version 1.7/1.7 Forwarding http://ngrok.123hello.tk.:8081 -> 127.0.0.1:8090 Forwarding https://ngrok.123hello.tk.:8082 -> 127.0.0.1:8091 Forwarding tcp://:2222 -> 127.0.0.1:22 Web Interface 127.0.0.1:4040 # Conn 0 Avg Conn Time 0.00ms 为什么我的域名后面会有一个点啊,我的ngrok.cfg配置文件里面没有啊

网友 2017-12-19

非常感谢你的提醒关闭防火墙就可以了!!!!!

网友 2017-12-19

http://chengmeiqq.hkhost13.08jt.com/1.png 麻烦帮忙我看看谢谢! 我弄了几天了看您这边的教程都跟您上面的教程一样的!!

UBOCK 2017-12-19

引用来之于网友的内容

您好我按照您的教程基本系统也是一样的,成功都编译成功了但是 就是无法使用!!连接失败,我用ngrok.cc上面测试是可以正常使用的,我自己搭建的就连接不上!!!麻烦您帮忙看看谢谢!!!509898118 qq ttpsAddr=":8082" -tunnelAddr=":8083" [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8081 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:8082 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:8083 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] R eporting every 30 seconds [14:53:37 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun eter.m1":0,"windows":0} [14:54:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun eter.m1":0,"windows":0} [14:54:37 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun

从打印的log中可以看出服务端没有收到来自客户端的连接请求
1、请确保你的域名已经解析到你的服务器,或者是域名写错了
2、是否是防火墙拦截请求,可以先关闭它

网友 2017-12-18

您这边可以新建一个群号方便学习!!

网友 2017-12-18

您好我按照您的教程基本系统也是一样的,成功都编译成功了但是 就是无法使用!!连接失败,我用ngrok.cc上面测试是可以正常使用的,我自己搭建的就连接不上!!!麻烦您帮忙看看谢谢!!!509898118 qq ttpsAddr=":8082" -tunnelAddr=":8083" [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:8081 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:8082 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:8083 [14:53:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] R eporting every 30 seconds [14:53:37 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun eter.m1":0,"windows":0} [14:54:07 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun eter.m1":0,"windows":0} [14:54:37 UTC 2017/12/18] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting: {"bytesIn.co r.count":0,"connMeter.m1":0,"httpTunnelMeter.count":0,"linux":0,"osx":0,"other":0,"tcpTunnelMeter.coun

UBOCK 2017-12-05

引用来之于网友的内容

Tunnel http://*:8081 unavailable Unable to initiate connection to 127.0.0.1:8090. A web server must be running on port 127.0.0.1:8090 to complete the tunnel. 这个问题怎么解决呢,thank you

你是在同一台机器上运行服务端和客户端吗?服务端没有启动

UBOCK

找方法不找借口