Example #1
0
        public static IpSetEntry ParseFromParts(IpSetSet set, String value)
        {
            var entry = new IpSetEntry(set);

            IpSetEntryParser.ParseEntry(entry, value);
            return(entry);
        }
Example #2
0
        public bool KeyEquals(IpSetEntry other)
        {
            bool r = _cidr.Equals(other._cidr) && _port == other._port && string.Equals(_mac, other._mac);

            if (!r)
            {
                return(false);
            }

            return(string.Equals(_protocol, other._protocol) ||
                   (String.IsNullOrEmpty(_protocol) && String.IsNullOrEmpty(other._protocol)));
        }
Example #3
0
        public static IpSetEntry Parse(String command, IpSetSets sets)
        {
            var parts = command.Split(new char[] { ' ' });

            if (parts.Length < 2)
            {
                return(null);
            }

            IpSetEntry entry = new IpSetEntry(null);

            string[] arguments = ArgumentHelper.SplitArguments(command);
            var      parser    = new IpSetEntryParser(arguments, entry, sets);

            for (int i = 0; i < arguments.Length; i++)
            {
                i += parser.FeedToSkip(i);
            }

            return(entry);
        }
Example #4
0
        public void Accept(String line, IpTablesSystem iptables)
        {
            String[] split = ArgumentHelper.SplitArguments(line);

            if (split.Length == 0)
            {
                return;
            }

            var command = split[0];

            switch (command)
            {
            case "create":
                var set = IpSetSet.Parse(split, iptables, 1);
                AddSet(set);
                break;

            case "add":
                IpSetEntry.Parse(split, this, 1);
                break;
            }
        }
Example #5
0
        public void Accept(String line, IpTablesSystem iptables)
        {
            String[] split = line.Split(new char[] { ' ' });

            if (split.Length == 0)
            {
                return;
            }

            var command = split[0];
            var options = String.Join(" ", split.Skip(1).ToArray());

            switch (command)
            {
            case "create":
                var set = IpSetSet.Parse(options, iptables);
                AddSet(set);
                break;

            case "add":
                IpSetEntry.Parse(options, this);
                break;
            }
        }
Example #6
0
        public bool KeyEquals(IpSetEntry other)
        {
            bool r = _cidr.Equals(other._cidr) && _port == other._port && string.Equals(_mac, other._mac);
            if (!r)
            {
                return false;
            }

            return string.Equals(_protocol, other._protocol) ||
                   (String.IsNullOrEmpty(_protocol) && String.IsNullOrEmpty(other._protocol));
        }
Example #7
0
        public static IpSetEntry Parse(String command, IpSetSets sets)
        {
            var parts = command.Split(new char[] {' '});

            if (parts.Length < 2)
            {
                return null;
            }

            IpSetEntry entry = new IpSetEntry(null);
            string[] arguments = ArgumentHelper.SplitArguments(command);
            var parser = new IpSetEntryParser(arguments, entry, sets);

            for (int i = 0; i < arguments.Length; i++)
            {
                i += parser.FeedToSkip(i);
            }

            return entry;
        }
Example #8
0
 protected bool Equals(IpSetEntry other)
 {
     return _set.Equals(other.Set) && KeyEquals(other);
 }
