ngrok由golang编写
首先安装依赖包,我也不知道为什么这么多依赖包,网上抄的。。。
[root@localhost ~]# yum install -y mercurial git bzr subversion golang golang-pkg-windows-amd64 golang-pkg-windows-386
下载ngrok源代码
[root@localhost ~]# git clone https://github.com/inconshreveable/ngrok.git
然后生成自签名证书,供用户连接和客户端连接使用
[root@localhost ~]# cd ngrok
[root@localhost ngrok]# NGROK_DOMAIN="milierchen.com"
[root@localhost ngrok]# openssl genrsa -out rootCA.key 2048
[root@localhost ngrok]# openssl req -x509 -new -nodes -key rootCA.key -subj "/CN=$NGROK_DOMAIN" -days 5000 -out rootCA.pem
[root@localhost ngrok]# openssl genrsa -out device.key 2048
[root@localhost ngrok]# openssl req -new -key device.key -subj "/CN=$NGROK_DOMAIN" -out device.csr
[root@localhost ngrok]# openssl x509 -req -in device.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out device.crt -days 5000
将新生成的证书,替换掉assets/client/tls下的证书
[root@localhost ngrok]# cp rootCA.pem assets/client/tls/ngrokroot.crt
[root@localhost ngrok]# cp device.crt assets/server/tls/snakeoil.crt
[root@localhost ngrok]# cp device.key assets/server/tls/snakeoil.key
详细的编译及执行配置文档在 ngrok/docs/SELFHOSTING.md 下
以下记录下我的配置
编译生成服务器文件
回到ngrok根目录,设置编译参数, 且编译
[root@localhost ngrok]# GOOS=linux GOARCH=amd64 make release-server
编译完成后服务器文件在 ./bin/ngrokd
启动服务器:
[root@localhost ngrok]# ./bin/ngrokd -tlsKey="assets/server/tls/snakeoil.key" -tlsCrt="assets/server/tls/snakeoil.crt" -domain="milierchen.com" -httpAddr=":8081" -httpsAddr=":8082" -tunnelAddr=":8083"
以上参数的意思是:
-tlsKey 私钥
-tlsCrt 证书
-domain 要穿透至内网的域名
-httpAddr http访问端口 给用户使用
-httpsAddr https访问端口 给用户使用
-tunnelAddr 隧道端口,给需要进行内网穿透的客户端使用
编译生成客户端文件
[root@localhost ngrok]# GOOS=linux GOARCH=amd64 make release-server
编译完成后客户端文件在 ./bin/ngrok
之后拷贝客户端文件到内网服务器上。或者直接在内网服务器上再编译生成一个也行。
添加配置文件:ngrok.cfg
server_addr: "milierchen.com:8083" # 指向ngrokd的地址和tunnel端口
trust_host_root_certs: false # 不对证书进行信任检查。
tunnels:
http: # 服务名称
subdomain: "happy" # 域名前缀
proto: # 穿透协议
http: "80" # 转发至本地的端口
https:
subdomain: "www"
proto:
https: "8091"
ssh:
remote_port: 2222
proto:
tcp: "22"
启动客户端
[root@localhost ngrok_client]# ./ngrok -config=ngrok.cfg -log=stdout start http
这里的http是ngrok.cfg里面的服务名称。要是三个服务都想启动,就这样执行:
[root@localhost ngrok_client]# ./ngrok -config=ngrok.cfg -log=stdout start http https ssh
之后再内网服务器上启动一个nginx,监听80端口, 访问公网的milierchen.com:8081 就能访问到内网服务器搭建的nginx