Example #1
0
        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());
        }
Example #2
0
        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());
            }
        }
Example #3
0
 public bool Equals(string host)
 {
     return(Equals(HostName.TryCreate(host)));
 }