Skip to content

Commit

Permalink
支持用户自定义规则
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnLynn committed May 9, 2012
1 parent 5d89fb9 commit 5046e01
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 20 deletions.
107 changes: 87 additions & 20 deletions genpac.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# 如果你可以正常访问gfwlistUrl,可以设置为不使用代理
# gfwProxyType 0 不使用代理; 1 SOCKS4; 2 SOCKS5; 3 HTTP
gfwProxyType = 2
gfwProxyHost = '127.0.0.1'
gfwProxyHost = '127.0.0.2'
gfwProxyPort = 9527
gfwProxyUsr = None
gfwProxyPwd = None
Expand All @@ -40,11 +40,15 @@
# ********************************************************************** #

VERSION = '0.1a'
DEBUGMODE = False
DEBUGMODE = True


gfwlistContent = ''

import sys, os, base64, re

def fetchGFWList():
global gfwlistContent
import socks, socket, urllib2
if (gfwProxyType == socks.PROXY_TYPE_SOCKS4) or (gfwProxyType == socks.PROXY_TYPE_SOCKS5) or (gfwProxyType == socks.PROXY_TYPE_HTTP):
socks.setdefaultproxy(gfwProxyType, gfwProxyHost, gfwProxyPort, True, gfwProxyUsr, gfwProxyPwd)
Expand All @@ -58,10 +62,10 @@ def fetchGFWList():

try:
response = urllib2.urlopen(gfwlistUrl)
content = response.read()
gfwlistContent = response.read()
except Exception, e:
return False, e
return True, content
return True, None

def wildcardToRegexp(pattern):
pattern = re.sub(r"([\\\+\|\{\}\[\]\(\)\^\$\.\#])", r"\\\1", pattern);
Expand Down Expand Up @@ -155,26 +159,57 @@ def convertListToJSArray(list):
array = "\n" + array + "\n" + indent;
return "[" + array + "]"

def generatePacRules(gfwlist):
gfwlist = base64.decodestring(gfwlist)
def parseGFWListRules():
global gfwlistContent
gfwlist = base64.decodestring(gfwlistContent)
if DEBUGMODE:
with open('tmp/gfwlist.txt', 'w') as f:
f.write(gfwlist)

directRegexpList, directWildcardList, proxyRegexpList, proxyWildcardList = parseRuleList(gfwlist)
return parseRuleList(gfwlist)

rules = ''' var directRegexpList = %s;
def parseUserRules():
directUserRegexpList = []
directUserWildcardList = []
proxyUserRegexpList = []
proxyUserWildcardList = []
try:
with open('user-rules.txt') as f:
directUserRegexpList, directUserWildcardList, proxyUserRegexpList, proxyUserWildcardList = parseRuleList(f.read())
except Exception, e:
pass

return directUserRegexpList, directUserWildcardList, proxyUserRegexpList, proxyUserWildcardList

def generatePACRuls(userRules, gfwListRules):
directRegexpList, directWildcardList, proxyRegexpList, proxyWildcardList = parseGFWListRules()
directUserRegexpList, directUserWildcardList, proxyUserRegexpList, proxyUserWildcardList = parseUserRules()

rules = ''' //User Rules
var directUserRegexpList = %s;
var directUserWildcardList = %s;
var proxyUserRegexpList = %s;
var proxyUserWildcardList = %s;
//gfwlist Rules
var directRegexpList = %s;
var directWildcardList = %s;
var proxyRegexpList = %s;
var proxyWildcardList = %s;
''' % ( convertListToJSArray(directRegexpList),
''' % ( convertListToJSArray(directUserRegexpList),
convertListToJSArray(directUserWildcardList),
convertListToJSArray(proxyUserRegexpList),
convertListToJSArray(proxyUserWildcardList),
convertListToJSArray(directRegexpList),
convertListToJSArray(directWildcardList),
convertListToJSArray(proxyRegexpList),
convertListToJSArray(proxyWildcardList)
)
return rules

def CreatePacFile(gfwlist):

def CreatePacFile(gfwlistRules, userRules):
pacContent = '''/**
* Generated by GenPAC %(ver)s
* Author: JinnLynn http://jeeker.net
Expand All @@ -188,10 +223,33 @@ def CreatePacFile(gfwlist):
function FindProxyForURL(url, host) {
var P = "%(proxy)s";
var D = "DIRECT";
%(rules)s
var i = 0;
var length = 0;
%(rules)s
// gfwlist Rules
length = directUserRegexpList.length;
for (i = 0; i < length; i++)
{
if(regExpMatch(url, directUserRegexpList[i])) return D;
}
length = directUserWildcardList.length;
for (i = 0; i < length; i++)
{
if (shExpMatch(url, directUserWildcardList[i])) return D;
}
length = proxyUserRegexpList.length;
for (i = 0; i < length; i++)
{
if(regExpMatch(url, proxyUserRegexpList[i])) return P;
}
length = proxyUserWildcardList.length;
for (i = 0; i < length; i++)
{
if(shExpMatch(url, proxyUserWildcardList[i])) return P;
}
length = directRegexpList.length;
for (i = 0; i < length; i++)
{
Expand Down Expand Up @@ -219,9 +277,9 @@ def CreatePacFile(gfwlist):
return D;
}
'''
result = { 'ver': VERSION,
'proxy': generateProxyVar(),
'rules': generatePacRules(gfwlist),
result = { 'ver': VERSION,
'proxy': generateProxyVar(),
'rules': generatePACRuls(userRules, gfwlistRules)
}
with open(pacFile, 'w') as handle:
handle.write(pacContent % result)
Expand All @@ -238,11 +296,20 @@ def CreatePacFile(gfwlist):
os.system("rm -rf tmp/*")

print "正在获取GFWList %s ..." % gfwlistUrl
res, content = fetchGFWList()
res, errorInfo = fetchGFWList()
if res == False:
print "GFWList获取失败,请检查相关内容是否配置正确。"
print "错误信息: %s" % content
print "错误信息: %s" % errorInfo
else:
print "正在生成 %s ..." % pacFile
CreatePacFile(content)
print "一切就绪。"
print '正在解析 GFWList Rules ...'

# 无论gfwlist是否获取成功,都要解析,否则PAC文件有错,只是获取失败时解析的是空数据
gfwlistRules = parseGFWListRules()

print '正在解析 User Rules ...'
userRules = parseUserRules()

print "正在生成 %s ..." % pacFile
CreatePacFile(userRules, gfwlistRules)

print "一切就绪。"
13 changes: 13 additions & 0 deletions user-rules.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
! 用户自定义的代理规则
! 语法与gfwlist相同,即AdBlock Plus过滤规则
! 简单说明如下:
! 通配符支持,如 *.example.com/* 实际书写时可省略* 如.example.com/ 意即*.example.com/*
! 正则表达式支持,以\开始和结束, 如 \[\w]+:\/\/example.com\
! 例外规则 @@,如 @@*.example.com/* 满足@@后规则的地址不使用代理
! 匹配地址开始和结尾 |,如 |http://example.com、example.com|分别表示以http://example.com开始和以example.com结束的地址
! || 标记,如 ||example.com 则http://example.com、https://example.com、ftp://example.com等地址均满足条件
! 注释 ! 如 ! Comment
!
! 更详细说明 请访问 http://adblockplus.org/en/filters
!
!

0 comments on commit 5046e01

Please sign in to comment.