/// <summary> /// Initializes a new instance of the <see cref="HostsEntry"/> class. /// </summary> /// <param name="entry">The entry to copy from.</param> public HostsEntry(HostsEntry entry) { this.comment = entry.comment; this.enabled = entry.enabled; this.hostnames = entry.hostnames; this.ipAddress = entry.ipAddress; this.unparsedText = entry.unparsedText; this.unparsedTextInvalid = entry.unparsedTextInvalid; this.valid = entry.valid; this.hostnamesValid = entry.hostnamesValid; this.ipAddressValid = entry.ipAddressValid; this.errors = new Dictionary <string, string>(entry.errors); }
private void NewEntry(HostsFile hostsFile) { Console.Clear(); Console.WriteLine("Please enter the URL you wish to map."); Regex regex = new Regex(@"(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)"); string URLInput = Console.ReadLine(); if (regex.IsMatch(URLInput)) { Console.WriteLine("valid URL"); } else { Console.WriteLine("Invalid URL. Press any key to return to main menu."); Console.ReadKey(); Main(hostsFile); } Console.WriteLine($"Please enter the IP you wish to map {URLInput} to."); regex = new Regex(@"((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]?)){3}|localhost)"); string IPInput = Console.ReadLine(); if (regex.IsMatch(IPInput)) { Console.WriteLine("valid IP"); HostsEntry newEntry = new HostsEntry(URLInput, IPInput); hostsFile.hostsEntries.Add(newEntry); hostsFile.SaveHostsFile(); Main(hostsFile); } else { Console.WriteLine("Invalid IP. Press any key to return to main menu."); Console.ReadKey(); Main(hostsFile); } }
void EntryDetails(HostsEntry entry) { Console.Clear(); Console.WriteLine($"{entry.URL} is mapped to {entry.IP}."); Console.WriteLine("Press \"U\" to change the URL, \"I\" to change the IP, \"D\" to delete, or \"B\" to go back"); switch (Console.ReadKey().Key) { case ConsoleKey.U: Console.Clear(); Console.Write($"Please enter the URL you would like to replace this entry." + "\n"); Regex regex = new Regex(@"(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)"); string URLInput = Console.ReadLine(); if (regex.IsMatch(URLInput)) { Console.Write($"Replace {entry.URL} with {URLInput}? (Y/N)"); ConsoleKey yesNoInput = ConsoleKey.Decimal; while (yesNoInput != ConsoleKey.Y || yesNoInput != ConsoleKey.N) { yesNoInput = Console.ReadKey().Key; } switch (yesNoInput) { case ConsoleKey.Y: entry.URL = URLInput; hosts.SaveHostsFile(); Console.ReadKey(); Main(hosts); break; case ConsoleKey.N: EntryDetails(entry); break; } } else { Console.WriteLine("Invalid URL. Press any key to try again."); Console.ReadKey(); EntryDetails(entry); } break; case ConsoleKey.I: Console.Clear(); Console.Write($"Please enter the IP you would like to replace this entry." + "\n"); regex = new Regex(@"((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]?)){3}|localhost)"); string IPInput = Console.ReadLine(); if (regex.IsMatch(IPInput)) { Console.Write($"Replace {entry.IP} with {IPInput}? (Y/N)"); switch (Console.ReadKey().Key) { case ConsoleKey.Y: entry.IP = IPInput; hosts.SaveHostsFile(); Console.ReadKey(); Main(hosts); break; case ConsoleKey.N: EntryDetails(entry); break; } } else { Console.WriteLine("Invalid IP. Press any key to try again."); Console.ReadKey(); EntryDetails(entry); } break; case ConsoleKey.D: Console.Clear(); Console.Write($"Are you sure you wish to delete the entry" + "\n"); Console.Write($"mapping {entry.URL} to {entry.IP}? (Y/N)"); switch (Console.ReadKey().Key) { case ConsoleKey.Y: hosts.hostsEntries.Remove(entry); hosts.SaveHostsFile(); Main(hosts); break; case ConsoleKey.N: EntryDetails(entry); break; } break; case ConsoleKey.B: Main(hosts); break; default: Console.WriteLine("invalid key"); EntryDetails(entry); break; } }