diff --git a/src/core/ProxyRules.ts b/src/core/ProxyRules.ts index 3ab3e87b..3ca505ec 100644 --- a/src/core/ProxyRules.ts +++ b/src/core/ProxyRules.ts @@ -263,6 +263,24 @@ export class ProxyRules { for (let rule of rules) { switch (rule.compiledRuleType) { + case CompiledProxyRuleType.SearchDomainSubdomain: + + if (domainHost == null) { + domainHost = Utils.extractHostFromUrl(url); + if (domainHost == null) { + continue; + } + } + // domain + if (domainHost == rule.search) + return rule; + + // subdomains + if (domainHost.endsWith('.' + rule.search)) + return rule; + + break; + case CompiledProxyRuleType.Exact: if (url == rule.search) @@ -306,24 +324,6 @@ export class ProxyRules { return rule; break; - case CompiledProxyRuleType.SearchDomainSubdomain: - - if (domainHost == null) { - domainHost = Utils.extractHostFromUrl(url); - if (domainHost == null) { - continue; - } - } - // domain - if (domainHost == rule.search) - return rule; - - // subdomains - if (domainHost.endsWith('.' + rule.search)) - return rule; - - break; - case CompiledProxyRuleType.SearchDomainAndPath: if (schemaLessUrl == null) { @@ -371,6 +371,80 @@ export class ProxyRules { break; } } + + + // if we had a rule with domain, we need to check for port as well + if (domainHost != null) { + // NOTE: Only rules that work on host name should be checked + let domainHostPure = Utils.extractHostNameFromUrl(url); + + if (domainHostPure != domainHost) { + + // host has port part, doing a recheck + domainHost = domainHostPure; + + for (let rule of rules) { + + switch (rule.compiledRuleType) { + + case CompiledProxyRuleType.SearchDomainSubdomain: + + // domain + if (domainHost == rule.search) + return rule; + + // subdomains + if (domainHost.endsWith('.' + rule.search)) + return rule; + + break; + + case CompiledProxyRuleType.RegexHost: + + if (rule.regex.test(domainHost)) + return rule; + break; + + case CompiledProxyRuleType.SearchDomain: + + if (rule.search == domainHost) + return rule; + break; + + case CompiledProxyRuleType.SearchDomainSubdomainAndPath: + + if (schemaLessUrl == null) { + schemaLessUrl = Utils.removeSchemaFromUrl(url); + if (schemaLessUrl == null) { + continue; + } + } + if (schemaLessUrl.startsWith(rule.search)) + return rule; + + let ruleSearchHost = Utils.extractHostNameFromInvalidUrl(rule.search); + if (ruleSearchHost != null) { + // should be the same + if (ruleSearchHost != domainHost && !domainHost.endsWith('.' + ruleSearchHost)) + continue; + + // after this state, we are sure that the url is for the same domain, now just checking the path + } + + // subdomains + if (schemaLessUrl.includes('.' + rule.search)) + return rule; + break; + + case CompiledProxyRuleType.Exact: + case CompiledProxyRuleType.RegexUrl: + case CompiledProxyRuleType.SearchUrl: + case CompiledProxyRuleType.SearchDomainAndPath: + break; + } + } + } + } } catch (e) { Debug.warn(`findMatchForUrl failed for ${url}`, e); } diff --git a/src/lib/Utils.ts b/src/lib/Utils.ts index 262e0f92..f885b5de 100644 --- a/src/lib/Utils.ts +++ b/src/lib/Utils.ts @@ -229,7 +229,26 @@ export class Utils { catch (e) { return null; } } + public static extractHostNameFromInvalidUrl(url: string): string | null { + try { + if (url.includes(":/")) { + try { + new URL(url); + // url is valid + return Utils.extractHostNameFromUrl(url); + } catch { } + } + + let urlFixed = 'http://' + url; + return Utils.extractHostNameFromUrl(urlFixed); + } + catch (e) { return null; } + } public static extractHostFromUrl(url: string): string | null { + /** + * For `http://sub.git.com/test` returns `sub.git.com` + * For `http://sub.git.com:6675/test` returns `sub.git.com:6675` + */ try { const u = new URL(url); if (Utils.invalidHostSchemas.indexOf(u.protocol) >= 0) @@ -241,6 +260,22 @@ export class Utils { catch (e) { return null; } } + public static extractHostNameFromUrl(url: string): string | null { + /** + * For `http://sub.git.com/test` returns `sub.git.com` + * For `http://sub.git.com:6675/test` returns `sub.git.com` + */ + try { + const u = new URL(url); + if (Utils.invalidHostSchemas.indexOf(u.protocol) >= 0) + return null; + let host = u.hostname; + + return host; + } + catch (e) { return null; } + } + public static extractSubdomainListFromUrl(url: string): string[] { let host = Utils.extractHostFromUrl(url); if (host === null)