搭建NGROK内网穿透服务

搭建NGROK内网穿透服务

准备所需:

  1. 顶级域名(freenom.com可注册域名)
  2. 服务器一台,linux即可(这里用的是腾讯的centos7.2)
  3. 把域名解析到服务器(这里用的是dnspod)

服务器端

安装git

1、安装git,我安装的是2.6版本,防止会出现另一个错误,安装git所需要的依赖包

  1. yum -y install zlib-devel openssl-devel perl hg cpio expat-devel gettext-devel curl curl-devel perl-ExtUtils-MakeMaker hg wget gcc gcc-c++

2、下载git

wget https://www.kernel.org/pub/software/scm/git/git-2.6.0.tar.gz

3、解压git

  1. tar zxvf git-2.6.0.tar.gz

4、编译git

  1. cd git-2.6.0
  2. ./configure –prefix=/usr/local/git
  3. make
  4. make install

5、创建git的软连接

  1. ln -s /usr/local/git/bin/* /usr/bin/

安装go环境

准备go环境,我的系统是64位的centos所以我下载amd64的包

1、下载go的软件包

https://golangtc.com/static/go/1.9.1/go1.9.1.linux-amd64.tar.gz

2、解压出来可以随便指定位置

  1. tar -zxvf go1.4.2.linux-386.tar.gz
  2. mv go /usr/local/

3、go的命令需要做软连接到/usr/bin

  1. ln -s /usr/local/go/bin/* /usr/bin/

编译ngrok

  1. cd /usr/local/
  2. git clone https://github.com/inconshreveable/ngrok.git
  3. export GOPATH=/usr/local/ngrok/
  4. export NGROK_DOMAIN=”cn”
  5. cd ngrok

为域名生成证书

  1. openssl genrsa -out rootCA.key2048
  2. openssl req -x509 -new -nodes -key rootCA.key-subj “/CN=$NGROK_DOMAIN” -days 5000 -out rootCA.pem
  3. openssl genrsa -out server.key2048
  4. openssl req -new -key server.key-subj “/CN=$NGROK_DOMAIN” -out server.csr
  5. openssl x509 -req -in server.csr-CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000

在软件源代码目录下面会生成一些证书文件,我们需要把这些文件拷贝到指定位置

  1. cp rootCA.pemassets/client/tls/ngrokroot.crt
  2. cp server.crtassets/server/tls/snakeoil.crt
  3. cp server.keyassets/server/tls/snakeoil.key

如果是在天朝的服务器需要改,香港或者国外的服务器不需要

  1. vim /usr/local/ngrok/src/ngrok/log/logger.go
  2. log “github.com/keepeye/log4go”

指定编译环境变量,如何确认GOOS和GOARCH,可以通过go env来查看

编译服务端

  1. cd /usr/local/go/src
  2. GOOS=linux GOARCH=386
  3. ./make.bash
  4. cd /usr/local/ngrok/
  5. GOOS=linux GOARCH=386
  6. make release-server

编译客户端,我的是树莓派,所以我的是下面的命令

  1. cd /usr/local/go/src
  2. GOOS=linux GOARCH=arm ./make.bash
  3. cd /usr/local/ngrok/
  4. GOOS=linux GOARCH=arm make release-client

编译好的文件在/usr/local/ngrok/bin下面

Windows的客户端编译

cd /usr/local/go/src

GOOS=windows GOARCH=amd64 ./make.bash

cd /usr/local/ngrok/

GOOS=windows GOARCH=amd64 make release-client

客户端配置文件

  1. server_addr:”candy994.cf:4443″
  2. trust_host_root_certs:false

服务端启动

/ngrokd -domain=”candy994.cn” -httpAddr=”:80″

ngrokd -domain=”tunnel.tonybai.com” -httpAddr=”:8080″ -httpsAddr=”:8081″

客户端使用 (树莓派)

  1. ./ngrok -config=./ngrok.cfg-subdomain=blog 80
  2. setsid ./ngrok -config=./ngrok.cfg-subdomain=test 80 #在linux下如果想后台运行

启动成功如下效果

 

 

 

注:这里测试时只编译了i386的服务器端和arm的客户端,实际上可以通过改变GOOS与GOARCH来获取各个平台的客户端与服务端,GOOS可以指定为windows、linux 、 freebsd 、darwin (Mac OS X 10.5 or 10.6) 和 nacl (Chrome 的Native Client 接口) ,GOARCH可以指定为amd64 (64-bit x86) 、386 (32-bit x86) 、 和arm (32-bit ARM)。

Windows下使用ngrok

同样需要配置文件

进入ngrok所在目录,输入以下命令

ngrok.exe -config ngrok.cfg -subdomain server 3389

成功如下图:

 

 

出现这个错误说明需要安装hg

package code.google.com/p/log4go: exec: “hg”: executable file not found in $PATH

解决办法

  1. yum install hg -y

编译到 go get gopkg.in/yaml.v1 的时候卡住不走了,说明是git比较低,版本需要大于1.7.9.5以上

fatal: Unable to find remote helper for ‘https’ 出现这个问题,可以重新安装 curl curl-devel 然后再重装git

安装git-core

  1. wget https://www.kernel.org/pub/software/scm/git/git-core-0.99.6.tar.gz
  2. tar zxvf git-core-0.99.6.tar.gz
  3. cd git-core-0.99.6
  4. make prefix=/usr/libexec/git-core install
  5. export PATH=$PATH:/usr/libexec/git-core/

 

附上树莓派cfg文件:

~       每次用命令启动显得有些麻烦,可以用脚本启动,下次启动直接执行脚本

 

 

当然也可以把脚本开机启动 编辑/etc/rc.local文件

//设置ngrok开机自启动

cp /usr/local/src/ngrok/bin/ngrokd /usr/bin/ngrokd

chmod +x /usr/bin/ngrokd

vi /etc/rc.local

ngrokd -domain=”candy994.cn” -httpAddr=”:8081″ -httpsAddr=”:8082″

/usr/local/ngrok/bin/ngrokd -domain=”candy994.cn” -httpAddr=”:81″

 

 

 

 

 

//生成windows64位客户端

 

cd /usr/local/go/src/

GOOS=windows GOARCH=amd64 CGO_ENABLED=0 ./make.bash

cd /usr/local/src/ngrok

GOOS=windows GOARCH=amd64 make release-client

 

 

//生成windows32位客户端

 

cd /usr/local/go/src/

GOOS=windows GOARCH=386 CGO_ENABLED=0 ./make.bash

cd /usr/local/src/ngrok

GOOS=windows GOARCH=386 make release-client

 

//生成Linux客户端

 

cd /usr/local/go/src/

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 ./make.bash

cd /usr/local/src/ngrok

GOOS=linux GOARCH=amd64 make release-client

 

//生成树莓派客户端

 

cd /usr/local/go/src/

GOOS=linux GOARCH=arm CGO_ENABLED=0 ./make.bash

cd /usr/local/src/ngrok

GOOS=linux GOARCH=arm make release-client

 

 

//客户端运行语句

ngrok -config=./ngrok.cfg -subdomain=www 80

 

 

 

/etc/rc.local

 

Runngrok.sh

#!/bin/bash

 

while true

do

procnum=` ps -ef|grep “ngrokd”|grep -v grep|wc -l`

if [ $procnum -eq 0 ]; then

/usr/local/ngrok/bin/ngrokd -domain=”candy994.cn” -httpAddr=”:82″ -tunnelAddr=”:8008″ > /dev/null 2>&1 &

fi

sleep 30

done

 

vi /etc/sysconfig/iptables //设置防火墙放行端口

注:如连接不上可检查端口是否被占用,是否装了网站环境,防火墙端口是否开放或者安全组端口是否开放。

发表评论

邮箱地址不会被公开。 必填项已用*标注