Skip to content

Instantly share code, notes, and snippets.

@upbit
Created August 28, 2014 13:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upbit/ae4724aeb302749fb1f0 to your computer and use it in GitHub Desktop.
Save upbit/ae4724aeb302749fb1f0 to your computer and use it in GitHub Desktop.
将hosts中的网站,转为dnsmasq的conf规则
# -*- coding: utf-8 -*-
HOSTS = """
# 将gfw hosts内容粘贴到这里
"""
BLACK_LIST = """com.co.jp
akamaihd.net
angrybirds.com
edgesuite.net
fastly.net
googlezip.net
keyhole.com
projecth.us
wikibooks.org
wikidata.org
wikimediafoundation.org
wikiquote.org
wikisource.org
wikiversity.org
wikivoyage.org
wiktionary.org
yahoo.com
"""
DNSMASQ_TEMP = "server=/.%s/192.168.1.1#5353"
def main():
blocked_domain = set()
for line in HOSTS.split("\n"):
if line.strip() == "": continue
if line[0]=="#": continue
domain_list = line.split("\t")[1].split(".")
if (domain_list[-2] != 'com') and (domain_list[-2] != 'co'):
main_domain = ".".join(domain_list[-2:])
else:
main_domain = ".".join(domain_list[-3:])
blocked_domain.add(main_domain)
for domain in sorted(blocked_domain):
if domain in BLACK_LIST.split("\n"): continue
print DNSMASQ_TEMP % domain
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment