vasaeru

分享一个使用 iptables 中转 Xray 的教程

  •  
  •   vasaeru · Aug 12, 2024 · 1986 views
  •   You need to sign in to view this topic
    This topic created in 814 days ago, the information mentioned may be changed or developed.

    本教程适用于 Ubuntu20 系统,其他系统也可以尝试。

    因为 Xray 只使用 TCP 进行代理(虽然它支持 fullcone 的 UPD ),所以我们只需要抓发 TCP 流量即可。 代理服务器记得打开所有 TCP 端口,否则 UDP 可能会失效。

    1.开启转发功能 echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p

    2.打开 iptables NAT /sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

    3.开放指定端口 iptables -A INPUT -p tcp --dport 中转服务器端口 -j ACCEPT iptables -A OUTPUT -p tcp --sport 中转服务器端口 -j ACCEPT

    iptables -A FORWARD -o eth0 -d 目标服务器 IP -p tcp --dport 目标服务器端口 -j ACCEPT iptables -A FORWARD -i eth0 -s 目标服务器 IP -p tcp --sport 目标服务器端口 -j ACCEPT

    4.获取中转服务器本地 IP (中转服务器 eth0 的局域网 IP ,不是公网 IP ) ifconfig

    5.转发到指定服务器 iptables -t nat -A PREROUTING -d 中转服务器本地 IP -p tcp --dport 中转服务器端口 -j DNAT --to-destination 目标服务器 IP:目标服务器端口 iptables -t nat -A POSTROUTING -s 目标服务器 IP -p tcp --dport 目标服务器端口 -j SNAT --to-source 中转服务器本地 IP:中转服务器端口

    举个例子 我想通过端口号 10000 中转服务器 IP 为 113.108.81.189 ,端口号为 443 (通常默认)的代理服务器 通过 ifconfig 查询到 eth0 本地 IP 为 172.13.0.26 iptables -A INPUT -p tcp --dport 10000 -j ACCEPT iptables -A OUTPUT -p tcp --sport 10000 -j ACCEPT iptables -A FORWARD -o eth0 -d 113.108.81.189 -p tcp --dport 443 -j ACCEPT iptables -A FORWARD -i eth0 -s 113.108.81.189 -p tcp --sport 443 -j ACCEPT iptables -t nat -A PREROUTING -d 172.13.0.26 -p tcp --dport 10000 -j DNAT --to-destination 113.108.81.189:443 iptables -t nat -A POSTROUTING -s 113.108.81.189 -p tcp --dport 443 -j SNAT --to-source 172.13.0.26:10000

    保存下 iptables 的规则 iptables-save

    可以通过以下代码查看规则是否添加成功 iptables -L -n --line-number

    中转服务器的使用方法 将节点配置中最上面的地址和端口改为中转服务器的公网 IP 和刚刚设置的端口,其他的不变

    最后的最后:记得在安全组打开需要的端口,如果需要开启 fullcone 转发可以参考这篇教程: https://www.186086.xyz/Linux/Ubuntu%2020%20%E5%90%AF%E7%94%A8%20iptables%20fullconenat

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1452 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 19:43 · PVG 03:43 · LAX 11:43 · JFK 14:43
    ♥ Do have faith in what you're doing.