V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MATTHEW728960
V2EX  ›  问与答

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

  •  
  •   MATTHEW728960 · 2016-03-30 12:13:10 +08:00 · 6202 次点击
    这是一个创建于 2955 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 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
    ...
    
    第 1 条附言  ·  2016-03-30 15:42:21 +08:00
    在 Openwrt 里使用的 ipset ,所以要在 dnsmasq.d 里写规则
    2 条回复    2016-03-30 14:26:38 +08:00
    tony1016
        1
    tony1016  
       2016-03-30 13:28:40 +08:00   ❤️ 1
    为什么要在 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
        2
    MATTHEW728960  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2450 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:20 · PVG 22:20 · LAX 07:20 · JFK 10:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.