Skip to content

Commit

Permalink
pac文件脚本微调;注释等
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnLynn committed Mar 1, 2013
1 parent 77104d9 commit e85a69e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 67 deletions.
112 changes: 50 additions & 62 deletions genpac.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,12 @@ def generateProxyVar():
else:
return 'SOCKS5 %s; SOCKS %s' % (host, host)

def convertListToJSArray(list):
array = ''
indent = ' '
for list_item in list:
if len(array) != 0:
array +=",\n"
array += "%s'%s'" % (indent, list_item)
if len(array) != 0:
array = "\n" + array + "\n" + indent;
return "[" + array + "]"
def convertListToJSArray(lst):
lst = filter(lambda s: isinstance(s, (str, unicode)) and len(s) > 0, lst)
array = "',\n '".join(lst)
if len(array) > 0:
array = "\n '" + array + "'\n "
return '[' + array + ']'

def parseGFWListRules():
global gfwlistContent
Expand Down Expand Up @@ -207,91 +203,82 @@ 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(directUserRegexpList),
convertListToJSArray(directUserWildcardList),
convertListToJSArray(proxyUserRegexpList),
convertListToJSArray(proxyUserWildcardList),
convertListToJSArray(directRegexpList),
convertListToJSArray(directWildcardList),
convertListToJSArray(proxyRegexpList),
convertListToJSArray(proxyWildcardList)
)
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(directUserRegexpList),
convertListToJSArray(directUserWildcardList),
convertListToJSArray(proxyUserRegexpList),
convertListToJSArray(proxyUserWildcardList),
convertListToJSArray(directRegexpList),
convertListToJSArray(directWildcardList),
convertListToJSArray(proxyRegexpList),
convertListToJSArray(proxyWildcardList)
)
return rules


def CreatePacFile(gfwlistRules, userRules):
pacContent = '''/**
* Generated by GenPAC %(ver)s
* Author: JinnLynn http://jeeker.net
* Project Page: http://jeeker.net/projects/genpac/
* GenPAC %(ver)s http://jeeker.net/projects/genpac/
* Generated: %(generated)s
* GFWList Last Modified: %(gfwmodified)s
* GFWList Last-Modified: %(gfwmodified)s
*/
function regExpMatch(url, pattern) {
try {
return new RegExp(pattern).test(url);
} catch(ex) {
return false;
}
}
// proxy
var P = "%(proxy)s";
%(rules)s
function FindProxyForURL(url, host) {
var P = "%(proxy)s";
var D = "DIRECT";
%(rules)s
var regExpMatch = function(url, pattern) {
try {
return new RegExp(pattern).test(url);
} catch(ex) {
return false;
}
};
var i = 0;
var length = 0;
length = directUserRegexpList.length;
for (i = 0; i < length; i++) {
for (i in directUserRegexpList) {
if(regExpMatch(url, directUserRegexpList[i])) return D;
}
length = directUserWildcardList.length;
for (i = 0; i < length; i++) {
for (i in directUserWildcardList) {
if (shExpMatch(url, directUserWildcardList[i])) return D;
}
length = proxyUserRegexpList.length;
for (i = 0; i < length; i++) {
for (i in proxyUserRegexpList) {
if(regExpMatch(url, proxyUserRegexpList[i])) return P;
}
length = proxyUserWildcardList.length;
for (i = 0; i < length; i++) {
for (i in proxyUserWildcardList) {
if(shExpMatch(url, proxyUserWildcardList[i])) return P;
}
length = directRegexpList.length;
for (i = 0; i < length; i++) {
for (i in directRegexpList) {
if(regExpMatch(url, directRegexpList[i])) return D;
}
length = directWildcardList.length;
for (i = 0; i < length; i++) {
for (i in directWildcardList) {
if (shExpMatch(url, directWildcardList[i])) return D;
}
length = proxyRegexpList.length;
for (i = 0; i < length; i++) {
for (i in proxyRegexpList) {
if(regExpMatch(url, proxyRegexpList[i])) return P;
}
length = proxyWildcardList.length;
for (i = 0; i < length; i++) {
for (i in proxyWildcardList) {
if(shExpMatch(url, proxyWildcardList[i])) return P;
}
Expand Down Expand Up @@ -335,6 +322,7 @@ def CreatePacFile(gfwlistRules, userRules):
if res == False:
print "GFWList获取失败,请检查相关内容是否配置正确。"
print "错误信息: %s" % errorInfo
sys.exit(1)
else:
print "GFWList[Last-Modified: %s]已获取。" % gfwlistModified
print '正在解析 GFWList Rules ...'
Expand Down
7 changes: 2 additions & 5 deletions user-rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,5 @@
! 如:
@@sina.com
@@163.com
@@github.com
.twitter.com
||twitter.com
.youtube.com
||youtube.com
twitter.com
youtube.com

0 comments on commit e85a69e

Please sign in to comment.