[求助] iptables 使用 ipset 匹配除 内网和中国的 ip 地址 怎么写?

2016-03-30 12:13:10 +08:00
 MATTHEW728960

使用 ss-redir 转发,端口 1080

ipset create chinaip hash:ip
ipset add chinaip 0.0.0.0/8
ipset add chinaip 10.0.0.0/8
ipset add chinaip 127.0.0.0/8
ipset add chinaip 169.254.0.0/16
ipset add chinaip 172.16.0.0/12
ipset add chinaip 192.168.0.0/16
ipset add chinaip 224.0.0.0/4
ipset add chinaip 240.0.0.0/4
ipset add chinaip $SERVER_addr
iptables -t nat -A PREROUTING -p tcp -m set ! --match-set chinaip dst -j REDIRECT --to-port 1080
iptables -t nat -A OUTPUT -p tcp -m set ! --match-set chinaip dst -j REDIRECT --to-port 1080

就是不知到在 dnsmasq 上怎么写,是这样吗?(应该不是吧)

ipset=/1.0.1.0/24/chinaip
ipset=/1.0.2.0/23/chinaip
...
6290 次点击
所在节点    问与答
2 条回复
tony1016
2016-03-30 13:28:40 +08:00
为什么要在 dnsmasq 里面写,用 curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > chnroute.txt 生成后导入
MATTHEW728960
2016-03-30 14:26:38 +08:00
@tony1016 我已经有 chnroute.txt 这样的文件了,但还是要匹配的中国 ip 怎样使用的问题啊

ipset create chinaip hash:ip (好像应改为 hash:net )
iptables -t nat -A PREROUTING -p tcp -m set ! --match-set chinaip dst -j REDIRECT --to-port 1080
awk '!/^$/&&!/^#/{printf("ipset=/%s/'"chinaip"'\n",$0)}' \
chnroute.txt > /etc/dnsmasq.d/ipset.conf

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/267308

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX