Пример #1
0
        private static void FirewallMangleMerge(ITikConnection connection)
        {
            //manage just subset before rules marked with comment =START= and =END=

            //Create subset boundaries if not present
            const string startComment = "=START=";
            const string endComment = "=END=";
            var startMangle = connection.LoadSingleOrDefault<FirewallMangle>(connection.CreateParameter("comment", startComment));
            if (startMangle == null)
            {
                startMangle = new FirewallMangle()
                {
                    Chain = "forward",
                    Action = FirewallMangle.ActionType.Passthrough,
                    Comment = startComment,
                    Disabled = true,
                };
                connection.Save(startMangle);
            };
            var endMangle = connection.LoadSingleOrDefault<FirewallMangle>(connection.CreateParameter("comment", endComment));
            if (endMangle == null)
            {
                endMangle = new FirewallMangle()
                {
                    Chain = "forward",
                    Action = FirewallMangle.ActionType.Passthrough,
                    Comment = endComment,
                    Disabled = true,
                };
                connection.Save(endMangle);
            };

            //Merge subset between boundaries
            string unique = Guid.NewGuid().ToString();
            List<FirewallMangle> original = connection.LoadAll<FirewallMangle>().SkipWhile(m=>m.Comment != startComment).TakeWhile(m=>m.Comment != endComment)
                .Concat(new List<FirewallMangle> { endMangle})
                .ToList(); //just subset between =START= and =END= (not very elegant but functional and short ;-) )
            List<FirewallMangle> expected = new List<FirewallMangle>();
            expected.Add(startMangle);
            expected.Add(new FirewallMangle()
            {
                Chain = "forward",
                SrcAddress = "192.168.1.1",
                Action = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-001",
                Passthrough = false,
            });
            expected.Add(new FirewallMangle()
            {
                Chain = "forward",
                SrcAddress = "192.168.1.2",
                Action = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-002" + "-" +  unique,
                Passthrough = false,
            });
            expected.Add(new FirewallMangle()
            {
                Chain = "forward",
                SrcAddress = "192.168.1.3",
                Action = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-003",
                Passthrough = false,
                Comment = unique,
            });
            expected.Add(endMangle);

            connection.CreateMerge(expected, original)
                .WithKey(mangle => mangle.SrcAddress + ":" + mangle.Comment) //Use src-address as key
                .Field(q => q.Chain)
                .Field(q => q.SrcAddress) //Do not forget include also key fields !!!
                .Field(q => q.Action)
                .Field(q => q.NewPacketMark)
                .Field(q => q.Passthrough)
                .Field(q => q.Comment)
                .Save();
        }
Пример #2
0
        private static void FirewallMangleMerge(ITikConnection connection)
        {
            //manage just subset before rules marked with comment =START= and =END=

            //Create subset boundaries if not present
            const string startComment = "=START=";
            const string endComment   = "=END=";
            var          startMangle  = connection.LoadSingleOrDefault <FirewallMangle>(connection.CreateParameter("comment", startComment));

            if (startMangle == null)
            {
                startMangle = new FirewallMangle()
                {
                    Chain    = "forward",
                    Action   = FirewallMangle.ActionType.Passthrough,
                    Comment  = startComment,
                    Disabled = true,
                };
                connection.Save(startMangle);
            }
            ;
            var endMangle = connection.LoadSingleOrDefault <FirewallMangle>(connection.CreateParameter("comment", endComment));

            if (endMangle == null)
            {
                endMangle = new FirewallMangle()
                {
                    Chain    = "forward",
                    Action   = FirewallMangle.ActionType.Passthrough,
                    Comment  = endComment,
                    Disabled = true,
                };
                connection.Save(endMangle);
            }
            ;

            //Merge subset between boundaries
            string unique = Guid.NewGuid().ToString();
            List <FirewallMangle> original = connection.LoadAll <FirewallMangle>().SkipWhile(m => m.Comment != startComment).TakeWhile(m => m.Comment != endComment)
                                             .Concat(new List <FirewallMangle> {
                endMangle
            })
                                             .ToList(); //just subset between =START= and =END= (not very elegant but functional and short ;-) )
            List <FirewallMangle> expected = new List <FirewallMangle>();

            expected.Add(startMangle);
            expected.Add(new FirewallMangle()
            {
                Chain         = "forward",
                SrcAddress    = "192.168.1.1",
                Action        = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-001",
                Passthrough   = false,
            });
            expected.Add(new FirewallMangle()
            {
                Chain         = "forward",
                SrcAddress    = "192.168.1.2",
                Action        = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-002" + "-" + unique,
                Passthrough   = false,
            });
            expected.Add(new FirewallMangle()
            {
                Chain         = "forward",
                SrcAddress    = "192.168.1.3",
                Action        = FirewallMangle.ActionType.MarkPacket,
                NewPacketMark = "mark-003",
                Passthrough   = false,
                Comment       = unique,
            });
            expected.Add(endMangle);

            connection.CreateMerge(expected, original)
            .WithKey(mangle => mangle.SrcAddress + ":" + mangle.Comment) //Use src-address as key
            .Field(q => q.Chain)
            .Field(q => q.SrcAddress)                                    //Do not forget include also key fields !!!
            .Field(q => q.Action)
            .Field(q => q.NewPacketMark)
            .Field(q => q.Passthrough)
            .Field(q => q.Comment)
            .Save();
        }