家庭网络媒体中心3:编译部署ngrok服务端和客户端
前言
很久以前,在家上网都可以获得公网IP,虽然获得的公网IP不是固定的,但是通过DDNS动态域名解析,可以对域名解析进行动态更新,可是随着IPV4的资源缺乏和网络用户量剧增。很多时候我们上网都只能获取到运营商的内网IP,这个时候就需要内网穿透才可以实现外网访问内网PC。
内网穿透其实也就是在内网和外网某有固定IP的服务器之间建立一个TCP隧道,外部请求都是通过将请求发送到服务端(外网有固定IP那台)再通过这个隧道进行发送与返回结果。
ngrok就是这样的一个内网穿透神器,而且提供开源,可以自己编译搭建自己的客户端和服务端。刚好我有云服务器,所以这个实在比花生壳好用多,关键是端口还是自己定。
编译
我是在Centos进行编译,先要安装go语言和相关只的依赖库
yum install mercurial golang
git版本需要在1.7.9.5以上,如果不符合条件需要将git版本升级。
git --version
下载ngrok源码
cd /opt/
git clone https://github.com/inconshreveable/ngrok.git
使用ngrok.com官方服务时,我们使用的是官方的SSL证书。自建ngrokd服务,我们需要生成自己的证书,并提供携带该证书的ngrok客户端。生成并替换源码里默认的证书,注意域名修改为你自己的。(之后编译出来的服务端客户端会基于这个证书来加密通讯,保证了安全性)
cd ngrok/
mkdir ssl
cd ssl/
NGROK_DOMAIN="xxx.staredis.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 server.key 2048
openssl req -new -key server.key -subj "/CN=$NGROK_DOMAIN" -out server.csr
openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 5000
因为我们后面编译是编译release
版本,release
版本会通过bindata将ngrok源码目录下的assets目录打包到可执行文件(ngrokd和ngrok)中去,assets/client/tls
和assets/server/tls
下分别存放着用于ngrok和ngrokd的默认证书文件,我们需要将它们替换成我们自己生成的。
在后面的执行过程中也可以通过参数进行指定
cp rootCA.pem ../assets/client/tls/ngrokroot.crt
cp server.crt ../assets/server/tls/snakeoil.crt
cp server.key ../assets/server/tls/snakeoil.key
编译前确保时间是正常的
ntpdate -u cn.pool.ntp.org
进入ngrok目录编译服务端和客户端
GOOS=linux GOARCH=amd64 make release-server
GOOS=linux GOARCH=arm make release-client
以上GOARCH有以下可选:amd64
64位版本,386
32位版本,arm
ARM版本
使用
服务端后台启动
nohup /opt/ngrok/ngrokd -domain="xxx.staredis.com" -httpAddr=":8001" -httpsAddr=":8002" -tunnelAddr=":4443" > /opt/ngrok/ngrok.log 2>&1 &
设置开机启动,把上面的启动命令加到开机脚本
vim /etc/rc.local
服务端参数说明
-domain string
Domain where the tunnels are hosted (default "ngrok.com")
-httpAddr string
Public address for HTTP connections, empty string to disable (default ":80")
-httpsAddr string
Public address listening for HTTPS connections, emptry string to disable (default ":443")
-log string
Write log messages to this file. 'stdout' and 'none' have special meanings (default "stdout")
-log-level string
The level of messages to log. One of: DEBUG, INFO, WARNING, ERROR (default "DEBUG")
-tlsCrt string
Path to a TLS certificate file
-tlsKey string
Path to a TLS key file
-tunnelAddr string
Public address listening for ngrok client (default ":4443")
客户端添加一个配置文件ngrok.cfg
server_addr: xxx.staredis.com:4443
trust_host_root_certs: false
tunnels:
test:
subdomain: "aaa"
proto:
http: 80
mstsc:
remote_port: 3389
proto:
tcp: 192.168.1.2:3389
启动命令
./ngrok -config=./ngrok.cfg start-all
客户端命令详解
-authtoken string
Authentication token for identifying an ngrok.com account
-config string
Path to ngrok configuration file. (default: $HOME/.ngrok)
-hostname string
Request a custom hostname from the ngrok server. (HTTP only) (requires CNAME of your DNS)
-httpauth string
username:password HTTP basic auth creds protecting the public tunnel endpoint
-log string
Write log messages to this file. 'stdout' and 'none' have special meanings (default "none")
-log-level string
The level of messages to log. One of: DEBUG, INFO, WARNING, ERROR (default "DEBUG")
-proto string
The protocol of the traffic over the tunnel {'http', 'https', 'tcp'} (default: 'http+https')
-subdomain string
Request a custom subdomain from the ngrok server. (HTTP only)