zfl9 / ipt2socks

将 iptables/nftables 传入的透明代理流量转为 socks5 流量的实用工具
GNU Affero General Public License v3.0
411 stars 94 forks source link

请问有可以参考的iptables规则吗 #54

Open qq71680264 opened 1 week ago

qq71680264 commented 1 week ago

对于小白来说,readme中提供的iptables规则看起来完全无从下手,我尝试了将里面的规则拷贝出来,并导入到iptables中,但出现了问题,网络无法正常使用了。下面是我的iptables规则:

iptables -t mangle -N SSREDIR

iptables -t mangle -A SSREDIR -j CONNMARK --restore-mark
iptables -t mangle -A SSREDIR -m mark --mark 0x2333 -j RETURN

iptables -t mangle -A SSREDIR -p tcp -d 209.250.18.134 --dport 8085 -j RETURN
iptables -t mangle -A SSREDIR -p udp -d 209.250.18.134 --dport 8085 -j RETURN

iptables -t mangle -A SSREDIR -d 0.0.0.0/8          -j RETURN
iptables -t mangle -A SSREDIR -d 10.0.0.0/8         -j RETURN
iptables -t mangle -A SSREDIR -d 100.64.0.0/10      -j RETURN
iptables -t mangle -A SSREDIR -d 127.0.0.0/8        -j RETURN
iptables -t mangle -A SSREDIR -d 169.254.0.0/16     -j RETURN
iptables -t mangle -A SSREDIR -d 172.16.0.0/12      -j RETURN
iptables -t mangle -A SSREDIR -d 192.0.0.0/24       -j RETURN
iptables -t mangle -A SSREDIR -d 192.0.2.0/24       -j RETURN
iptables -t mangle -A SSREDIR -d 192.88.99.0/24     -j RETURN
iptables -t mangle -A SSREDIR -d 192.168.0.0/16     -j RETURN
iptables -t mangle -A SSREDIR -d 198.18.0.0/15      -j RETURN
iptables -t mangle -A SSREDIR -d 198.51.100.0/24    -j RETURN
iptables -t mangle -A SSREDIR -d 203.0.113.0/24     -j RETURN
iptables -t mangle -A SSREDIR -d 224.0.0.0/4        -j RETURN
iptables -t mangle -A SSREDIR -d 240.0.0.0/4        -j RETURN
iptables -t mangle -A SSREDIR -d 255.255.255.255/32 -j RETURN

iptables -t mangle -A SSREDIR -p tcp --syn                      -j MARK --set-mark 0x2333
iptables -t mangle -A SSREDIR -p udp -m conntrack --ctstate NEW -j MARK --set-mark 0x2333

iptables -t mangle -A SSREDIR -j CONNMARK --save-mark

iptables -t mangle -A OUTPUT -p tcp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
iptables -t mangle -A OUTPUT -p udp -m addrtype --src-type LOCAL ! --dst-type LOCAL -j SSREDIR

iptables -t mangle -A PREROUTING -p tcp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR
iptables -t mangle -A PREROUTING -p udp -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -j SSREDIR

iptables -t mangle -A PREROUTING -p tcp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 12345
iptables -t mangle -A PREROUTING -p udp -m mark --mark 0x2333 -j TPROXY --on-ip 127.0.0.1 --on-port 10053

ip route add local default dev lo table 100
ip rule  add fwmark 0x2333        table 100

有哪位大佬可以帮我看看是哪里出问题了或者提供下简单的例子吗?

zfl9 commented 1 week ago

建议用 ss-tproxy 脚本,或者你可以说下你的需求:全局代理?还是gfwlist分流?还是chnroute分流

qq71680264 commented 1 week ago

建议用 ss-tproxy 脚本,或者你可以说下你的需求:全局代理?还是gfwlist分流?还是chnroute分流

非常感谢你的回复 我想在VPS上实现全局的透明代理,将所有端口上的TCP以及UDP流量都转发到socks5 服务 请问这种情况需要如何实现?

zfl9 commented 1 week ago

TCP好搞定,UDP的话,要设置TPROXY规则,想省事的话还是上 ss-tproxy 脚本,global模式,selfonly=true

qq71680264 commented 1 week ago

TCP好搞定,UDP的话,要设置TPROXY规则,想省事的话还是上 ss-tproxy 脚本,global模式,selfonly=true

但使用ss-proxy脚本的话,是不是对入站的流量有要求,需要配置客户端(想科学上网的手机或电脑),例如小火箭,v2rayN等。并不能直接处理所有接收到的流量?这是我看了ss-tproxy的README以后,对代理套件配置一栏的理解,如果我的理解有误,请告诉我,再次感谢你回复。你的回复对我有很大的帮助!

zfl9 commented 1 week ago

你只需要将 “代理套件” 替换为 ipt2socks + 上游的socks代理,就能理解了。 另外,在 VPS 上运行并没有什么特别之处,无非就是只代理 VPS 本机罢了,也即 selfonly='true'

zfl9 commented 1 week ago

