bitwarden-android/src/Core/Utilities/CoreHelpers.cs

155 lines
4.8 KiB
C#
Raw Normal View History

2019-04-17 15:46:51 +03:00
using Bit.Core.Models.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
2019-04-16 01:17:19 +03:00
using System.Text.RegularExpressions;
2019-04-09 17:35:21 +03:00
namespace Bit.Core.Utilities
{
public static class CoreHelpers
{
2019-04-16 14:44:05 +03:00
public static readonly string IpRegex =
2019-04-16 01:17:19 +03:00
"^(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]?)$";
2019-04-16 14:44:05 +03:00
public static readonly string TldEndingRegex =
2019-04-16 01:17:19 +03:00
".*\\.(com|net|org|edu|uk|gov|ca|de|jp|fr|au|ru|ch|io|es|us|co|xyz|info|ly|mil)$";
2019-04-09 17:35:21 +03:00
public static readonly DateTime Epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static bool InDebugMode()
{
#if DEBUG
return true;
#else
return false;
#endif
}
2019-04-16 01:17:19 +03:00
public static string GetHostname(string uriString)
{
return GetUri(uriString)?.Host;
}
public static string GetHost(string uriString)
{
var uri = GetUri(uriString);
if(uri != null)
{
if(uri.IsDefaultPort)
{
return uri.Host;
}
else
{
return string.Format("{0}:{1}", uri.Host, uri.Port);
}
}
return null;
}
public static string GetDomain(string uriString)
{
var uri = GetUri(uriString);
if(uri == null)
{
return null;
}
if(uri.Host == "localhost" || Regex.IsMatch(uriString, IpRegex))
{
return uri.Host;
}
try
{
if(DomainName.TryParseBaseDomain(uri.Host, out var baseDomain))
{
return baseDomain ?? uri.Host;
}
}
catch { }
return null;
}
private static Uri GetUri(string uriString)
{
if(string.IsNullOrWhiteSpace(uriString))
{
return null;
}
var httpUrl = uriString.StartsWith("https://") || uriString.StartsWith("http://");
if(!httpUrl && !uriString.Contains("://") && Regex.IsMatch(uriString, TldEndingRegex))
{
uriString = "http://" + uriString;
}
if(Uri.TryCreate(uriString, UriKind.Absolute, out var uri))
{
return uri;
}
return null;
}
2019-04-17 15:46:51 +03:00
public static void NestedTraverse<T>(List<TreeNode<T>> nodeTree, int partIndex, string[] parts,
T obj, T parent, char delimiter) where T : ITreeNodeObject
{
if(parts.Length <= partIndex)
{
return;
}
var end = partIndex == parts.Length - 1;
var partName = parts[partIndex];
foreach(var n in nodeTree)
{
if(n.Node.Name != parts[partIndex])
{
continue;
}
if(end && n.Node.Id != obj.Id)
{
// Another node with the same name.
nodeTree.Add(new TreeNode<T>(obj, partName, parent));
return;
}
NestedTraverse(n.Children, partIndex + 1, parts, obj, n.Node, delimiter);
return;
}
if(!nodeTree.Any(n => n.Node.Name == partName))
{
if(end)
{
nodeTree.Add(new TreeNode<T>(obj, partName, parent));
return;
}
var newPartName = string.Concat(parts[partIndex], delimiter, parts[partIndex + 1]);
var newParts = new List<string> { newPartName };
var newPartsStartFrom = partIndex + 2;
newParts.AddRange(new ArraySegment<string>(parts, newPartsStartFrom, parts.Length - newPartsStartFrom));
NestedTraverse(nodeTree, 0, newParts.ToArray(), obj, parent, delimiter);
}
}
public static TreeNode<T> GetTreeNodeObject<T>(List<TreeNode<T>> nodeTree, string id) where T : ITreeNodeObject
{
foreach(var n in nodeTree)
{
if(n.Node.Id == id)
{
return n;
}
else if(n.Children != null)
{
var node = GetTreeNodeObject(n.Children, id);
if(node != null)
{
return node;
}
}
}
return null;
}
2019-04-09 17:35:21 +03:00
}
}