背景
公司突然要做微信开发,需要做内网穿透,本人一开始使用过www.ngrok.cc
,价格不贵,也很方便,不过自己比较喜欢折腾,于是自己查询资料,可以使用ngrok
在云服务器上搭建来实现多客户端的内网穿透,配置上自己的域名还是很好的。
服务器选择
个人有一台阿里云服务器Ubuntu 16.04 1核1G
,一般使用绝对是没问题的,其他的linux发行版本也是没有问题的。
搭建
域名配置
这里用到的域名是ngrok.codedog.link
,需要将ngrok.codedog.link
和*.ngrok.codedog.link
解析到该云服务器上。
注:博主换过域名,以前用的是codedog.link
,后来感觉有点搞自己,现在用自己名字重新注册了一个luxiang.wiki
配置go语音环境
1 2 3 4 5 6
| #下载 wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz #解压 tar -zxvf go1.9.1.linux-amd64.tar.gz #移动到local目录 mv go /usr/local
|
创建软连接
1 2 3 4 5
| #进入/usr/bin cd /usr/bin ln -s /usr/local/go/bin/go ./ ln -s /usr/local/go/bin/godoc ./ ln -s /usr/local/go/bin/gofmt ./
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| go env GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/root/go" GORACE="" GOROOT="/usr/local/go" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build025512091=/tmp/go-build -gno-record-gcc-switches" CXX="g++" CGO_ENABLED="1" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config"
|
至此go
语言环境配置完成。
安装ngrok
下载源码
1
| git clone https://github.com/inconshreveable/ngrok.git
|
生成证书
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # 进入ngrok源码的目录 cd ngrok # 创建保存自定义签名证书的文件夹 这里叫myssl mkdir myssl # 进入 cd myssl # 设置域名,这里使用 ngrok.codedog.link export NGROK_DOMAIN="ngrok.codedog.link" # 然后依次执行以下命令即可 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 # 替换原来证书 cp rootCA.pem ../assets/client/tls/ngrokroot.crt cp device.crt ../assets/server/tls/snakeoil.crt cp device.key ../assets/server/tls/snakeoil.key
|
编译服务端
1 2 3 4
| # 在ngrok目录执行 export GOOS=linux export GOARCH=386 make release-server
|
编译各大平台客户端
linux
1 2 3
| export GOOS=linux export GOARCH=386 make release-client
|
mac
1 2 3
| export GOOS=darwin export GOARCH=amd64 make release-client
|
windows
1 2 3
| export GOOS=windows export GOARCH=amd64 make release-client
|
生成的可执行文件在 ngrok/bin
目录下,每个平台对应都有文件夹
启动服务端
由于服务器本身启动了80
和443
端口,因此这里改为8000
和444
端口,启动命令如下:
1
| sudo ./ngrokd -domain="ngrok.codedog.link" -httpAddr=":8000" -httpsAddr=":444"
|

上图可以看出使用了4443
端口进行通讯
- 注意:阿里云等有安全组的服务器,需要把端口加入白名单,一共需要开启白名单(
8000
,444
,4443
)
客户端配置文件
将上面生成的客户端可执行文件拷贝到需要内网穿透的设备上即可,这里直接说多域名和多TCP内网穿透配置文件,直接在客户端同级目录下创建ngrok.cfg
配置文件,内容如下(只放了部分):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server_addr: ngrok.codedog.link:4443 trust_host_root_certs: false tunnels: weixin: subdomain: weixin proto: http: 8002 about: subdomain: about proto: http: 192.168.0.1:80 ssh: remote_port: 2020 proto: tcp: 22
|
采用严格的单空格缩进,可以代理其他主机,直接跟端口默认是本主机,还要注意防火墙的配置。
内网穿透
启动特定的转发:
1
| ./ngrok -config ngrok.cfg start weixin
|
将所有配置转发:
1
| ./ngrok -config ngrok.cfg start-all
|