Skip to content

Commit 398f25e

Browse files
committed
Reordering rules checks based on their popularity
1 parent 7efc52b commit 398f25e

3 files changed

Lines changed: 123 additions & 112 deletions

File tree

src/core/ProxyEngineChrome.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,45 @@ function findMatchedUrlInRules(searchUrl, host, hostAndPort, rules) {
239239
240240
break;
241241
242-
case CompiledProxyRuleType.Exact:
242+
case CompiledProxyRuleType.SearchDomainSubdomainAndPath:
243+
{
244+
schemaLessUrlLowerCase ??= removeSchemaFromUrl(lowerCaseUrl);
245+
if (schemaLessUrlLowerCase == null)
246+
continue;
243247
244-
if (lowerCaseUrl == rule.search)
248+
if (schemaLessUrlLowerCase.startsWith(rule.search))
249+
return rule;
250+
251+
rule.searchHost ??= extractHostFromInvalidUrl(rule.search);
252+
if (rule.searchHost != null) {
253+
254+
// should be the same
255+
if (rule.searchHost != host && !host.endsWith('.' + rule.searchHost))
256+
continue;
257+
258+
// after this state, we are sure that the url is for the same domain, now just checking the path
259+
}
260+
261+
// subdomains
262+
if (schemaLessUrlLowerCase.includes('.' + rule.search))
263+
return rule;
264+
break;
265+
}
266+
267+
case CompiledProxyRuleType.SearchDomainAndPath:
268+
269+
schemaLessUrlLowerCase ??= removeSchemaFromUrl(lowerCaseUrl);
270+
if (schemaLessUrlLowerCase == null)
271+
continue;
272+
273+
if (schemaLessUrlLowerCase.startsWith(rule.search))
245274
return rule;
275+
246276
break;
247277
248-
case CompiledProxyRuleType.RegexHost:
249-
250-
if (rule.regex.test(host))
278+
case CompiledProxyRuleType.SearchUrl:
279+
280+
if (lowerCaseUrl.startsWith(rule.search))
251281
return rule;
252282
break;
253283
@@ -257,9 +287,9 @@ function findMatchedUrlInRules(searchUrl, host, hostAndPort, rules) {
257287
return rule;
258288
break;
259289
260-
case CompiledProxyRuleType.SearchUrl:
261-
262-
if (lowerCaseUrl.startsWith(rule.search))
290+
case CompiledProxyRuleType.RegexHost:
291+
292+
if (rule.regex.test(host))
263293
return rule;
264294
break;
265295
@@ -269,41 +299,11 @@ function findMatchedUrlInRules(searchUrl, host, hostAndPort, rules) {
269299
return rule;
270300
break;
271301
272-
case CompiledProxyRuleType.SearchDomainAndPath:
273-
274-
schemaLessUrlLowerCase ??= removeSchemaFromUrl(lowerCaseUrl);
275-
if (schemaLessUrlLowerCase == null)
276-
continue;
302+
case CompiledProxyRuleType.Exact:
277303
278-
if (schemaLessUrlLowerCase.startsWith(rule.search))
304+
if (lowerCaseUrl == rule.search)
279305
return rule;
280-
281306
break;
282-
283-
case CompiledProxyRuleType.SearchDomainSubdomainAndPath:
284-
{
285-
schemaLessUrlLowerCase ??= removeSchemaFromUrl(lowerCaseUrl);
286-
if (schemaLessUrlLowerCase == null)
287-
continue;
288-
289-
if (schemaLessUrlLowerCase.startsWith(rule.search))
290-
return rule;
291-
292-
rule.searchHost ??= extractHostFromInvalidUrl(rule.search);
293-
if (rule.searchHost != null) {
294-
295-
// should be the same
296-
if (rule.searchHost != host && !host.endsWith('.' + rule.searchHost))
297-
continue;
298-
299-
// after this state, we are sure that the url is for the same domain, now just checking the path
300-
}
301-
302-
// subdomains
303-
if (schemaLessUrlLowerCase.includes('.' + rule.search))
304-
return rule;
305-
break;
306-
}
307307
}
308308
}
309309
@@ -317,18 +317,6 @@ function findMatchedUrlInRules(searchUrl, host, hostAndPort, rules) {
317317
// NOTE: Only rules that work on hostName should be checked, others can be ignored
318318
switch (rule.compiledRuleType) {
319319
320-
case CompiledProxyRuleType.RegexHost:
321-
322-
if (rule.regex.test(host))
323-
return rule;
324-
break;
325-
326-
case CompiledProxyRuleType.SearchDomain:
327-
328-
if (rule.search == host)
329-
return rule;
330-
break;
331-
332320
case CompiledProxyRuleType.SearchDomainSubdomain:
333321
334322
// domain
@@ -366,6 +354,18 @@ function findMatchedUrlInRules(searchUrl, host, hostAndPort, rules) {
366354
break;
367355
}
368356
357+
case CompiledProxyRuleType.RegexHost:
358+
359+
if (rule.regex.test(host))
360+
return rule;
361+
break;
362+
363+
case CompiledProxyRuleType.SearchDomain:
364+
365+
if (rule.search == host)
366+
return rule;
367+
break;
368+
369369
case CompiledProxyRuleType.Exact:
370370
case CompiledProxyRuleType.RegexUrl:
371371
case CompiledProxyRuleType.SearchUrl:

src/core/ProxyRules.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -290,29 +290,50 @@ export class ProxyRules {
290290

291291
break;
292292

293-
case CompiledProxyRuleType.Exact:
293+
case CompiledProxyRuleType.SearchDomainSubdomainAndPath:
294294

295-
if (lowerCaseUrl == rule.search)
295+
if (schemaLessUrlLowerCase == null) {
296+
schemaLessUrlLowerCase = Utils.removeSchemaFromUrl(lowerCaseUrl);
297+
if (schemaLessUrlLowerCase == null) {
298+
continue;
299+
}
300+
}
301+
if (schemaLessUrlLowerCase.startsWith(rule.search))
296302
return rule;
297-
break;
298303

299-
case CompiledProxyRuleType.RegexHost:
304+
let ruleSearchHost = Utils.extractHostNameFromInvalidUrl(rule.search);
305+
if (ruleSearchHost != null) {
300306

301-
if (domainHostLowerCase == null) {
302-
domainHostLowerCase = Utils.extractHostNameFromUrl(lowerCaseUrl);
303307
if (domainHostLowerCase == null) {
304-
continue;
308+
domainHostLowerCase = Utils.extractHostNameFromUrl(lowerCaseUrl);
309+
if (domainHostLowerCase == null) {
310+
continue;
311+
}
305312
}
313+
314+
// should be the same
315+
if (ruleSearchHost != domainHostLowerCase && !domainHostLowerCase.endsWith('.' + ruleSearchHost))
316+
continue;
317+
318+
// after this state, we are sure that the url is for the same domain, now just checking the path
306319
}
307320

308-
if (rule.regex.test(domainHostLowerCase))
321+
// subdomains
322+
if (schemaLessUrlLowerCase.includes('.' + rule.search))
309323
return rule;
310324
break;
311325

312-
case CompiledProxyRuleType.RegexUrl:
313-
// Using original url with case sensitivity
314-
if (rule.regex.test(searchUrl))
326+
case CompiledProxyRuleType.SearchDomainAndPath:
327+
328+
if (schemaLessUrlLowerCase == null) {
329+
schemaLessUrlLowerCase = Utils.removeSchemaFromUrl(lowerCaseUrl);
330+
if (schemaLessUrlLowerCase == null) {
331+
continue;
332+
}
333+
}
334+
if (schemaLessUrlLowerCase.startsWith(rule.search))
315335
return rule;
336+
316337
break;
317338

318339
case CompiledProxyRuleType.SearchUrl:
@@ -321,61 +342,40 @@ export class ProxyRules {
321342
return rule;
322343
break;
323344

324-
case CompiledProxyRuleType.SearchDomain:
345+
case CompiledProxyRuleType.RegexUrl:
346+
// Using original url with case sensitivity
347+
if (rule.regex.test(searchUrl))
348+
return rule;
349+
break;
350+
351+
case CompiledProxyRuleType.RegexHost:
325352

326353
if (domainHostLowerCase == null) {
327354
domainHostLowerCase = Utils.extractHostNameFromUrl(lowerCaseUrl);
328355
if (domainHostLowerCase == null) {
329356
continue;
330357
}
331358
}
332-
if (rule.search == domainHostLowerCase)
333-
return rule;
334-
break;
335-
336-
case CompiledProxyRuleType.SearchDomainAndPath:
337359

338-
if (schemaLessUrlLowerCase == null) {
339-
schemaLessUrlLowerCase = Utils.removeSchemaFromUrl(lowerCaseUrl);
340-
if (schemaLessUrlLowerCase == null) {
341-
continue;
342-
}
343-
}
344-
if (schemaLessUrlLowerCase.startsWith(rule.search))
360+
if (rule.regex.test(domainHostLowerCase))
345361
return rule;
346-
347362
break;
348363

349-
case CompiledProxyRuleType.SearchDomainSubdomainAndPath:
364+
case CompiledProxyRuleType.SearchDomain:
350365

351-
if (schemaLessUrlLowerCase == null) {
352-
schemaLessUrlLowerCase = Utils.removeSchemaFromUrl(lowerCaseUrl);
353-
if (schemaLessUrlLowerCase == null) {
366+
if (domainHostLowerCase == null) {
367+
domainHostLowerCase = Utils.extractHostNameFromUrl(lowerCaseUrl);
368+
if (domainHostLowerCase == null) {
354369
continue;
355370
}
356371
}
357-
if (schemaLessUrlLowerCase.startsWith(rule.search))
372+
if (rule.search == domainHostLowerCase)
358373
return rule;
374+
break;
359375

360-
let ruleSearchHost = Utils.extractHostNameFromInvalidUrl(rule.search);
361-
if (ruleSearchHost != null) {
362-
363-
if (domainHostLowerCase == null) {
364-
domainHostLowerCase = Utils.extractHostNameFromUrl(lowerCaseUrl);
365-
if (domainHostLowerCase == null) {
366-
continue;
367-
}
368-
}
369-
370-
// should be the same
371-
if (ruleSearchHost != domainHostLowerCase && !domainHostLowerCase.endsWith('.' + ruleSearchHost))
372-
continue;
373-
374-
// after this state, we are sure that the url is for the same domain, now just checking the path
375-
}
376+
case CompiledProxyRuleType.Exact:
376377

377-
// subdomains
378-
if (schemaLessUrlLowerCase.includes('.' + rule.search))
378+
if (lowerCaseUrl == rule.search)
379379
return rule;
380380
break;
381381
}
@@ -408,18 +408,6 @@ export class ProxyRules {
408408

409409
break;
410410

411-
case CompiledProxyRuleType.RegexHost:
412-
413-
if (rule.regex.test(domainHostLowerCase))
414-
return rule;
415-
break;
416-
417-
case CompiledProxyRuleType.SearchDomain:
418-
419-
if (rule.search == domainHostLowerCase)
420-
return rule;
421-
break;
422-
423411
case CompiledProxyRuleType.SearchDomainSubdomainAndPath:
424412

425413
if (schemaLessUrlLowerCase == null) {
@@ -445,6 +433,18 @@ export class ProxyRules {
445433
return rule;
446434
break;
447435

436+
case CompiledProxyRuleType.RegexHost:
437+
438+
if (rule.regex.test(domainHostLowerCase))
439+
return rule;
440+
break;
441+
442+
case CompiledProxyRuleType.SearchDomain:
443+
444+
if (rule.search == domainHostLowerCase)
445+
return rule;
446+
break;
447+
448448
case CompiledProxyRuleType.Exact:
449449
case CompiledProxyRuleType.RegexUrl:
450450
case CompiledProxyRuleType.SearchUrl:

src/core/definitions.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ export enum CompiledProxyRuleType {
9393
SearchDomainAndPath,
9494
/** Matches domain and its subdomains including path in the end of each */
9595
SearchDomainSubdomainAndPath
96+
97+
/*
98+
Note on how popular these rules are on a subscription
99+
{
100+
"1": 31, // RegexUrl
101+
"3": 394, // SearchUrl
102+
"5": 3326, // SearchDomainSubdomain
103+
"6": 1074, // SearchDomainAndPath
104+
"7": 2363 // SearchDomainSubdomainAndPath
105+
}
106+
*/
96107
}
97108
function convertCompiledToProxyRuleType(compiledRule: CompiledProxyRuleType): ProxyRuleType | null {
98109
switch (compiledRule) {

0 commit comments

Comments
 (0)