在阿里云服务器自己搭 ngrok 的服务器,步骤基本跟着 [1, 2] 一步步来,本人需要用到:
- 一个 linux 的 server 软件,放在有公网 IP 的服务器上(那个阿里云服务器);
- 一个 linux 的 client 软件,放在自己的内网 linux 机器上;
- 一个 windows 的 client 软件,放在自己的内网 windows 机器上;
这里仅提几点注意:
- 我那个阿里云服务器有个域名,在管理页可以看的,生成证书时设置
export NGROK_DOMAIN="<那个域名>"
就是用这个域名,后面 ngrokd 的运行参数、client 端的配置文件也要用到(好像说只要内网穿透的话只用 ip 也行?); - 编译前要改两处源代码,使 ngrok 从用 ipv6 转回用 ipv4,见 [2],具体就是:
- src/ngrok/server/tunnel.go 文件中
net.ListenTCP
后面的tcp
改为tcp4
; - src/ngrok/conn/conn.go 文件中
net.Listen
后面的tcp
改为tcp4
;
- src/ngrok/server/tunnel.go 文件中
- 前作 [6]在小米球下的 windows 客户端软件好像不能用了,因为编译的时候会将证书也一起写进软件里(所谓 证书 参见 [1, 2] 的配置过程),所以在编译的时候总共要编译三个文件:linux 的 server、linux 的 client、windows 的 client,即这几条:
# linux server GOOS=linux GOARCH=amd64 make release-server # linux client GOOS=linux GOARCH=amd64 make release-client # windows client GOOS=windows GOARCH=amd64 make release-client
- 要开放防火墙对应端口,用到哪些就开哪些,比如 ngrokd 运行的
tunnelAddr
和httpAddr
、客户端配置文件里用到的那些端口,这些。下面贴配置文件再说; - 阿里云的服务器需要需要在它的网页开放防火墙端口,在系统里设置是没用的(那个阿里云的系统里 firewalld 和 iptables 根本就没开),参考 [3];
ngrokd
ngrokd
就是 ngrok 的 server 软件的名字,为了启动方便,将启动命令写成 shell 文件,用到nohup
后台运行,也可以用screen
。log 文件的操作见 [7]。
#!/usr/bash
# run.ngrokd.sh
NGROKD=/root/tom/ngrok/bin
DATE=$(date +%Y-%m-%d-%H-%M)
LOG=/root/tom/log/ngrokd.$DATE.log
nohup $NGROKD/ngrokd -domain="<那个域名>" -tunnelAddr=":4443" \
--httpAddr=":50018" \
--httpsAddr=":50019" \
> $LOG 2>&1 &
这里出现的tunnelAddr
、httpAddr
、httpsAddr
要在防火墙那放行。
auto run
将 ngrokd 设成开机启动,参考 [4, 5],在/etc/rc.d/init.d
放一个 shell 脚本:
#!/bin/sh
# chkconfig: 2345 55 25
sh /root/tom/run.ngrokd.sh
就是运行上面写的那个 ngrokd 启动命令脚本,但注意# chkconfig: 2345 55 25
这行,是我从同目录下的nginx
文件那抄来的,不加这行 [4] 中用chkconfig
的几条命令会报错,见 [5],加上就解决了。
ngrok
client 端的软件叫 ngrok,这里分 windows 和 linux 两台内网机器。
这两个内网 client 的配置文件中用到若干端口(那几个50
开头的),也要在防火墙放行。
in windows machine
将编译出来的软件下载到自己的 windows 机器,就一个ngrok.exe
文件。
基本配置跟之前的 [6] 差不多,就是改下配置文件(ngrok.windows.conf)。
# ngrok.windows.conf
server_addr: "<那个域名>:<上面的 tunnelAddr>"
trust_host_root_certs: false
tunnels:
sshW:
remote_port: 50000
proto:
tcp: 127.0.0.1:22
mstscW:
remote_port: 50001
proto:
tcp: 127.0.0.1:3389
jupyterW:
remote_port: 50002
subdomain: jupyter148
proto:
http: 127.0.0.1:8888
tensorboardW:
remote_port: 50003
subdomain: tensorboard148
proto:
http: 127.0.0.1:6006
将启动命令写成 .bat 文件:
@echo off
set DAY=%date:~0,4%-%date:~5,2%-%date:~8,2%
set TIME=%time:~0,2%-%time:~3,2%
set NGROK=E:\ngrok
set LOG=%NGROK%\log\ngrok.%DAY%-%TIME%.log
%NGROK%\ngrok.exe -config=%NGROK%\ngrok.windows.conf -log=%LOG% start sshW mstscW jupyterW tensorboardW
注意这里用新编译的那个 ngrok.exe,而不是用 [6] 里下的那个。[6] 下的那个也能用,但配置文件就用回 [6] 里的配置文件。
in linux machine
也是下那个 linux 的 client 软件,也是只有也个ngrok
文件,放在自己内网的 linux 机器。
配置文件 ngrok.linux.conf
# ngrok.linux.conf
server_addr: "<那个域名>:<上面的 tunnelAddr>"
trust_host_root_certs: false
tunnels:
sshL:
remote_port: 50010
proto:
tcp: 127.0.0.1:22
jupyterL:
remote_port: 50012
subdomain: jupyter243
proto:
http: 127.0.0.1:8888
tensorboardL:
remote_port: 50013
subdomain: tensorboard243
proto:
http: 127.0.0.1:6006
这里不用 mstsc,因为那个是 windows 的远程登录。
启动命令:
#!/bin/sh
DATE=$(date +%Y-%m-%d-%H-%m)
./ngrok -log=log/ngrok.$DATE.log -config=ngrok.linux.conf start sshL jupyterL tensorboardL