From 100b7ef11a77a1bbad648ca2429e3028155d2630 Mon Sep 17 00:00:00 2001 From: cokebar Date: Sat, 14 Feb 2015 17:19:36 +0800 Subject: [PATCH] firt_version --- README.md | 31 ++++++++++++++++++++ gfwlist2dnsmasq.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++ gfwlist2dnsmasq.sh | 40 ++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 gfwlist2dnsmasq.py create mode 100755 gfwlist2dnsmasq.sh diff --git a/README.md b/README.md index a9c24b8..67ab06f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,33 @@ # gfwlist2dnsmasq Just another script to auto-generate dnsmasq ipset rules using gfwlist + +Using: + +git clone & modify gfwlist2dnsmasq.sh: + +# Change this to your DNS server +DNS='127.0.0.1#5353' + +# Change this to your ipset name +IPSET=gfwlist + +# Path to save you rule file +RULE_FILE=./dnsmasq_list.conf + +# Add your own extra domain here. One domain in a line. eg: +EX_DOMAIN='.google.com +.google.com.hk +.google.com.tw +.google.com.sg +.google.co.jp +.blogspot.com +.blogspot.sg +.blogspot.hk +.blogspot.jp +.gvt1.com +.gvt2.com +.gvt3.com +.1e100.net +.blogspot.tw' + +And then just cd to the directory and run gfwlist2dnsmasq.sh \ No newline at end of file diff --git a/gfwlist2dnsmasq.py b/gfwlist2dnsmasq.py new file mode 100644 index 0000000..d2f6f03 --- /dev/null +++ b/gfwlist2dnsmasq.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +#coding=utf-8 +# +# Generate a list of dnsmasq rules with ipset for gfwlist +# +# Copyright (C) 2014 http://www.shuyz.com +# Ref https://code.google.com/p/autoproxy-gfwlist/wiki/Rules + +import urllib2 +import re +import os +import datetime +import base64 +import shutil + +mydnsip = '127.0.0.1' +mydnsport = '5353' + +# the url of gfwlist +baseurl = 'https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt' +# match comments/title/whitelist/ip address +comment_pattern = '^\!|\[|^@@|^\d+\.\d+\.\d+\.\d+' +domain_pattern = '([\w\-\_]+\.[\w\.\-\_]+)[\/\*]*' +tmpfile = '/tmp/gfwlisttmp' +# do not write to router internal flash directly +outfile = '/tmp/gfwlist.conf' +rulesfile = '/var/www/wordpress/wp-content/uploads/secured_files/dnsmasq_list.conf' + +fs = file(outfile, 'w') +fs.write('# gfw list ipset rules for dnsmasq\n') +fs.write('# updated on ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n') +fs.write('#\n') + +print 'fetching list...' +content = urllib2.urlopen(baseurl, timeout=15).read().decode('base64') + +# write the decoded content to file then read line by line +tfs = open(tmpfile, 'w') +tfs.write(content) +tfs.close() +tfs = open(tmpfile, 'r') + +print 'page content fetched, analysis...' + +# remember all blocked domains, in case of duplicate records +domainlist = [] + +for line in tfs.readlines(): + if re.findall(comment_pattern, line): + print 'this is a comment line: ' + line + #fs.write('#' + line) + else: + domain = re.findall(domain_pattern, line) + if domain: + try: + found = domainlist.index(domain[0]) + print domain[0] + ' exists.' + except ValueError: + print 'saving ' + domain[0] + domainlist.append(domain[0]) + fs.write('server=/.%s/%s#%s\n'%(domain[0],mydnsip,mydnsport)) + fs.write('ipset=/.%s/gfwlist\n'%domain[0]) + else: + print 'no valid domain in this line: ' + line + +tfs.close() +fs.close(); + +print 'moving generated file to dnsmasg directory' +shutil.move(outfile, rulesfile) + +print 'done!' \ No newline at end of file diff --git a/gfwlist2dnsmasq.sh b/gfwlist2dnsmasq.sh new file mode 100755 index 0000000..47be6ca --- /dev/null +++ b/gfwlist2dnsmasq.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Change this to your DNS server +DNS='127.0.0.1#5353' + +# Change this to your ipset name +IPSET=gfwlist + +# Path to save you rule file +RULE_FILE=./dnsmasq_list.conf + +# Add your own extra domain here. One domain in a line. +EX_DOMAIN='.google.com +.google.com.hk +.google.com.tw +.google.com.sg +.google.co.jp +.blogspot.com +.blogspot.sg +.blogspot.hk +.blogspot.jp +.gvt1.com +.gvt2.com +.gvt3.com +.1e100.net +.blogspot.tw' + + +python ./gfwlist2dnsmasq.py + +echo '#extra domains' >> $RULE_FILE + +for each_line in $EX_DOMAIN +do + echo 'server=/'$each_line'/'$DNS >> $RULE_FILE + echo 'ipset=/'$each_line'/'$IPSET >> $RULE_FILE +done + +chown www-data:www-data $RULE_FILE +