V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
helloworld12
V2EX  ›  问与答

如何用 iptables 把发向网卡 eth0 端口 8080 的数据包 转发到 localhost 的 8080 端口

  •  
  •   helloworld12 · 2018-04-04 00:58:29 +08:00 · 2285 次点击
    这是一个创建于 2208 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的目的是写小程序 然后需要 https 然后又需要本地调试

    想了的办法是 https://github.com/SteveLTN/https-portal 用这个来脱 https,

    用 https-portal 的话, 可以把请求转发到 本机的 eth0 的 8080 端口

    接着我需要本地调试: 想用 ssh 的远程端口转发

    ssh -vnNT -R 8080:127.0.0.1:8080 root@ali

    但是,貌似 ssh 的远程端口转发,只能监听远程的 127.0.0.1 网卡的端口

    (指定远程网卡 IP 都无效, 譬如这样: ssh -vnNT -R 172.17.0.1:8080:127.0.0.1:8080 root@ali)

    所以,出现了 client --> (server docker) https-portal --> (server)eth0:8080 --> (server)localhost:8080 --> (我的笔记本电脑)localhost:8080 这样的需求

    现在主要的问题是 eth0:8080 --> (server)localhost:8080 无效

    eth0 的 IP 是 172.17.0.1 我使用的命令是:

    iptables -t nat -A PREROUTING -i eth0 --dport 8080 -p tcp -j DNAT --to-destination 127.0.0.1:8080

    这一步好像错了...

    谢谢

    6 条回复    2018-04-04 10:07:04 +08:00
    helloworld12
        1
    helloworld12  
    OP
       2018-04-04 01:05:41 +08:00
    目的就是把 从本机网卡 1 eth0:8080 的数据, 转发到本机的 网卡 2 eth1:8080
    LGA1150
        2
    LGA1150  
       2018-04-04 01:30:54 +08:00 via Android   ❤️ 1
    @helloworld12 从本机发出的数据包,不经过 PREROUTING 链,应该用 OUTPUT
    iptables -t nat -I OUTPUT -d 172.17.0.1 -p tcp --dport 8080 -j REDIRECT --to-port 8080
    boyxupers
        3
    boyxupers  
       2018-04-04 02:39:36 +08:00 via iPhone
    小程序有个开发工具,可以免 https
    或 把某个 https 域名指向内网地址
    t123yh
        4
    t123yh  
       2018-04-04 07:41:27 +08:00 via Android   ❤️ 1
    ssh 转发可以监听远端 0.0.0.0,只需在远端机器 sshd_config 添加 GatewayPorts clientspecified
    kaneg
        5
    kaneg  
       2018-04-04 08:36:33 +08:00 via iPhone
    另一个简单的工具是 socat:
    socat tcp-listen:x.x.x.x:8080,reuseaddr,fork tcp-connect:localhost:8080
    其中 x.x.x.x 是 eth0 的 ip 地址
    lianxiaoyi
        6
    lianxiaoyi  
       2018-04-04 10:07:04 +08:00
    apt-get install rinetd 想转发啥就转发啥。。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2326 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:00 · PVG 09:00 · LAX 18:00 · JFK 21:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.