Skip to content

Instantly share code, notes, and snippets.

@imfing
Last active January 24, 2018 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imfing/5d4b3b1558d34168a6e18d1d2dd26532 to your computer and use it in GitHub Desktop.
Save imfing/5d4b3b1558d34168a6e18d1d2dd26532 to your computer and use it in GitHub Desktop.
Ngrok内网穿透配置

准备公网域名 创建两个A记录ngrok*.ngrok到云主机的IP

在系统上安装Go,最好不要安装在/usr/local/下,会有很多权限问题,直接安装在home目录下即可

注意对应下载相应系统的版本

参考的配置,其中GOPATH不用管:

GOPATH="/home/ubuntu/go"
GORACE=""
GOROOT="/home/ubuntu/dev/go"
GOTOOLDIR="/home/ubuntu/dev/go/pkg/tool/linux_386"

配置好环境变量之后就可以使用go version或者go env来查看相应的环境变量

下载ngrok,注意对应的环境变量目录,最好也下载在home目录下面

git clone https://github.com/inconshreveable/ngrok.git
cd ngrok

生成相应的证书,中间最好不要使用环境变量$NGROK_DOMAIN,可能出错

openssl genrsa -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=ngrok.example.cn" -days 5000 -out rootCA.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=ngrok.example.cn" -out server.csr
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000

拷贝证书文件到ngrok响应目录

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

切换到相应的目录下,先编译go,再编译ngrok。如果要编译32位平台的需要使用GOARCH=386

cd /usr/local/go/src
GOOS=linux GOARCH=amd64 ./make.bash
cd /usr/local/ngrok/
GOOS=linux GOARCH=amd64 make release-server release-client

其中,会有个问题$GOROOT_BOOTSTRAP must not be set to $GOROOT

配置的 $GOROOT_BOOTSTRAP 和 $GOROOT 一致导致。可以复制一份go的源码到另外的路径,如/usr/local/go-copy,同时export GOROOT_BOOTSTRAP=/usr/local/go-copy

备注编译MAC和WIN客户端,分别对应GOOS=darwinGOOS=windows

编译完成之后在服务器上打开服务端ngrokd,切换到ngrok/bin目录下

sudo ./ngrokd -domain="ngrok.example.cn" -httpAddr=":8081" -tunnelAddr=":8082"

将服务器8081端口给http请求,将8082端口给tunnel请求(如ssh)

再把编译好的客户端ngrok下载到本机

在相同目录创建一个ngrok.cfg的文件,修改内容

server_addr: ngrok.example.cn:8082
trust_host_root_certs: false
tunnels:
    http:
        proto:
            http: 80
        subdomain: pi
    ssh:
        remote_port: 8084
        proto:
            tcp: 22

其中8082要对应服务器端的tunnel地址,否则无法连接。

在本地运行./ngrok -config=ngrok.cfg start http ssh就可以打开http和ssh的tunnel了

看到Tunnel Status online以及下面的地址就表面隧道建立好了

这时候就可以通过ssh user@ngrok.example.cn -p 8084来ssh登录本地账户了,其中user为本地用户名,8084为配置文件中的端口

参考:

http://linfuyan.com/ubuntu-ngrok/

https://zhuanlan.zhihu.com/p/29019562

https://zhuanlan.zhihu.com/p/25768758

http://blog.csdn.net/shikewei0103/article/details/41284751

http://www.k2zone.cn/?p=674

http://www.echojb.com/go/2017/06/14/428700.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment