2022-10-13 13:05:43 +02:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
2024-09-16 16:09:43 -04:00
|
|
|
|
2022-10-13 13:05:43 +02:00
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2024-04-26 19:41:44 +02:00
|
|
|
using HostsUILib.Helpers;
|
2022-10-13 13:05:43 +02:00
|
|
|
|
2024-04-26 19:41:44 +02:00
|
|
|
namespace HostsUILib.Models
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2025-06-15 23:13:16 -07:00
|
|
|
#if !TESTONLY
|
|
|
|
|
[Microsoft.UI.Xaml.Data.Bindable]
|
|
|
|
|
#endif
|
2022-10-13 13:05:43 +02:00
|
|
|
public partial class Entry : ObservableObject
|
|
|
|
|
{
|
2023-06-06 17:16:06 +02:00
|
|
|
private static readonly char[] _spaceCharacters = new char[] { ' ', '\t' };
|
|
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
[ObservableProperty]
|
|
|
|
|
[NotifyPropertyChangedFor(nameof(Valid))]
|
2024-02-14 14:42:31 +00:00
|
|
|
[NotifyPropertyChangedFor(nameof(IsAddressValid))]
|
2022-10-13 13:05:43 +02:00
|
|
|
private string _address;
|
|
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
partial void OnAddressChanged(string value)
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-06-05 12:02:32 +02:00
|
|
|
if (ValidationHelper.ValidIPv4(value))
|
|
|
|
|
{
|
|
|
|
|
Type = AddressType.IPv4;
|
|
|
|
|
}
|
|
|
|
|
else if (ValidationHelper.ValidIPv6(value))
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-06-05 12:02:32 +02:00
|
|
|
Type = AddressType.IPv6;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Type = AddressType.Invalid;
|
2022-10-13 13:05:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
[ObservableProperty]
|
|
|
|
|
[NotifyPropertyChangedFor(nameof(Valid))]
|
2024-02-14 14:42:31 +00:00
|
|
|
[NotifyPropertyChangedFor(nameof(IsHostsValid))]
|
2022-10-13 13:05:43 +02:00
|
|
|
private string _hosts;
|
|
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
partial void OnHostsChanged(string value)
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-06-05 12:02:32 +02:00
|
|
|
SplittedHosts = value.Split(' ');
|
2022-10-13 13:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2023-09-05 13:13:41 +02:00
|
|
|
[NotifyPropertyChangedFor(nameof(Valid))]
|
2022-10-13 13:05:43 +02:00
|
|
|
private string _comment;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _active;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool? _ping;
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _pinging;
|
|
|
|
|
|
2022-12-14 16:52:00 +01:00
|
|
|
[ObservableProperty]
|
|
|
|
|
private bool _duplicate;
|
|
|
|
|
|
2023-06-23 21:54:45 +02:00
|
|
|
public bool Valid => Validate(true);
|
2023-06-05 12:02:32 +02:00
|
|
|
|
2024-02-14 14:42:31 +00:00
|
|
|
public bool IsAddressValid => ValidateAddressField();
|
|
|
|
|
|
|
|
|
|
public bool IsHostsValid => ValidateHostsField(true);
|
|
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
public string Line { get; private set; }
|
2023-02-27 19:11:57 +01:00
|
|
|
|
|
|
|
|
public AddressType Type { get; private set; }
|
2022-10-13 13:05:43 +02:00
|
|
|
|
2022-12-14 16:52:00 +01:00
|
|
|
public string[] SplittedHosts { get; private set; }
|
|
|
|
|
|
2023-02-27 19:11:57 +01:00
|
|
|
public int Id { get; set; }
|
|
|
|
|
|
2022-10-13 13:05:43 +02:00
|
|
|
public Entry()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 19:11:57 +01:00
|
|
|
public Entry(int id, string line)
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-02-27 19:11:57 +01:00
|
|
|
Id = id;
|
2023-06-05 12:02:32 +02:00
|
|
|
Line = line.Trim();
|
2022-10-13 13:05:43 +02:00
|
|
|
Parse();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 19:11:57 +01:00
|
|
|
public Entry(int id, string address, string hosts, string comment, bool active)
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-02-27 19:11:57 +01:00
|
|
|
Id = id;
|
2022-10-13 13:05:43 +02:00
|
|
|
Address = address.Trim();
|
|
|
|
|
Hosts = hosts.Trim();
|
|
|
|
|
Comment = comment.Trim();
|
|
|
|
|
Active = active;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Parse()
|
|
|
|
|
{
|
2023-06-05 12:02:32 +02:00
|
|
|
Active = !Line.StartsWith("#", StringComparison.InvariantCultureIgnoreCase);
|
2022-10-13 13:05:43 +02:00
|
|
|
|
2023-06-05 12:02:32 +02:00
|
|
|
var lineSplit = Line.TrimStart(' ', '#').Split('#');
|
2022-10-13 13:05:43 +02:00
|
|
|
|
|
|
|
|
if (lineSplit.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var addressHost = lineSplit[0];
|
|
|
|
|
|
2023-06-06 17:16:06 +02:00
|
|
|
var addressHostSplit = addressHost.Split(_spaceCharacters, StringSplitOptions.RemoveEmptyEntries);
|
2022-10-13 13:05:43 +02:00
|
|
|
var hostsBuilder = new StringBuilder();
|
|
|
|
|
var commentBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < addressHostSplit.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var element = addressHostSplit[i].Trim();
|
|
|
|
|
|
2023-06-06 17:16:06 +02:00
|
|
|
if (i == 0 && IPAddress.TryParse(element, out var _) && (element.Contains(':') || element.Contains('.')))
|
2022-10-13 13:05:43 +02:00
|
|
|
{
|
2023-06-06 17:16:06 +02:00
|
|
|
Address = element;
|
2022-10-13 13:05:43 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (hostsBuilder.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
hostsBuilder.Append(' ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hostsBuilder.Append(element);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Hosts = hostsBuilder.ToString();
|
|
|
|
|
|
|
|
|
|
for (var i = 1; i < lineSplit.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (commentBuilder.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
commentBuilder.Append('#');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commentBuilder.Append(lineSplit[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Comment = commentBuilder.ToString().Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Entry Clone()
|
|
|
|
|
{
|
|
|
|
|
return new Entry
|
|
|
|
|
{
|
2023-06-05 12:02:32 +02:00
|
|
|
Line = Line,
|
2022-10-13 13:05:43 +02:00
|
|
|
Address = Address,
|
|
|
|
|
Hosts = Hosts,
|
|
|
|
|
Comment = Comment,
|
|
|
|
|
Active = Active,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-06-23 21:54:45 +02:00
|
|
|
|
2024-02-14 14:42:31 +00:00
|
|
|
private bool ValidateAddressField()
|
|
|
|
|
{
|
|
|
|
|
return Type != AddressType.Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ValidateHostsField(bool validateHostsLength)
|
|
|
|
|
{
|
|
|
|
|
return ValidationHelper.ValidHosts(Hosts, validateHostsLength);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 21:54:45 +02:00
|
|
|
public bool Validate(bool validateHostsLength)
|
|
|
|
|
{
|
2023-09-05 13:13:41 +02:00
|
|
|
if (Equals("102.54.94.97", "rhino.acme.com", "source server") || Equals("38.25.63.10", "x.acme.com", "x client host"))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 14:42:31 +00:00
|
|
|
return ValidateAddressField() && ValidateHostsField(validateHostsLength);
|
2023-06-23 21:54:45 +02:00
|
|
|
}
|
2023-09-05 13:13:41 +02:00
|
|
|
|
|
|
|
|
private bool Equals(string address, string hosts, string comment)
|
|
|
|
|
{
|
|
|
|
|
return string.Equals(Address, address, StringComparison.Ordinal)
|
|
|
|
|
&& string.Equals(Hosts, hosts, StringComparison.Ordinal)
|
|
|
|
|
&& string.Equals(Comment, comment, StringComparison.Ordinal);
|
|
|
|
|
}
|
2022-10-13 13:05:43 +02:00
|
|
|
}
|
|
|
|
|
}
|