自搭ngrok服务器

在阿里云服务器自己搭 ngrok 的服务器,步骤基本跟着 [1, 2] 一步步来,本人需要用到:

  • 一个 linux 的 server 软件,放在有公网 IP 的服务器上(那个阿里云服务器);
  • 一个 linux 的 client 软件,放在自己的内网 linux 机器上;
  • 一个 windows 的 client 软件,放在自己的内网 windows 机器上;

这里仅提几点注意:

  1. 我那个阿里云服务器有个域名,在管理页可以看的,生成证书时设置export NGROK_DOMAIN="<那个域名>"就是用这个域名,后面 ngrokd 的运行参数、client 端的配置文件也要用到(好像说只要内网穿透的话只用 ip 也行?);
  2. 编译前要改两处源代码,使 ngrok 从用 ipv6 转回用 ipv4,见 [2],具体就是:
    • src/ngrok/server/tunnel.go 文件中 net.ListenTCP 后面的 tcp 改为 tcp4
    • src/ngrok/conn/conn.go 文件中 net.Listen 后面的 tcp 改为 tcp4
  3. 前作 [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
    
  4. 要开放防火墙对应端口,用到哪些就开哪些,比如 ngrokd 运行的tunnelAddrhttpAddr、客户端配置文件里用到的那些端口,这些。下面贴配置文件再说;
  5. 阿里云的服务器需要需要在它的网页开放防火墙端口,在系统里设置是没用的(那个阿里云的系统里 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 &

这里出现的tunnelAddrhttpAddrhttpsAddr要在防火墙那放行。

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

References

  1. 内网穿透 ngrok 服务器和客户端配置
  2. ngrok 实现内网穿透
  3. 阿里云端口开放
  4. Linux CentOS 开机自动启动的设置方法
  5. 解决“service nginx does not support chkconfig”的问题?
  6. 用ngrok穿透内网访问windows
  7. .bat和.sh生成带时间的log文件
  8. Ubuntu开机启动脚本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值