|

楼主 |
发表于 2023-11-8 18:01
|
显示全部楼层
- 写在最前面的说明:
- 我本来是想直接用路由器科学的,结果路由器因为闪存容量不足,失败了
- 然后我就用虚拟机装了个openwrt继续研究
- 然后发现自己建立一个table的想法,需要在重启以后手动执行命令来激活自定义的表
- nft -f /root/nftables.d/my_table.nft
- 把上面这条语句放到开机脚本就行了
- 目前遇到的坑
- nftset 如果同时匹配src和dst会出问题
- 我估计可能是bug
- 然后还遇到一个问题就是ssht-go不能在开机自动执行,甚至我TM感觉这家伙是不是被招安了
- 算了, 路由器刷了openwrt还是老老实实的当AP吧,甚至我感觉如果纯当AP用根本就不需要刷op
- 以后不弄了,老老实实的用iptables和ipset来实现科学吧
- 下面的东西慢慢找吧,有好的有坏的 我以后不研究nftables了
- 研究把R6220改为可以科学
- 有个缺点,最新版的op在这个路由器上只有17M的可用空间了 这128M的闪存 不知道怎么分配的.浪费了好多
- 安装完整的dnsmasq
- opkg remove dnsmasq && opkg install dnsmasq-full
- #只能用这种方式 软链接单个文件的方式不行 然后没法跨分区进行硬链接 把下面3行放在一个脚本文件里面,然后脚本文件开机的时候执行一次 就可以实现dnsmasq额外使用自定义的conf
- rm -rf /tmp/dnsmasq.d
- ln -s /root/dnsmasq/dnsmasq.d /tmp/
- service dnsmasq restart
- 安装trojan-go
- opkg install trojan-go
- 使用的话2条命令
- trojan-go -config /root/config-nat.json 2>&1 &
- trojan-go -config /root/config-forward.json 2>&1 &
- 安装v2ray
- opkg install v2ray-core
- v2ray -c /root/v2ray/v2ray-linux-64/v2ray-client.json 2>&1 &
- 不知道为什么,在openwrt上这个运行不起来 算了,不用了
- #使用自定义的nftables防火墙
- 创建table
- nft add table inet my_table
- 查看table
- nft list table inet my_table
- 创建链
- nft add chain inet my_table prerouting { type nat hook prerouting priority dstnat \; policy accept \;}
- 删除链
- nft delete chain inet my_table prerouting
- 查看链
- nft list chain inet my_table prerouting
- nft list chain inet fw4 dstnat
- 端口重定向
- nft add rule inet my_table prerouting tcp dport 1800 redirect to 1801
- 删除重定向
- 先查看handle值
- nft --handle list chain inet my_table prerouting
- 然后根据handle删除
- nft delete rule inet my_table prerouting handle 8
- 查看所有nftables的规则
- nft list ruleset
- iptables的转发
- #本地都直接return
- iptables -t nat -I PREROUTING 1 -i eth0 -p tcp -m set --match-set BENDI dst -j RETURN -m comment --comment "第1条内网"
- iptables-translate -t nat -I PREROUTING 1 -i eth0 -p tcp -m set --match-set BENDI dst -j RETURN -m comment --comment "第1条内网" #这里有ipset 翻译不成功
- iptables-translate -A PREROUTING -i eth0 -d 192.168.1.0/24 -m comment --comment "第1条内网" -j RETURN #把ipset的部分去掉翻译就成功了
- 下面是翻译完毕的
- nft add rule inet my_table prerouting iifname "br-lan" ip daddr @BENDI counter return comment "第1条内网" #这条成功添加了===============================================================
- nft add rule inet fw4 dstnat iifname "br-lan" ip daddr @BENDI counter return comment "第1条内网"
- iifname ens3 tcp dport { 80, 443 }
- #dnsmasq的走ssht
- iptables -t nat -I PREROUTING 2 -i eth0 -m set --match-set WOZIJI src -p tcp -m set --match-set DNSMASQ dst -j REDIRECT --to-ports 1081 -m comment --comment "第2条谷歌走SSHT 源IP是WOZIJI"
- iptables-translate -t nat -I PREROUTING 2 -i eth0 -m set --match-set WOZIJI src -p tcp -m set --match-set DNSMASQ dst -j REDIRECT --to-ports 1081 -m comment --comment "第2条谷歌走SSHT 源IP是WOZIJI"
- nft # -t nat -I PREROUTING 2 -i eth0 -m set --match-set WOZIJI src -p tcp -m set --match-set DNSMASQ dst -j REDIRECT --to-ports 1081 -m comment --comment 第2条谷歌走SSHT 源IP是WOZIJI
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr @WOZIJI ip daddr @DNSMASQ counter redirect to 1801 comment "第2条谷歌走SSHT 源IP是WOZIJI" #这条成功添加了===============================================================
- nft add rule inet my_table prerouting iifname "br-lan" [ip protocol tcp ip][这里必须指定协议,如果需要udp的话需要另外写一条,其实貌似根本不会有udp吧] saddr @WOZIJI ip daddr @DNSMASQ counter redirect to 1801 comment "第2条谷歌走SSHT 源IP是WOZIJI"
- ############
- 为什么这里这一条没有生效?
- 试试不要目的看看
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip daddr @DNSMASQ counter redirect to 1801 comment "第2条谷歌走SSHT 源IP是WOZIJI"
- 不行
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr @WOZIJI counter redirect to 1801 comment "第2条谷歌走SSHT 源IP是WOZIJI"
- 不行
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr 192.168.1.179/32 counter redirect to :1081 comment "第3条180全局走香港,用于需要全局科学的情况"
- 可以
- 试试SRC不用set
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr 192.168.1.179/32 ip daddr @DNSMASQ counter redirect to :1081 comment "试试SRC不用set"
- 可以
- 卧槽,我估计是防火墙抽风,上面的几条实验结果作废,刚刚恢复正常速度了 使用第一条可以了
- ############
- nft add rule inet fw4 dstnat iifname "br-lan" ip protocol tcp ip daddr @DNSMASQ counter redirect to 1801 comment "第2条谷歌走SSHT 源IP是WOZIJI"
- 不知道为什么这个提示Error: Could not process rule: Not supported
- 哦,懂了,默认的这条链 priority是filter 想要这样做必须是dstnat才行
- #这个是用于我自己玩游戏的时候走腾讯云方便加速的 先临时用一个重复的
- #iptables -t nat -I PREROUTING 3 -i eth0 -s 192.168.99.0/24 -p tcp -j RETURN -m comment --comment "第3条我自己的全局腾讯云加速"
- iptables -t nat -I PREROUTING 3 -i eth0 -s 192.168.11.180 -p tcp -j REDIRECT --to-ports 1081 -m comment --comment "forcebindIP的走香港"
- -A PREROUTING -s 192.168.11.180/32 -i eth0 -p tcp -m comment --comment "forcebindIP的走香港" -j REDIRECT --to-ports 1081
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr 192.168.1.180/32 counter redirect to :1081 comment "第3条180全局走香港,用于需要全局科学的情况" #这条成功添加了===============================================================
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr @WOZIJI redirect to :1082 #这条成功添加了===============================================================
- 这里的counter参数表示显示数据包数量
- comment WOZIJI全局走香港
- #所有已知国内IP都走国内
- iptables -t nat -I PREROUTING 4 -i eth0 -p tcp -m set --match-set GUONEI dst -j RETURN -m comment --comment "第4条已知的国内IP都直接return"
- nft add rule inet my_table prerouting iifname "br-lan" ip daddr @GUONEI counter return comment "第4条已知的国内IP都直接return" #这条成功添加了===============================================================
- #剩下的IP全部走腾讯轻量(一般剩下的都是没有被gfw屏蔽,但是IP是国外的,用腾讯的服务器可以加速访问)
- iptables -t nat -I PREROUTING 5 -i eth0 -m set --match-set WOZIJI src -p tcp -j REDIRECT --to-ports 1082 -m comment --comment "第5条剩余的没有屏蔽的国外IP走腾讯云加速 源IP是WOZIJI"
- nft add rule inet my_table prerouting iifname "br-lan" ip protocol tcp ip saddr @WOZIJI counter redirect to :1082 comment "第5条剩余的没有屏蔽的国外IP走腾讯云加速 源IP是WOZIJI" #这条成功添加了===============================================================
- #我自己临时走腾讯
- #iptables -t nat -I PREROUTING 2 -i eth0 -s 192.168.10.179 -p tcp -j REDIRECT --to-ports 1082
- #iptables -t nat -I PREROUTING 6 -i eth0 -s 192.168.10.174 -p tcp -j REDIRECT --to-ports 1082
- #IPV6相关的
- ip6tables -t nat -I PREROUTING 1 -i eth0 -p tcp -m set --match-set DNSMASQ6 dst -j REDIRECT --to-ports 1081
- shijian=`date`
- echo $shijian >> /tmpfs/trojan-go.txt
- #IPV6的转发
- ip6tables -t nat -I POSTROUTING 1 -j MASQUERADE
- 可以直接用
- iptables-translate这个命令把iptables规则转换为nftables
- 参考这里
- https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/configuring_and_managing_networking/getting-started-with-nftables_configuring-and-managing-networking#ref_comparison-of-common-iptables-and-nftables-commands_assembly_migrating-from-iptables-to-nftables
- 最后保存
- nft list table inet my_table >/etc/nftables.d/my_table.nft
- 查看fw4的prerouting 这里结果有些没有显示是哪个表 等我重启路由器再看看
- nft list ruleset |grep prerouting
- 得到结果如下
- table inet my_table{
- chain prerouting {
- type filter hook prerouting priority filter; policy accept;
- type nat hook prerouting priority dstnat; policy accept;
- chain raw_prerouting {
- type filter hook prerouting priority raw; policy accept;
- chain mangle_prerouting {
- type filter hook prerouting priority mangle; policy accept;
- }
- 这里是NAT相关的教程
- https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/securing_networks/configuring-nat-using-nftables_getting-started-with-nftables
- #使用nftset
- 创建
- nft add set inet my_table WOZIJI { type ipv4_addr \; } #对应ipset create WOZIJI hash:ip
- nft add set inet my_table DNSMASQ { type ipv4_addr \; } #对应ipset create DNSMASQ hash:ip
- nft add set inet my_table DNSMASQ6 { type ipv6_addr \; } #对应ipset create DNSMASQ6 hash:ip family inet6
- nft add set inet my_table GUONEI { type ipv4_addr \; flags interval \; } #对应ipset create GUONEI hash:net
- nft add set inet my_table BENDI { type ipv4_addr \; flags interval \; }
- 删除
- nft delete set inet my_table GUONEI
- nft delete set inet my_table BENDI
- 增加
- nft add element inet my_table WOZIJI { 192.168.11.179, 192.168.11.157 }
- nft add element inet my_table WOZIJI { 192.168.1.179 }
- nft add element inet my_table BENDI { 172.16.0.0/12 }
- 查看
- nft list sets
- 单个set查看
- nft list set inet my_table GUONEI
- nft list set inet my_table BENDI
- nft list set inet my_table WOZIJI
- nft list set inet my_table DNSMASQ
- 这里的教程可以看看
- https://cloud.tencent.com/developer/article/1585873
- 创建
- nft add set inet fw4 WOZIJI { type ipv4_addr \; } #对应ipset create WOZIJI hash:ip
- nft add set inet fw4 DNSMASQ { type ipv4_addr \; } #对应ipset create DNSMASQ hash:ip
- nft add set inet fw4 DNSMASQ6 { type ipv6_addr \; } #对应ipset create DNSMASQ6 hash:ip family inet6
- nft add set inet fw4 GUONEI { type ipv4_addr \; flags interval \; } #对应ipset create GUONEI hash:net
- nft add set inet fw4 BENDI { type ipv4_addr \; flags interval \; }
- 删除
- nft delete set inet fw4 GUONEI
- nft delete set inet fw4 BENDI
- 增加
- nft add element inet fw4 WOZIJI { 192.168.11.179, 192.168.11.157 }
- nft add element inet fw4 WOZIJI { 192.168.1.179 }
- nft add element inet fw4 BENDI { 172.16.0.0/12 }
- 查看
- nft list sets
- 单个set查看
- nft list set inet fw4 GUONEI
- nft list set inet fw4 BENDI
- 转发
- #iptables -t nat -I PREROUTING 1 -i eth0 -p tcp -m set --match-set BENDI dst -j RETURN -m comment --comment "第1条内网"
- nft list ruleset |grep prerouting
- 得到结果如下
- table inet fw4{
- chain prerouting {
- type filter hook prerouting priority filter; policy accept;
- type nat hook prerouting priority dstnat; policy accept;
- chain raw_prerouting {
- type filter hook prerouting priority raw; policy accept;
- chain mangle_prerouting {
- type filter hook prerouting priority mangle; policy accept;
- }
- #dnsmasq里面解析自动进入nftset
- nftset=/first.allowed.urls/4#inet#fw4#DNSMASQ,6#inet#fw4#DNSMASQ6
- iptables-restore-translate -f /root/iptables.dump > /root/ruleset-migrated-from-iptables.nft
- iptables-translate -A PREROUTING -i eth0 -d 192.168.1.0/24 -m comment --comment "第1条内网" -j RETURN
复制代码
|
|