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

关于 Clash 透明代理涉及 iptables 的一些问题

  •  
  •   seers · 2021-03-15 22:13:40 +08:00 · 5236 次点击
    这是一个创建于 1135 天前的主题,其中的信息可能已经有所发展或是发生改变。

    clash 架设成功,但是在 iptables 表出了点小问题,我用这个表可以成功透明代理

    ##### TCP #####
    
    # Bypass private IP address ranges
    iptables -t nat -N CLASH
    iptables -t nat -A CLASH -d 0.0.0.0/8 -j RETURN
    iptables -t nat -A CLASH -d 10.0.0.0/8 -j RETURN
    iptables -t nat -A CLASH -d 127.0.0.0/8 -j RETURN
    iptables -t nat -A CLASH -d 169.254.0.0/16 -j RETURN
    iptables -t nat -A CLASH -d 172.16.0.0/12 -j RETURN
    iptables -t nat -A CLASH -d 192.168.0.0/16 -j RETURN
    iptables -t nat -A CLASH -d 224.0.0.0/4 -j RETURN
    iptables -t nat -A CLASH -d 240.0.0.0/4 -j RETURN
    
    # Redirect all TCP traffic to redir port, where Clash listens
    iptables -t nat -A CLASH -p tcp -j REDIRECT --to-ports 7892
    iptables -t nat -A PREROUTING -p tcp -j CLASH
    
    ##### UDP #####
    
    # IP rules
    ip rule add fwmark 1 table 100
    ip route add local default dev lo table 100
    
    # Bypass private IP address ranges
    iptables -t mangle -N CLASH
    iptables -t mangle -A CLASH -d 0.0.0.0/8 -j RETURN
    iptables -t mangle -A CLASH -d 10.0.0.0/8 -j RETURN
    iptables -t mangle -A CLASH -d 127.0.0.0/8 -j RETURN
    iptables -t mangle -A CLASH -d 169.254.0.0/16 -j RETURN
    iptables -t mangle -A CLASH -d 172.16.0.0/12 -j RETURN
    iptables -t mangle -A CLASH -d 192.168.0.0/16 -j RETURN
    iptables -t mangle -A CLASH -d 224.0.0.0/4 -j RETURN
    iptables -t mangle -A CLASH -d 240.0.0.0/4 -j RETURN
    
    # Redirect
    iptables -t mangle -A CLASH -p udp -j TPROXY --on-port 7892 --tproxy-mark 1
    iptables -t mangle -A PREROUTING -p udp -j CLASH
    
    ##### DNS #####
    
    # Redirect 53 to 5353 
    iptables -t nat -I PREROUTING -p udp --dport 53 -d 192.168.0.0/16 -j REDIRECT --to 1053
    

    但是用这个表就不成功

    #tcp
    iptables -t nat -N clash
    iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
    iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
    iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
    iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
    iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
    iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
    iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
    iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
    iptables -t nat -A clash -p tcp -j REDIRECT --to-port "7892"
    iptables -t nat -A PREROUTING -p tcp -j clash
    
    #udp
    ip rule add fwmark 1 table 100
    ip route add local default dev lo table 100
    iptables -t mangle -N clash
    iptables -t mangle -A clash -d 0.0.0.0/8 -j RETURN
    iptables -t mangle -A clash -d 10.0.0.0/8 -j RETURN
    iptables -t mangle -A clash -d 127.0.0.0/8 -j RETURN
    iptables -t mangle -A clash -d 169.254.0.0/16 -j RETURN
    iptables -t mangle -A clash -d 172.16.0.0/12 -j RETURN
    iptables -t mangle -A clash -d 192.168.0.0/16 -j RETURN
    iptables -t mangle -A clash -d 224.0.0.0/4 -j RETURN
    iptables -t mangle -A clash -d 240.0.0.0/4 -j RETURN
    iptables -t mangle -A clash -p udp -j TPROXY --on-port "7892" --tproxy-mark 1
    iptables -t mangle -A PREROUTING -p udp -j clash
    
    iptables -t nat -N CLASH_DNS
    iptables -t nat -F CLASH_DNS 
    iptables -t nat -A CLASH_DNS -p udp -j REDIRECT --to-port 1053
    iptables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS
    iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
    

    有没有熟悉 iptables 的大佬来看看,我感觉第一个比较粗暴,把 192.168.0.0/16 的 53 口 udp 直接转发去 1053,第二个比较优雅,新建了一个 nat 表 CLASH_DNS,但是却不能运行?

    8 条回复    2021-03-16 16:11:44 +08:00
    codehz
        1
    codehz  
       2021-03-15 22:32:43 +08:00 via Android
    1202 年了,就不要用 iptables 了。。。
    这里推荐用 nf 桌子(
    https://gist.github.com/codehz/db39a6d5732ccbd6343f277b78f1eb19
    单独开个 clash 用户,然后用上述配置即可(端口可以自己修改
    des
        2
    des  
       2021-03-15 22:44:37 +08:00
    要不要试试 tun 模式?
    xarthur
        3
    xarthur  
       2021-03-15 22:51:29 +08:00 via iPhone
    @codehz OpenWRT 还没迁移到 nftable 上?
    learningman
        4
    learningman  
       2021-03-16 02:08:14 +08:00 via Android
    @xarthur 默认还是 iptables,有 nftables 支持,但是感觉会出锅
    dullwit
        5
    dullwit  
       2021-03-16 08:11:48 +08:00 via iPhone
    clash 直接接管 53 端口呗
    jasonyang9
        6
    jasonyang9  
       2021-03-16 09:10:19 +08:00
    不成功的命令:

    ```
    iptables -t nat -N CLASH_DNS
    iptables -t nat -F CLASH_DNS
    iptables -t nat -A CLASH_DNS -p udp -j REDIRECT --to-port 1053
    iptables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS
    iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053
    ```

    `iptables -t nat -I OUTPUT -p udp --dport 53 -j CLASH_DNS`

    OUTPUT 链经过的是本机发出的数据包,udp,目标端口 53,跳转到 CLASH_DNS 自定义链,修改目标端口为 1053 。
    注意:没有匹配目标地址,不一定是 192.168.0.0/16 。

    `iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to 1053`

    PREROUTING 链经过的是收到的不论是发给本机的或需要转发的数据包,udp,目标端口 53,修改目标端口为 1053 。
    注意:没有匹配目标地址,不一定是 192.168.0.0/16 。

    成功的命令:

    ```
    # Redirect 53 to 5353
    iptables -t nat -I PREROUTING -p udp --dport 53 -d 192.168.0.0/16 -j REDIRECT --to 1053
    ```

    对收到的数据包,udp,目标端口 53,目标地址 192.168.0.0/16,修改目标端口为 1053 。
    seers
        7
    seers  
    OP
       2021-03-16 14:32:48 +08:00
    @jasonyang9 大佬,意思是去掉 OUTPUT 这行语句?或者是给它加上-d 目标地址?
    jasonyang9
        8
    jasonyang9  
       2021-03-16 16:11:44 +08:00
    @seers 不是,我把自己理解的内容整理了一下方便你排错。
    而且这里将所有 UDP 数据包不论目标 IP,只要匹配到 53 口的都映射到 1053 也有些奇怪。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3270 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:00 · PVG 21:00 · LAX 06:00 · JFK 09:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.