forked from cokebar/gfwlist2dnsmasq_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|