V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sahrechiiz
V2EX  ›  DNS

请问如何将 laod 发布的 hosts 文件转换成此项目的 rpz.zone?

  •  
  •   sahrechiiz · 2016-05-19 09:11:48 +08:00 · 3046 次点击
    这是一个创建于 2905 天前的主题,其中的信息可能已经有所发展或是发生改变。
    5 条回复    2016-05-19 21:24:07 +08:00
    dphdjy
        1
    dphdjy  
       2016-05-19 09:29:41 +08:00 via Android
    keyword: hosts2dns hosts2zone
    zyqf
        2
    zyqf  
       2016-05-19 15:28:00 +08:00
    ```
    #!/usr/bin/python
    # -*- encoding: utf-8 -*-

    import re
    import urllib2

    print 'fetching gfw hosts...'
    url = 'https://github.com/racaljk/hosts/raw/master/hosts'
    res = urllib2.urlopen(url).read()
    lines = res.splitlines(True)

    filename = 'rpz.zone'
    conf = open(filename, 'w')
    conf.write('# Dnsmasq Format\n\n\n')

    ippat = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
    dmpat = re.compile('^[a-zA-Z\d-]{,63}(\.[a-zA-Z\d-]{,63})*$')

    print 'parsing file...'
    for line in lines:
    line = line.strip()
    if line.startswith('#'):
    conf.write(line + "\n")
    else:
    if len(line) > 0:
    if line.find('localhost') < 0 and line.find('255.255.255.255') < 0:
    (ip, dm) = line.split()[:2]
    if ippat.match(ip) and dmpat.match(dm):
    conf.write(dm + ' IN A ' + ip + "\n")
    else:
    conf.write("\n")

    conf.close()
    print 'done, please use ' + filename
    ```
    sahrechiiz
        3
    sahrechiiz  
    OP
       2016-05-19 17:41:33 +08:00
    @zyqf 谢谢
    项目我 fork 了 twitter 那段改了下
    ;twitter
    twitter.com IN A 104.244.42.1
    *.twitter.com IN A 104.244.42.3
    www.twitter.com IN A 104.244.42.1
    api.twitter.com IN A 104.244.42.2
    mobile.twitter.com IN A 104.244.42.6
    support.twitter.com IN A 104.244.42.7
    upload.twitter.com IN A 104.244.42.11
    tweetdeck.twitter.com IN A 104.244.42.132
    tweetdeck.com IN A 104.244.42.132
    *.tweetdeck.com IN A 104.244.42.132
    syndication.twitter.com IN A 104.244.42.136
    syndication-o.twitter.com IN A 104.244.42.136
    cdn.syndication.twimg.com IN A 104.244.42.136
    cdn.syndication.twitter.com IN A 104.244.42.136
    cdn.api.twitter.com IN A 104.244.42.2
    preview.cdn.twitter.com IN A 104.244.42.1
    t.co IN A 104.244.42.197
    *.t.co IN A 104.244.42.69
    twitpic.com IN A 173.236.110.98
    *.twitpic.com IN A 173.236.110.98
    tinypic.com IN A 209.17.68.209
    *.tinypic.com IN A 209.17.68.209
    twimg.com IN A 104.244.43.199
    *.twimg.com IN A 104.244.43.199
    video.twimg.com IN A 104.244.43.199
    zyqf
        4
    zyqf  
       2016-05-19 21:23:29 +08:00
    @sahrechiiz 比原来的完整, pull 过来呗
    zyqf
        5
    zyqf  
       2016-05-19 21:24:07 +08:00
    @sahrechiiz 额,我自己改吧。。无视上条
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2204 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 11:16 · PVG 19:16 · LAX 04:16 · JFK 07:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.