|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace NETworkManager.Models.RunCommand; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Detects the kind of a target that is typed without a command, so the commands that accept it can be suggested |
| 10 | +/// (e.g. "10.0.0.1" → ping, traceroute, ...). A single word without a domain is |
| 11 | +/// <see cref="RunCommandTargetKind.Hostname" />, since it can also be a mistyped command. |
| 12 | +/// </summary> |
| 13 | +public static partial class RunCommandTarget |
| 14 | +{ |
| 15 | + [GeneratedRegex("^https?://", RegexOptions.IgnoreCase)] |
| 16 | + private static partial Regex UrlRegex(); |
| 17 | + |
| 18 | + [GeneratedRegex("^(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2}$|^[0-9a-f]{12}$|^(?:[0-9a-f]{4}\\.){2}[0-9a-f]{4}$", |
| 19 | + RegexOptions.IgnoreCase)] |
| 20 | + private static partial Regex MACAddressRegex(); |
| 21 | + |
| 22 | + [GeneratedRegex("^(?:[0-9a-f]{2}[:-]){2}[0-9a-f]{2}$", RegexOptions.IgnoreCase)] |
| 23 | + private static partial Regex OUIRegex(); |
| 24 | + |
| 25 | + [GeneratedRegex("^\\d{1,5}(?:-\\d{1,5})?(?:/(?:tcp|udp|sctp))?(?:;\\d{1,5}(?:-\\d{1,5})?(?:/(?:tcp|udp|sctp))?)*$", |
| 26 | + RegexOptions.IgnoreCase)] |
| 27 | + private static partial Regex PortsRegex(); |
| 28 | + |
| 29 | + [GeneratedRegex("^as\\d{1,10}$", RegexOptions.IgnoreCase)] |
| 30 | + private static partial Regex ASNRegex(); |
| 31 | + |
| 32 | + [GeneratedRegex("^[0-9.\\-\\[\\],]+$")] |
| 33 | + private static partial Regex IPv4RangeRegex(); |
| 34 | + |
| 35 | + // The last label (top-level domain) has at least one letter, so a shortened or partly typed IPv4 address (e.g. |
| 36 | + // "192.168.1") is not a host name. |
| 37 | + [GeneratedRegex( |
| 38 | + "^(?=.{1,253}\\.?$)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.)+" + |
| 39 | + "(?=[a-z0-9-]*[a-z])[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\\.?$", |
| 40 | + RegexOptions.IgnoreCase)] |
| 41 | + private static partial Regex HostnameWithDomainRegex(); |
| 42 | + |
| 43 | + // A label with at least one letter (only digits is not a host name) |
| 44 | + [GeneratedRegex("^(?=[a-z0-9-]*[a-z])[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$", RegexOptions.IgnoreCase)] |
| 45 | + private static partial Regex HostnameRegex(); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets the kind of the target. |
| 49 | + /// </summary> |
| 50 | + /// <param name="target">Target without spaces (e.g. "10.0.0.1" or "10.0.0.1;10.0.0.2").</param> |
| 51 | + /// <returns>Kind of the target, <see cref="RunCommandTargetKind.None" /> if it is not clearly a target.</returns> |
| 52 | + public static RunCommandTargetKind GetKind(string target) |
| 53 | + { |
| 54 | + if (string.IsNullOrEmpty(target) || target.Any(char.IsWhiteSpace)) |
| 55 | + return RunCommandTargetKind.None; |
| 56 | + |
| 57 | + if (UrlRegex().IsMatch(target)) |
| 58 | + return Uri.TryCreate(target, UriKind.Absolute, out _) ? RunCommandTargetKind.URL : RunCommandTargetKind.None; |
| 59 | + |
| 60 | + if (MACAddressRegex().IsMatch(target)) |
| 61 | + return RunCommandTargetKind.MACAddress; |
| 62 | + |
| 63 | + if (OUIRegex().IsMatch(target)) |
| 64 | + return RunCommandTargetKind.OUI; |
| 65 | + |
| 66 | + if (PortsRegex().IsMatch(target)) |
| 67 | + return RunCommandTargetKind.Ports; |
| 68 | + |
| 69 | + if (ASNRegex().IsMatch(target)) |
| 70 | + return RunCommandTargetKind.ASN; |
| 71 | + |
| 72 | + // Several hosts, IP ranges or subnets |
| 73 | + if (target.Contains(';')) |
| 74 | + { |
| 75 | + var parts = target.Split(';', StringSplitOptions.RemoveEmptyEntries); |
| 76 | + |
| 77 | + return parts.Length > 0 && parts.All(x => GetKind(x) is RunCommandTargetKind.Host |
| 78 | + or RunCommandTargetKind.Hostname or RunCommandTargetKind.IPRange or RunCommandTargetKind.Subnet) |
| 79 | + ? RunCommandTargetKind.Hosts |
| 80 | + : RunCommandTargetKind.None; |
| 81 | + } |
| 82 | + |
| 83 | + // Subnet with CIDR or subnet mask (e.g. 10.0.0.0/24, 10.0.0.0/255.255.255.0, 2001:db8::/32) |
| 84 | + var slash = target.IndexOf('/'); |
| 85 | + |
| 86 | + if (slash > 0) |
| 87 | + return IPAddress.TryParse(target[..slash], out _) && |
| 88 | + (int.TryParse(target[(slash + 1)..], out _) || IPAddress.TryParse(target[(slash + 1)..], out _)) |
| 89 | + ? RunCommandTargetKind.Subnet |
| 90 | + : RunCommandTargetKind.None; |
| 91 | + |
| 92 | + // IPv4 range (e.g. 10.0.0.1-10.0.0.100, 10.0.0.1-100, 10.0.[0-1].1) |
| 93 | + if ((target.Contains('-') || target.Contains('[')) && target.Contains('.') && IPv4RangeRegex().IsMatch(target)) |
| 94 | + return RunCommandTargetKind.IPRange; |
| 95 | + |
| 96 | + // IP address: IPv4 with all 4 parts (a shortened or partly typed form like "10.1" is not a target) or IPv6 |
| 97 | + if (IPAddress.TryParse(target, out _) && (target.Count(x => x == '.') == 3 || target.Contains(':'))) |
| 98 | + return RunCommandTargetKind.Host; |
| 99 | + |
| 100 | + if (HostnameWithDomainRegex().IsMatch(target)) |
| 101 | + return RunCommandTargetKind.Host; |
| 102 | + |
| 103 | + return HostnameRegex().IsMatch(target) ? RunCommandTargetKind.Hostname : RunCommandTargetKind.None; |
| 104 | + } |
| 105 | +} |
0 commit comments