Skip to content

Commit

Permalink
Port number should be double checked #304
Browse files Browse the repository at this point in the history
  • Loading branch information
salarcode committed Jul 17, 2023
1 parent 0ac498a commit f7a2fc5
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 18 deletions.
110 changes: 92 additions & 18 deletions src/core/ProxyRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
35 changes: 35 additions & 0 deletions src/lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f7a2fc5

Please sign in to comment.