MATTHEW728960
V2EX  ›  问与答

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

  •  
  •   MATTHEW728960 · Mar 30, 2016 · 7021 views
    This topic created in 3709 days ago, the information mentioned may be changed or developed.

    使用 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
    ...
    
    Supplement 1  ·  Mar 30, 2016
    在 Openwrt 里使用的 ipset ,所以要在 dnsmasq.d 里写规则
    2 replies    2016-03-30 14:26:38 +08:00
    tony1016
        1
    tony1016  
       Mar 30, 2016   ❤️ 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
       Mar 30, 2016
    @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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3595 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 04:57 · PVG 12:57 · LAX 21:57 · JFK 00:57
    ♥ Do have faith in what you're doing.