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-04

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 2017-11-29

引用来之于网友的内容

[root@localhost src]# GOOS=windows GOARCH=amd64 ./make.bash ##### Building Go bootstrap tool. cmd/dist ERROR: Cannot find /root/go1.4/bin/go. Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4. -----go我已经安装了,安装的是1.9

从log来看应该是跟你的go环境有关,go最好是用源码安装,我用的是1.8版本,你可以装个1.8版本试试

网友 2017-11-28

引用来之于UBOCK的内容

你可以尝试一下如下命令,再编译一次: cd /usr/local/go/src GOOS=windows GOARCH=amd64 ./make.bash cd -

[root@localhost src]# GOOS=windows GOARCH=amd64 ./make.bash ##### Building Go bootstrap tool. cmd/dist ERROR: Cannot find /root/go1.4/bin/go. Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4. -----go我已经安装了,安装的是1.9

UBOCK 2017-11-28

引用来之于网友的内容

[root@localhost ngrok]# GOOS=windows GOARCH=amd64 make release-client bin/go-bindata -nomemcopy -pkg=assets -tags=release \ -debug=false \ -o=src/ngrok/client/assets/assets_release.go \ assets/client/... bin/go-bindata -nomemcopy -pkg=assets -tags=release \ -debug=false \ -o=src/ngrok/server/assets/assets_release.go \ assets/server/... go get -tags 'release' -d -v ngrok/... go install -tags 'release' ngrok/main/ngrok # syscall ../../go/src/syscall/dll_windows.go:24:6: missing function body ../../go/src/syscall/dll_windows.go:25:6: missing function body ../../go/src/syscall/dll_windows.go:26:6: missing function body ../../go/src/syscall/dll_windows.go:27:6: missing function body ../../go/src/syscall/dll_windows.go:28:6: missing function body ../../go/src/syscall/dll_windows.go:29:6: missing function body ../../go/src/syscall/dll_windows.go:30:6: missing function body ../../go/src/syscall/dll_windows.go:31:6: missing function body ../../go/src/syscall/syscall.go:101:6: missing function body ../../go/src/syscall/syscall_windows.go:121:6: missing function body ../../go/src/syscall/syscall_windows.go:121:6: too many errors make: *** [client] 错误 2 运行 GOOS=windows GOARCH=amd64 make release-client 这条语句报错了,不怎么懂Linux ,帮忙解答一下

你可以尝试一下如下命令,再编译一次: cd /usr/local/go/src GOOS=windows GOARCH=amd64 ./make.bash cd -

网友 2017-11-28

引用来之于UBOCK的内容

这个提示说明服务端已经启动成功了

[root@localhost ngrok]# GOOS=windows GOARCH=amd64 make release-client bin/go-bindata -nomemcopy -pkg=assets -tags=release \ -debug=false \ -o=src/ngrok/client/assets/assets_release.go \ assets/client/... bin/go-bindata -nomemcopy -pkg=assets -tags=release \ -debug=false \ -o=src/ngrok/server/assets/assets_release.go \ assets/server/... go get -tags 'release' -d -v ngrok/... go install -tags 'release' ngrok/main/ngrok # syscall ../../go/src/syscall/dll_windows.go:24:6: missing function body ../../go/src/syscall/dll_windows.go:25:6: missing function body ../../go/src/syscall/dll_windows.go:26:6: missing function body ../../go/src/syscall/dll_windows.go:27:6: missing function body ../../go/src/syscall/dll_windows.go:28:6: missing function body ../../go/src/syscall/dll_windows.go:29:6: missing function body ../../go/src/syscall/dll_windows.go:30:6: missing function body ../../go/src/syscall/dll_windows.go:31:6: missing function body ../../go/src/syscall/syscall.go:101:6: missing function body ../../go/src/syscall/syscall_windows.go:121:6: missing function body ../../go/src/syscall/syscall_windows.go:121:6: too many errors make: *** [client] 错误 2 运行 GOOS=windows GOARCH=amd64 make release-client 这条语句报错了,不怎么懂Linux ,帮忙解答一下

UBOCK 2017-11-28

引用来之于网友的内容

[root@localhost ngrok]# ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:80 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds 这个是为什么? 后面就是一只的重复

这个提示说明服务端已经启动成功了

UBOCK 2017-11-28

引用来之于网友的内容

你好,有反应,好像说的是坏的证书还是什么的,方便留个联系方式吗?

u@ubock.com

网友 2017-11-28

[root@localhost ngrok]# ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [registry] [tun] No affinity cache specified [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for public http connections on [::]:80 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for public https connections on [::]:443 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.Info:112) Listening for control and proxy connections on [::]:4443 [13:46:30 CST 2017/11/28] [INFO] (ngrok/log.(*PrefixLogger).Info:83) [metrics] Reporting every 30 seconds 这个是为什么? 后面就是一只的重复

网友 2017-11-28

引用来之于UBOCK的内容

应该是可以看一下服务器端有没有响应,服务器的日志

你好,有反应,好像说的是坏的证书还是什么的,方便留个联系方式吗?

UBOCK 2017-11-28

引用来之于网友的内容

奇怪,一直都是reconnecting 也没报什么错

应该是可以看一下服务器端有没有响应,服务器的日志

UBOCK

找方法不找借口