Example #9
0
        /// <summary>
        /// Sync with an IPTables system
        /// </summary>
        /// <param name="canDeleteSet"></param>
        /// <param name="transactional"></param>
        public void Sync(
            Func <IpSetSet, bool> canDeleteSet = null, bool transactional = true)
        {
            if (transactional)
            {
                //Start transaction
                _system.SetAdapter.StartTransaction();
            }

            var systemSets = _system.SetAdapter.SaveSets(_system);

            foreach (var set in _sets)
            {
                var systemSet = systemSets.GetSetByName(set.Name);
                if (systemSet == null)
                {
                    //Add
                    _system.SetAdapter.CreateSet(set);
                    systemSet = new IpSetSet(set.Type, set.Name, set.Timeout, _system, set.SyncMode);
                }
                else
                {
                    //Update if applicable
                    if (!systemSet.SetEquals(set))
                    {
                        _system.SetAdapter.DestroySet(set.Name);
                        _system.SetAdapter.CreateSet(set);
                        systemSet = new IpSetSet(set.Type, set.Name, set.Timeout, _system, set.SyncMode);
                    }
                }

                if (set.SyncMode == IpSetSyncMode.SetAndEntries)
                {
                    foreach (var entry in set.Entries)
                    {
                        try
                        {
                            var systemEntry = systemSet.Entries.FirstOrDefault((a) => a.KeyEquals(entry));
                            if (systemEntry == null)
                            {
                                _system.SetAdapter.AddEntry(entry);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(entry.Port);
                            Console.WriteLine(systemSet.Entries.Count);
                            throw;
                        }
                    }

                    foreach (var entry in systemSet.Entries)
                    {
                        IpSetEntry entry1   = entry;
                        var        memEntry = set.Entries.FirstOrDefault(((a) => a.KeyEquals(entry1)));
                        if (memEntry == null)
                        {
                            _system.SetAdapter.DeleteEntry(entry);
                        }
                    }
                }
            }

            if (canDeleteSet != null)
            {
                foreach (var set in systemSets.Sets)
                {
                    if (_sets.FirstOrDefault((a) => a.Name == set.Name) == null && canDeleteSet(set))
                    {
                        _system.SetAdapter.DestroySet(set.Name);
                    }
                }
            }

            if (transactional)
            {
                //End Transaction: COMMIT
                _system.SetAdapter.EndTransactionCommit();
            }
        }
Example #10
0
 protected bool Equals(IpSetEntry other)
 {
     return(_set.Equals(other.Set) && KeyEquals(other));
 }
Example #11
0
        /// <summary>
        /// Sync with an IPTables system
        /// </summary>
        /// <param name="canDeleteSet"></param>
        /// <param name="transactional"></param>
        public void Sync(
            Func <IpSetSet, bool> canDeleteSet = null, bool transactional = true)
        {
            if (transactional)
            {
                //Start transaction
                System.SetAdapter.StartTransaction();
            }

            var systemSets = System.SetAdapter.SaveSets(System);

            foreach (var set in _sets)
            {
                var systemSet = systemSets.GetSetByName(set.Name);
                if (systemSet == null)
                {
                    //Add
                    System.SetAdapter.CreateSet(set);
                    systemSet = new IpSetSet(set.Type, set.Name, set.Timeout, "inet", System, set.SyncMode);
                }
                else
                {
                    //Update if applicable
                    if (!systemSet.SetEquals(set))
                    {
                        System.SetAdapter.DestroySet(set.Name);
                        System.SetAdapter.CreateSet(set);
                        systemSet = new IpSetSet(set.Type, set.Name, set.Timeout, "inet", System, set.SyncMode);
                    }
                }

                if (set.SyncMode == IpSetSyncMode.SetAndEntries)
                {
                    var distinctEntries = set.Entries.Distinct().ToList();
                    try
                    {
                        foreach (var entry in distinctEntries)
                        {
                            try
                            {
                                var systemEntry = systemSet.Entries.FirstOrDefault((a) => a.KeyEquals(entry));
                                if (systemEntry == null)
                                {
                                    System.SetAdapter.AddEntry(entry);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw;
                            }
                        }

                        foreach (var entry in systemSet.Entries)
                        {
                            IpSetEntry entry1   = entry;
                            var        memEntry = distinctEntries.FirstOrDefault(((a) => a.KeyEquals(entry1)));
                            if (memEntry == null)
                            {
                                System.SetAdapter.DeleteEntry(entry);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new IpTablesNetException(String.Format("An exception occured while adding or removing on entries of set {0} message:{1}", set.Name, ex.Message), ex);
                    }
                }
            }

            if (canDeleteSet != null)
            {
                foreach (var set in systemSets.Sets)
                {
                    if (_sets.FirstOrDefault((a) => a.Name == set.Name) == null && canDeleteSet(set))
                    {
                        System.SetAdapter.DestroySet(set.Name);
                    }
                }
            }

            if (transactional)
            {
                //End Transaction: COMMIT
                if (!System.SetAdapter.EndTransactionCommit())
                {
                    throw new IpTablesNetException("Failed to commit IPSets");
                }
            }
        }
Example #12
0
 public static IpSetEntry ParseFromParts(IpSetSet set, String value)
 {
     var entry = new IpSetEntry(set);
     IpSetEntryParser.ParseEntry(entry, value);
     return entry;
 }