如果我没理解错的话,你是想代理 VPS 本机传出的流量,将其导入至指定的 socks5 服务器?

我想在VPS上实现全局的透明代理,将所有端口上的TCP以及UDP流量都转发到socks5 服务

qq71680264 commented 1 week ago

如果我没理解错的话,你是想代理 VPS 本机传出的流量,将其导入至指定的 socks5 服务器?

我想在VPS上实现全局的透明代理,将所有端口上的TCP以及UDP流量都转发到socks5 服务

是的,我确实是想要这么做,将VPS上所有流量都导入到指定的socks5服务器。

zfl9 commented 1 week ago

那就没问题,ss-tproxy + mode=global + selfonly=true 代理相关的配置,可以参考【trojan(socks5)】,只不过把trojan去掉,配置ipt2socks就行

qq71680264 commented 6 days ago

那就没问题,ss-tproxy + mode=global + selfonly=true 代理相关的配置,可以参考【trojan(socks5)】,只不过把trojan去掉,配置ipt2socks就行

我已按照你所说的步骤执行,但不清楚为什么ss-tproxy无法获取到我在ipt2proxy中的设置,下面是我可以提供的信息,我是否遗漏了什么步骤?感谢你的回复,让我有信心解决所遇到任何的问题。 e3 e5 r3

zfl9 commented 6 days ago

-s和-p等参数需要在kakami函数中设置,传递给ipt2socks命令

qq71680264 commented 6 days ago

-s和-p等参数需要在kakami函数中设置,传递给ipt2socks命令

我想我已经快要成功了,但目前还有一点点 问题需要解决,你有什么好的建议吗?感谢你的回复! e6

zfl9 commented 6 days ago

因为你的ipt2socks启动命令不正确,缺少重定向,具体参考readme的,也可以对比下上面的trojan启动行,就明白了。

另外,curl失败时,ipt2socks已经给出了错误原因,具体要查看socks服务器那边咋回事

qq71680264 commented 6 days ago

我已经将日志重定向,但我开启ss-tproxy后使用curl时,似乎总是不成功,我查看了日志上的信息,得到的错误和昨晚看到的相同,都是host unreachable,不知道要如何进行下一步,可以请你帮我分析一下吗?感谢你的回复! er1 er2

zfl9 commented 6 days ago

问题已经不在 ss-tproxy、ipt2socks 这边了,错误 log 的意思是: socks5 服务器无法访问 ifconfig.me,错误码是:Host unreachable。

因此你需要去 socks5 服务器上看看为什么(看socks5服务器的log); 另外,可以试试访问其他网站,curl baidu.com、curl google.com 还有就是,调试期间,先把 ipt2socks 详细日志打开(ss-tproxy.conf中,给ipt2socks命令行加上 -v 参数)

zfl9 commented 6 days ago

为了排除 socks5 服务器问题,先 ss-tproxy stop,执行以下命令:

# 告诉 curl 使用给定的 socks5 代理,将 IP:PORT 改为正确的地址。把结果发出来看看。
curl -x socks5h://IP:PORT ifconfig.me -vv
qq71680264 commented 6 days ago

为了排除 socks5 服务器问题,先 ss-tproxy stop,执行以下命令:

# 告诉 curl 使用给定的 socks5 代理,将 IP:PORT 改为正确的地址。把结果发出来看看。
curl -x socks5h://IP:PORT ifconfig.me -vv

er3 另外,我尝试了将tproxy改为true,即纯TPROXY模式,在该模式下,可以返回socks5服务器IP,但大概需要15秒,在这个基础上,我再将tcponly改为true,再尝试使用curl访问ifconfig.me,可以迅速返回socks5服务器IP。不知道这是否有助于你的分析。感谢你的回复!

zfl9 commented 6 days ago
#执行以下命令,输出发一下
ss-tproxy stop
curl -x socks5h://user:pwd@ip:port ifconfig.me -4 -vv
curl -x socks5h://user:pwd@ip:port ifconfig.me -6 -vv
zfl9 commented 6 days ago

在这个基础上,我再将tcponly改为true,再尝试使用curl访问ifconfig.me,可以迅速返回socks5服务器IP

如果是这样,说明你的socks5服务器不支持 UDP 代理,这种情况下,必须在 ss-tproxy.conf 中配置 tcponly=true

qq71680264 commented 6 days ago
#执行以下命令,输出发一下
ss-tproxy stop
curl -x socks5h://user:pwd@ip:port ifconfig.me -4 -vv
curl -x socks5h://user:pwd@ip:port ifconfig.me -6 -vv

er4

zfl9 commented 6 days ago

看起来 socks5 没问题,只是不支持 UDP 代理,使用 tcponly=true 模式吧。

qq71680264 commented 6 days ago

看起来 socks5 没问题,只是不支持 UDP 代理,使用 tcponly=true 模式吧。

看来我是被socks5的服务商骗了,他写着支持UDP,实际上却无法实现,我换一家再试试。感谢你如此耐心的回复,我学习到了很多,真心非常感谢!