@@ -3,7 +3,9 @@ let { Unknown } = require('sdk/platform/xpcom');
33let { Cc, Ci } = require ( 'chrome' ) ;
44let pS = Cc [ '@mozilla.org/network/protocol-proxy-service;1' ] . getService ( Ci . nsIProtocolProxyService ) ;
55let sp = require ( "sdk/simple-prefs" ) ;
6-
6+ let ProxyRule = require ( './proxy_rule' ) . ProxyRule ;
7+ let { Matcher, PrefixMatcher, DomainNameMatcher, TextRegexpMatcher } = require ( './matchers' ) ;
8+ let { stringStartsWith, addValueToKey } = require ( './utils' ) ;
79let { domainOf, allDomainsOf } = require ( './domain_utils' ) ;
810let getParsedRules = require ( './proxy_rules_subscription' ) . getParsedRules ;
911
@@ -13,11 +15,19 @@ const ProxyModeToConst = { auto: PROXY_AUTO, global: PROXY_GLOBAL, off: PROXY_OF
1315
1416let ProxyMode = ProxyModeToConst [ sp . prefs . proxyMode ] ;
1517
16- let parsedRules = {
18+ let subscriptionRules = {
19+ whiteList : { } ,
20+ proxyList : { }
21+ } ;
22+
23+ let userProxyRules = {
1724 whiteList : { } ,
1825 proxyList : { }
1926} ;
2027
28+ let whiteLists = [ ] ;
29+ let proxyLists = [ ] ;
30+
2131
2232sp . on ( 'proxyMode' , function ( ) {
2333 ProxyMode = ProxyModeToConst [ sp . prefs . proxyMode ] ;
@@ -49,20 +59,31 @@ function createProxyFromPrefs() {
4959}
5060
5161function matchesAnyIn ( matchersHash , uri , domains ) {
52- for ( var i = 0 ; i < domains . length ; i ++ ) {
53- let matchers = matchersHash [ domains [ i ] ] ;
54-
55- if ( matchers ) {
56- for ( var j = 0 ; j < matchers . length ; j ++ ) {
57- let matched = matchers [ j ] . match ( uri ) ;
58- if ( matched ) {
59- return true ;
62+ if ( Array . isArray ( matchersHash ) ) {
63+ for ( var i = 0 ; i < matchersHash . length ; i ++ ) {
64+ if ( matchesAnyIn ( matchersHash [ i ] , uri , domains ) ) {
65+ return true ;
66+ }
67+ } ;
68+
69+ return false ;
70+ } else {
71+ for ( var i = 0 ; i < domains . length ; i ++ ) {
72+ let matchers = matchersHash [ domains [ i ] ] ;
73+
74+ if ( matchers ) {
75+ for ( var j = 0 ; j < matchers . length ; j ++ ) {
76+ let matched = matchers [ j ] . match ( uri ) ;
77+ if ( matched ) {
78+ return true ;
79+ }
6080 }
6181 }
6282 }
83+
84+ return false ;
6385 }
6486
65- return false ;
6687}
6788
6889function applyFilter ( proxyService , uri , proxyInfo ) {
@@ -75,7 +96,7 @@ function applyFilter(proxyService, uri, proxyInfo) {
7596 if ( ProxyMode === PROXY_GLOBAL ) {
7697 return defaultProxy ;
7798 } else if ( ProxyMode === PROXY_AUTO ) {
78- if ( ! parsedRules ) {
99+ if ( ! subscriptionRules && ! userProxyRules ) {
79100 return proxyInfo ;
80101 }
81102
@@ -84,10 +105,10 @@ function applyFilter(proxyService, uri, proxyInfo) {
84105 allDomains . push ( '__default__' ) ;
85106 }
86107
87- if ( matchesAnyIn ( parsedRules . whiteList , uri , allDomains ) ) {
108+ if ( matchesAnyIn ( whiteLists , uri , allDomains ) ) {
88109 console . log ( uri . spec + ' matched whiteList' ) ;
89110 return proxyInfo ;
90- } else if ( matchesAnyIn ( parsedRules . proxyList , uri , allDomains ) ) {
111+ } else if ( matchesAnyIn ( proxyLists , uri , allDomains ) ) {
91112 console . log ( uri . spec + ' matched proxyList' ) ;
92113 return defaultProxy ;
93114 } else {
@@ -122,22 +143,57 @@ function reloadSubscription(ignoreCache, callback) {
122143 console . error ( "invalid subscription content format" ) ;
123144 }
124145
125- parsedRules = result ;
146+ subscriptionRules = result ;
147+ reloadRuleLists ( ) ;
126148
127149 if ( typeof callback === 'function' ) {
128150 callback ( ) ;
129151 }
130152
131153 // var File = require('sdk/io/file');
132154 // var f = File.open('C:\\Users\\Xenofex\\Sandbox\\javascript\\autoproxy\\autoproxyplus\\parsed_rules.json', 'w');
133- // f.write(JSON.stringify(parsedRules ));
155+ // f.write(JSON.stringify(subscriptionRules ));
134156 // f.close();
135157 } ) ;
136158
137159}
138160
161+ function reloadUserProxyRules ( ) {
162+ let allRules = ProxyRule . findAll ( ) ;
163+ console . log ( 'in reloadUserProxyRules, allRules: ' , allRules ) ;
164+
165+ let whiteList = { } , proxyList = { } ;
166+ let DEFAULT = '__default__' ;
167+
168+ allRules . forEach ( function ( rule ) {
169+ var matcher = ProxyRule . toMatcher ( rule ) ;
170+ var toList = rule . isWhitelist ? whiteList : proxyList ;
171+ if ( matcher instanceof TextRegexpMatcher ) {
172+ if ( matcher . isRegex ( ) ) {
173+ addValueToKey ( toList , DEFAULT , matcher ) ;
174+ } else {
175+ addValueToKey ( toList , domainOf ( matcher . expression ) , matcher ) ;
176+ }
177+ } else {
178+ addValueToKey ( toList , domainOf ( matcher . expression ) , matcher ) ;
179+ }
180+
181+ } ) ;
182+ console . log ( 'whiteList and proxyList generated' ) ;
183+
184+ userProxyRules = { whiteList : whiteList , proxyList : proxyList } ;
185+ reloadRuleLists ( ) ;
186+ console . log ( 'reloadUserProxyRules: ' , userProxyRules ) ;
187+ }
188+
189+ function reloadRuleLists ( ) {
190+ whiteLists = [ userProxyRules . whiteList , subscriptionRules . whiteList ] ;
191+ proxyLists = [ userProxyRules . proxyList , subscriptionRules . proxyList ] ;
192+ }
193+
139194function init ( ) {
140195 reloadSubscription ( false ) ;
196+ reloadUserProxyRules ( ) ;
141197
142198 sp . on ( 'reloadSubscription' , function ( ) {
143199 reloadSubscription ( true ) ;
@@ -151,5 +207,6 @@ function init() {
151207exports . init = init ;
152208exports . applyFilter = applyFilter ;
153209exports . defaultProxy = defaultProxy ;
154- exports . parsedRules = parsedRules ;
155- exports . reloadSubscription = reloadSubscription ;
210+ exports . subscriptionRules = subscriptionRules ;
211+ exports . reloadSubscription = reloadSubscription ;
212+ exports . reloadUserProxyRules = reloadUserProxyRules ;
0 commit comments