V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lswang
V2EX  ›  程序员

内网穿透工具(替代 ngrok 和花生壳)

  •  5
     
  •   lswang · 2019-11-08 10:25:29 +08:00 · 16017 次点击
    这是一个创建于 1603 天前的主题,其中的信息可能已经有所发展或是发生改变。

    简介

    由于 IPv4 资源的有限性,目前大部分的本地电脑都是没有公网 IP。所以想要访问自己的本地服务(ssh、http、vnc、NAS、智能家居接口回调 - 比如天猫精灵自定义语义),需要有个服务来进行内网穿透。 目前有一些工具比如花生壳、ngrok 等。但是,

    1. 花生壳是收费的;
    2. ngrok 第二版也开始收费(第一版据说有一些严重 bug)。

    所以,需要一款开源的方案( 当然还需要一个固定的公网 IP >_<)。

    解决方案

    本人用 netty 写了一个 tcp 的穿透工具叫 Tcp Through,直接下载可用。安装方式下面有介绍

    该项目分 server 端和 client 端,想要看详细信息的话,可以去 github 上查看

    这个工具有以下一些比较实用的功能

    1. 支持 http api 管理服务端,还支持 命令行来管理(自己写的 python 库,自己感觉还挺炫)
    2. 支持安全模式。只有在信任列表的 IP 地址才可以访问 client (防止攻击的最好方式)
    3. 支持速度限制,功能已经测试可用。只是目前在代码中没有限速,可自己修改代码,具体代码在 server 库中的 OuterServer 中,注释中有说明。
    4. client 可设定不允许 server 控制 (client 端的 isRemoteManage 参数)
    5. 支持查看实时速率(不需要用总量来计算)
    6. 管理通道进行了 SSL 加密,防止注册信息被抓包
    7. 数据平面和控制平面分离,提高性能(mac 下测速能达到 10Gb/s+)。
    8. 管理平面和控制平面都进行了安全性校验,不正确的连接会被 kill 掉,拒绝攻击。
    9. Server 可以增加密码校验,不允许其他的 client 注册。Server 启动时候加上 -s 参数。

    备注: 如果是公网虚拟机(阿里云或者腾讯云),记得要开放相应的端口

    Server 安装和运行

    # 下载并解压
    > wget https://github.com/longshengwang/tcpthrough-server/releases/download/v1.0/server-1.0.zip
    > unzip server-1.0.zip
    > cd server-1.0
    # 启动 server 端,会占用 3 个端口,用途分别是 控制面(默认 9000)、数据面(9009)、http 服务(8080)。可以使用 --help 查看如何设定
    > bin/server
    

    注: 详细参数可以通过 bin/server --help 来查看

    下面的例子是通过 服务器端的 333 端口来访问内网的 localhost22端口

    Client 安装和运行

    # 下载并解压
    > wget https://github.com/longshengwang/tcpthrough-client/releases/download/client-1.0/client-1.0.zip
    > unzip client-1.0.zip
    > cd client-1.0
    # 启动 client 端, 下面的字段表示:通过服务器上的 333 端口,可以访问客户端的 localhost:22 服务(这里是 ssh 服务)
    > bin/client -u my_home -s <server ip>  -p 333 -l localhost:22 -p 333 -c true -a true
    

    注: 详细参数可以通过 bin/client --help 来查看

    参数说明:

    • -c 表示 client 是否可以被 server 控制(是否可以增加被代理的服务)
    • -u 客户端的名称,也是唯一标识,server 下的 client name 不可以重复
    • -a 表示被代理的端口只可以被信任的主机访问 (如何增加被信任的主机,下面的命令行会有提到)
    • -f 表示可以把所有的参数都写入文件,然后用 -f 指向该文件 文件模板如下
    name=wls_home
    password=wo_shi_server_password
    remote_host=192.168.122.20
    remote_data_port=9009
    remote_manager_port=9000
    local_host=192.168.122.20
    local_port=22
    remote_proxy_port=2222
    is_remote_manage=true
    

    命令行

    可以通过 python 的一个库(自己写的),来查看和管理 server 上的连接信息

    ➜ ~ pip install tcpth.cmd
    ➜ ~ tcpthcmd  # 如果 server 上的 http 端口不是默认的 8080,可以后面加上 -p <port>
    Welcome to use the tcp through.
    tcpthrough> help
    tcpthrough> help
        list -- get all registration
        get <name> -- get special name information
        monitor [<name>] -- monitor the information, refresh on 2s
        register add <name> <localhost:port> <proxy port> -- add registration
        register delete <name> <proxy port> -- delete registration
        trust add <name> [<proxy port>] <trusted ip> -- add trust ip
        trust delete <name> [<proxy port>] <trusted ip> -- delete trust ip
        trust get <name> [<proxy port>] -- get trust ip
    tcpthrough>
    tcpthrough> list   # 在命令行下,列是对齐的,拷贝过来之后就不对齐了
     Name | Local Service | Proxy Port | Out Conn Count | Remote Managed | Security | Write Speed | Read Speed
    --------------------------------------------------------------------------------------------------------------
     my_home | localhost:22 | 333 | 0 | true | true | 0KB/s | 0KB/s
    tcpthrough>
    

    说明:

    • 可以通过 exit 或者 ctrl + d 或者 ctrl + c 退出命令行模式
    • 如果客户端允许服务端控制,那么可以通过 register 命令来进行添加和删除
    • 如果客户端开启安全模式 -a, 那么只有通过 trust add <your-client-name> <your-ip> 来添加信任的 IP 地址
    • monitor 是可以 每个 2s 打印一次 server 端的连接信息
    • register add 中的 localhost 可以是 client 网络中的其他主机的 IP 地址
    104 条回复    2019-12-11 16:51:11 +08:00
    1  2  
    lswang
        101
    lswang  
    OP
       2019-11-25 23:49:10 +08:00
    @easing 一个 client 可以支持多个转发
    lswang
        102
    lswang  
    OP
       2019-11-25 23:49:44 +08:00
    @wslzy007 是的,没有最好,只有最适合自己
    lswang
        103
    lswang  
    OP
       2019-11-25 23:50:38 +08:00
    @easing 一个 client 可以同时起多个
    ChangHaoWei
        104
    ChangHaoWei  
       2019-12-11 16:51:11 +08:00
    @jaybing926 sshd config GateWay yes

    。。。没有限制的!
    1  2  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5350 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:01 · PVG 16:01 · LAX 01:01 · JFK 04:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.