mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 01:48:25 +03:00
parse IP addresses as base domains
This commit is contained in:
parent
8922459418
commit
f5dd91afe5
4 changed files with 33 additions and 17 deletions
|
@ -4,12 +4,18 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Bit.App.Models
|
||||
{
|
||||
// ref: https://github.com/danesparza/domainname-parser
|
||||
public class DomainName
|
||||
{
|
||||
private const string IpRegex = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
|
||||
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||||
|
||||
private string _subDomain = string.Empty;
|
||||
private string _domain = string.Empty;
|
||||
private string _tld = string.Empty;
|
||||
|
@ -59,6 +65,20 @@ namespace Bit.App.Models
|
|||
return retval;
|
||||
}
|
||||
|
||||
public static bool TryParseBaseDomain(string domainString, out string result)
|
||||
{
|
||||
if(Regex.IsMatch(domainString, IpRegex))
|
||||
{
|
||||
result = domainString;
|
||||
return true;
|
||||
}
|
||||
|
||||
DomainName domain;
|
||||
var retval = TryParse(domainString, out domain);
|
||||
result = domain?.BaseDomain;
|
||||
return retval;
|
||||
}
|
||||
|
||||
private static void ParseDomainName(string domainString, out string TLD, out string SLD, out string SubDomain, out TLDRule MatchingRule)
|
||||
{
|
||||
// Make sure domain is all lowercase
|
||||
|
|
|
@ -115,10 +115,10 @@ namespace Bit.App.Models.Page
|
|||
return Uri;
|
||||
}
|
||||
|
||||
DomainName domain;
|
||||
if(DomainName.TryParse(uri.Host, out domain))
|
||||
string domain;
|
||||
if(DomainName.TryParseBaseDomain(uri.Host, out domain))
|
||||
{
|
||||
return domain.BaseDomain;
|
||||
return domain;
|
||||
}
|
||||
|
||||
return uri.Host;
|
||||
|
|
|
@ -30,20 +30,15 @@ namespace Bit.App.Pages
|
|||
Uri = uriString;
|
||||
|
||||
Uri uri;
|
||||
DomainName domainName;
|
||||
if(uriString?.StartsWith(Constants.AndroidAppProtocol) ?? false)
|
||||
{
|
||||
_name = uriString.Substring(Constants.AndroidAppProtocol.Length);
|
||||
}
|
||||
else if(!System.Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
|
||||
!DomainName.TryParse(uri.Host, out domainName))
|
||||
!DomainName.TryParseBaseDomain(uri.Host, out _name))
|
||||
{
|
||||
_name = "--";
|
||||
}
|
||||
else
|
||||
{
|
||||
_name = domainName.BaseDomain;
|
||||
}
|
||||
|
||||
_loginService = Resolver.Resolve<ILoginService>();
|
||||
_deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
|
||||
|
|
|
@ -78,11 +78,12 @@ namespace Bit.App.Services
|
|||
}
|
||||
|
||||
Uri uri = null;
|
||||
DomainName domainName = null;
|
||||
string domainName = null;
|
||||
var androidApp = UriIsAndroidApp(uriString);
|
||||
|
||||
if(!androidApp &&
|
||||
(!Uri.TryCreate(uriString, UriKind.Absolute, out uri) || !DomainName.TryParse(uri.Host, out domainName)))
|
||||
(!Uri.TryCreate(uriString, UriKind.Absolute, out uri) ||
|
||||
!DomainName.TryParseBaseDomain(uri.Host, out domainName)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
@ -104,7 +105,7 @@ namespace Bit.App.Services
|
|||
matchingFuzzyDomains.AddRange(eqDomain.Select(d => d).ToList());
|
||||
}
|
||||
}
|
||||
else if(Array.IndexOf(eqDomain, domainName.BaseDomain) >= 0)
|
||||
else if(Array.IndexOf(eqDomain, domainName) >= 0)
|
||||
{
|
||||
matchingDomains.AddRange(eqDomain.Select(d => d).ToList());
|
||||
}
|
||||
|
@ -112,7 +113,7 @@ namespace Bit.App.Services
|
|||
|
||||
if(!matchingDomains.Any())
|
||||
{
|
||||
matchingDomains.Add(androidApp ? uriString : domainName.BaseDomain);
|
||||
matchingDomains.Add(androidApp ? uriString : domainName);
|
||||
}
|
||||
|
||||
if(androidApp && androidAppWebUriString != null &&
|
||||
|
@ -156,18 +157,18 @@ namespace Bit.App.Services
|
|||
}
|
||||
|
||||
Uri loginUri;
|
||||
DomainName loginDomainName;
|
||||
string loginDomainName = null;
|
||||
if(!Uri.TryCreate(loginUriString, UriKind.Absolute, out loginUri)
|
||||
|| !DomainName.TryParse(loginUri.Host, out loginDomainName))
|
||||
|| !DomainName.TryParseBaseDomain(loginUri.Host, out loginDomainName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(Array.IndexOf(matchingDomainsArray, loginDomainName.BaseDomain) >= 0)
|
||||
if(Array.IndexOf(matchingDomainsArray, loginDomainName) >= 0)
|
||||
{
|
||||
matchingLogins.Add(new Login(login));
|
||||
}
|
||||
else if(androidApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName.BaseDomain) >= 0)
|
||||
else if(androidApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName) >= 0)
|
||||
{
|
||||
matchingFuzzyLogins.Add(new Login(login));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue