Ejemplo n.º 1
0
        static void AddHostsItem(NetAddress address, HostAliases aliases, string comment)
        {
            if (aliases.Count == 0)
            {
                throw new HostNotSpecifiedException();
            }

            // Remove duplicates
            foreach (HostName host in aliases)
            {
                var lines = Hosts.FindAll(item => item.Valid && item.IP.Type == address.Type && item.Aliases.Contains(host));
                foreach (var line in lines)
                {
                    if (line.Aliases.Count == 1)
                    {
                        Hosts.Remove(line);
                        Console.WriteLine("[REMOVED] {0} {1}", line.IP.ToString(), line.Aliases.ToString());
                    }
                    else
                    {
                        line.Aliases.Remove(host);
                        Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString());
                    }
                }
            }

            // New host
            var new_item = new HostsItem(address, aliases, comment == null ? "" : comment.Trim());

            Hosts.Add(new_item);
            Console.WriteLine("[ADDED] {0} {1}", new_item.IP.ToString(), new_item.Aliases.ToString());
        }
Ejemplo n.º 2
0
 public HostsItem(NetAddress ip, HostAliases hosts, string comment = "")
 {
     Enabled = true;
     IP      = ip;
     Aliases = hosts;
     Comment = comment;
     Valid   = true;
 }
Ejemplo n.º 3
0
 public HostsItem(NetAddress ip, HostName host, string comment = "")
 {
     Enabled = true;
     IP      = ip;
     Aliases = new HostAliases(host);
     Comment = comment;
     Valid   = true;
 }
Ejemplo n.º 4
0
 public HostsItem(string ip, string[] hosts, string comment = "")
 {
     Enabled = true;
     IP      = new NetAddress(ip);
     Aliases = new HostAliases(hosts);
     Comment = comment;
     Valid   = true;
 }
Ejemplo n.º 5
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());
        }
Ejemplo n.º 6
0
 public bool Parse(string value)
 {
     ParsedString = value;
     try
     {
         var match = HostRowPattern.Match(value);
         if (!match.Success)
         {
             throw new FormatException();
         }
         Enabled = value[0] != '#';
         IP      = new NetAddress(match.Groups["ip"].Value);
         Aliases = new HostAliases(match.Groups["hosts"].Value);
         Comment = match.Groups["comment"].Value;
         Hidden  = false;
         if (!String.IsNullOrEmpty(Comment))
         {
             Hidden = Comment[0] == '!';
             if (Hidden)
             {
                 Comment = Comment.Substring(1).Trim();
             }
         }
         Valid = true;
     }
     catch
     {
         Enabled = false;
         Hidden  = false;
         IP      = null;
         Aliases = null;
         Comment = null;
         Valid   = false;
     }
     ParsedHash = HalfMD5.ComputeHash(ToString());
     return(Valid);
 }
Ejemplo n.º 7
0
 public int Set(HostAliases hosts)
 {
     Clear(); return(Add(hosts));
 }
Ejemplo n.º 8
0
 public HostAliases(HostAliases hosts)
 {
     Set(hosts);
 }
Ejemplo n.º 9
0
 public int Add(HostAliases hosts)
 {
     return(Add(hosts.ToArray()));
 }
Ejemplo n.º 10
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());
            }
        }
Ejemplo n.º 11
0
 public void Add(NetAddress ip, HostAliases hosts, string comment = "")
 {
     base.Add(new HostsItem(ip, hosts, comment));
 }
Ejemplo n.º 12
0
        public bool Parse(string value)
        {
            SourceString = value;
            try
            {
                value = value.Trim();
                if (String.IsNullOrEmpty(value))
                {
                    throw new FormatException();
                }
                if (value.IndexOfAny(new char[] { '\r', '\n' }) != -1)
                {
                    throw new FormatException();
                }

                // Check if enabled.
                Enabled = value[0] != '#';
                if (!Enabled)
                {
                    value = value.Substring(1).TrimStart();
                }
                if (String.IsNullOrEmpty(value))
                {
                    throw new FormatException();
                }

                // Parse comment part.
                Hidden  = false;
                Comment = null;
                var comment_pos = value.IndexOf('#');
                if (comment_pos != -1)
                {
                    Comment = value.Substring(comment_pos + 1).Trim();
                    if (!String.IsNullOrEmpty(Comment))
                    {
                        Hidden = Comment[0] == '!';
                        if (Hidden)
                        {
                            Comment = Comment.Substring(1).Trim();
                        }
                    }
                    value = value.Substring(0, comment_pos).TrimEnd();
                    if (String.IsNullOrEmpty(value))
                    {
                        throw new FormatException();
                    }
                }

                // Parse IP and hosts.
                var parts = value.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length < 2)
                {
                    throw new FormatException();
                }

                IP      = new NetAddress(parts[0]);
                Aliases = new HostAliases(parts.Skip(1).ToArray());
                Valid   = true;
            }
            catch
            {
                Enabled = false;
                Hidden  = false;
                IP      = null;
                Aliases = null;
                Comment = null;
                Valid   = false;
            }
            SourceHash = HalfMD5.ComputeHash(ToString());
            return(Valid);
        }