跳至正文
Wener Notes

TProxy

约 2 分钟阅读
bash
#
modinfo xt_TPROXY

# Legacy IPTables
iptables -t mangle -N DIVERT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 1
iptables -t mangle -A DIVERT -j TPROXY --on-port 1080

ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100

# 重定向
iptables -t mangle -A PREROUTING -p tcp --dport 50080 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 80
iptables -t mangle -A PREROUTING -p tcp -m multiport --dport 50080 -j REDIRECR --to-port 1234

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

#
iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TPROXY --tproxy-mark 0x1/0x1 --on-port 3128

nftables

bash
# enable forwarding
cat /proc/sys/net/ipv4/ip_forward

# 创建新的路由表并设置路由规则
echo 100 mytable >> /etc/iproute2/rt_tables

# 添加路由规则,指定通过代理接口转发流量
ip rule add fwmark 1 table mytable
ip route add default via 192.168.1.100 dev eth0 table mytable
text
# 允许 IP 透明代理(TProxy)流量
table inet filter {
    chain input {
        type filter hook input priority 0; policy accept;

        # 允许到达 TProxy 的流量,通常会是代理端口(如 1080 或其他)
        ip daddr 0.0.0.0/0 tcp dport 1080 tproxy
    }
}

# 透明代理转发流量
table ip nat {
    chain prerouting {
        type nat hook prerouting priority -100; policy accept;

        # 将流量重定向到透明代理端口(如 1080)
        iifname "eth0" ip daddr 0.0.0.0/0 tcp dport {80,443} counter tproxy to :1080
    }
}

# 转发配置
table inet filter {
    chain forward {
        type filter hook forward priority 0; policy accept;

        # 允许透明代理转发
        ip protocol {tcp, udp} accept
    }
}

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • Network FAQ
    笔记 · TPROXY

    curl 一个地址的时候,返回 302 到 183.192.65.101。 · 目前无解,只能代理。 · 183.192.65.101 · 上海市反电信网络诈骗中心 · STUN · 实现 NAT 穿透、端口映射 · 实现信令能力 · 资源占用低,服务器流量很小

参考资料

其他外链6 条
最近更新commit d8b44e6编辑