static void RunUpdateMode() { if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } string mask = ArgsQueue.Dequeue(); List <HostsItem> lines = Hosts.GetMatched(mask); if (lines.Count == 0) { throw new HostNotFoundException(mask); } NetAddress address = null; string comment = null; bool inComment = false; while (ArgsQueue.Count > 0) { string arg = ArgsQueue.Dequeue(); if (inComment) { comment += arg + " "; continue; } if (arg.Length > 0 && arg[0] == '#') { inComment = true; comment = (arg.Length > 1) ? (arg.Substring(1) + " ") : ""; continue; } arg = arg.ToLower(); NetAddress TestAddress = NetAddress.TryCreate(arg); if (TestAddress != null) { address = TestAddress; continue; } } foreach (HostsItem line in lines) { if (address != null) { line.IP = address; } if (comment != null) { line.Comment = comment; } Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); } }
static void RunAddMode() { if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } HostAliases aliases = new HostAliases(); NetAddress address = new NetAddress("127.0.0.1"); string comment = ""; bool inComment = false; while (ArgsQueue.Count > 0) { string arg = ArgsQueue.Dequeue(); if (inComment) { comment += arg + " "; continue; } if (arg.Length > 0 && arg[0] == '#') { inComment = true; comment = (arg.Length > 1) ? (arg.Substring(1) + " ") : ""; continue; } arg = arg.ToLower(); NetAddress TestAddress = NetAddress.TryCreate(arg); if (TestAddress != null) { address = TestAddress; continue; } HostName TestHostName = HostName.TryCreate(arg); if (TestHostName != null) { aliases.Add(TestHostName); continue; } throw new Exception(String.Format("Unknown argument '{0}'", arg)); } HostsItem line = new HostsItem(address, aliases, comment); Hosts.Add(line); Console.WriteLine("[ADDED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); }
public bool Equals(string ip) { return(Equals(NetAddress.TryCreate(ip))); }
static void RunUpdateMode(bool autoadd = false) { if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } string mask = ArgsQueue.Dequeue(); List <HostsItem> lines = Hosts.GetMatched(mask); if (lines.Count == 0 && (!autoadd || mask.IndexOf('*') != -1)) { throw new HostNotFoundException(mask); } NetAddress address_ipv4 = null; NetAddress address_ipv6 = null; string comment = null; bool in_comment = false; while (ArgsQueue.Count > 0) { string arg = ArgsQueue.Dequeue(); if (in_comment) { comment += arg + " "; continue; } if (arg.Length > 0 && arg[0] == '#') { in_comment = true; comment = (arg.Length > 1) ? (arg.Substring(1) + " ") : ""; continue; } arg = arg.ToLower(); NetAddress address_test = NetAddress.TryCreate(arg); if (address_test != null) { if (address_test.Type == NetAddressType.IPv4) { address_ipv4 = address_test; } else { address_ipv6 = address_test; } continue; } } var ipv4_added = false; var ipv6_added = false; foreach (HostsItem line in lines) { if (address_ipv4 == null && address_ipv6 == null && comment != null) { // Update comments only line.Comment = comment; Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); continue; } if (address_ipv4 != null && line.IP.Type == NetAddressType.IPv4) { ipv4_added = true; line.IP = address_ipv4; if (comment != null) { line.Comment = comment; } Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); } if (address_ipv6 != null && line.IP.Type == NetAddressType.IPv6) { ipv6_added = true; line.IP = address_ipv6; if (comment != null) { line.Comment = comment; } Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); } } if (address_ipv4 != null && !ipv4_added && autoadd) { AddHostsItem(address_ipv4, new HostAliases(mask), comment); } if (address_ipv6 != null && !ipv6_added && autoadd) { AddHostsItem(address_ipv6, new HostAliases(mask), comment); } }
static void RunAddMode() { if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } HostAliases aliases = new HostAliases(); NetAddress address_ipv4 = null; NetAddress address_ipv6 = null; string comment = ""; bool in_comment = false; while (ArgsQueue.Count > 0) { string arg = ArgsQueue.Dequeue(); if (in_comment) { comment += arg + " "; continue; } if (arg.Length > 0 && arg[0] == '#') { in_comment = true; comment = (arg.Length > 1) ? (arg.Substring(1) + " ") : ""; continue; } arg = arg.ToLower(); var address_test = NetAddress.TryCreate(arg); if (address_test != null) { if (address_test.Type == NetAddressType.IPv4) { address_ipv4 = address_test; } else { address_ipv6 = address_test; } continue; } var hostname_test = HostName.TryCreate(arg); if (hostname_test != null) { aliases.Add(hostname_test); continue; } throw new Exception(String.Format("Unknown argument '{0}'", arg)); } if (address_ipv4 == null && address_ipv6 == null) { address_ipv4 = new NetAddress("127.0.0.1"); } if (address_ipv4 != null) { AddHostsItem(address_ipv4, aliases, comment.Trim()); } if (address_ipv6 != null) { AddHostsItem(address_ipv6, aliases, comment.Trim()); } }