diff --git a/genpac.py b/genpac.py index ecd91e01..0cb7d681 100755 --- a/genpac.py +++ b/genpac.py @@ -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 @@ -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; } @@ -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 ...' diff --git a/user-rules.txt b/user-rules.txt index 7d44a2bf..a9201020 100644 --- a/user-rules.txt +++ b/user-rules.txt @@ -22,8 +22,5 @@ ! 如: @@sina.com @@163.com -@@github.com -.twitter.com -||twitter.com -.youtube.com -||youtube.com \ No newline at end of file +twitter.com +youtube.com \ No newline at end of file