Skip to content

Commit

Permalink
firt_version
Browse files Browse the repository at this point in the history
  • Loading branch information
cokebar committed Feb 14, 2015
1 parent 03b0a6c commit 100b7ef
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
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
72 changes: 72 additions & 0 deletions gfwlist2dnsmasq.py
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!'
40 changes: 40 additions & 0 deletions gfwlist2dnsmasq.sh
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

0 comments on commit 100b7ef

Please sign in to comment.