Example #1
0
        public AntdFirewallModule()
        {
            Get["/firewall"] = x => {
                var firewallIsActive = FirewallConfiguration.IsActive();
                var model            = new PageFirewallModel {
                    FirewallIsActive = firewallIsActive,
                    FwIp4Filter      = FirewallConfiguration.Get()?.Ipv4FilterTable,
                    FwIp4Nat         = FirewallConfiguration.Get()?.Ipv4NatTable,
                    FwIp6Filter      = FirewallConfiguration.Get()?.Ipv6FilterTable,
                    FwIp6Nat         = FirewallConfiguration.Get()?.Ipv6NatTable
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/firewall/set"] = x => {
                FirewallConfiguration.Set();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/restart"] = x => {
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/stop"] = x => {
                FirewallConfiguration.Stop();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/enable"] = x => {
                FirewallConfiguration.Enable();
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/disable"] = x => {
                FirewallConfiguration.Disable();
                FirewallConfiguration.Stop();
                return(HttpStatusCode.OK);
            };

            #region [    IPV4    ]
            Post["/firewall/ipv4/filter/set"] = x => {
                string set      = Request.Form.Set;
                string type     = Request.Form.Type;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(set) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4FilterTable;
                var sets      = table.Sets.ToList();
                var tryGetSet = sets.FirstOrDefault(_ => _.Name == set && _.Type == type);
                if (tryGetSet == null)
                {
                    sets.Add(new FirewallSet {
                        Name = set, Type = type, Elements = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    sets.Remove(tryGetSet);
                    tryGetSet.Elements = elements.SplitToList().ToArray();
                    sets.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv4/filter/chain"] = x => {
                string chain    = Request.Form.Chain;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(chain) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4FilterTable;
                var chains    = table.Chains.ToList();
                var tryGetSet = chains.FirstOrDefault(_ => _.Name == chain);
                if (tryGetSet == null)
                {
                    chains.Add(new FirewallChain {
                        Name = chain, Rules = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    chains.Remove(tryGetSet);
                    tryGetSet.Rules = elements.SplitToList().ToArray();
                    chains.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv4/nat/set"] = x => {
                string set      = Request.Form.Set;
                string type     = Request.Form.Type;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(set) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4NatTable;
                var sets      = table.Sets.ToList();
                var tryGetSet = sets.FirstOrDefault(_ => _.Name == set && _.Type == type);
                if (tryGetSet == null)
                {
                    sets.Add(new FirewallSet {
                        Name = set, Type = type, Elements = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    sets.Remove(tryGetSet);
                    tryGetSet.Elements = elements.SplitToList().ToArray();
                    sets.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv4/nat/chain"] = x => {
                string chain    = Request.Form.Chain;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(chain) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4NatTable;
                var chains    = table.Chains.ToList();
                var tryGetSet = chains.FirstOrDefault(_ => _.Name == chain);
                if (tryGetSet == null)
                {
                    chains.Add(new FirewallChain {
                        Name = chain, Rules = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    chains.Remove(tryGetSet);
                    tryGetSet.Rules = elements.SplitToList().ToArray();
                    chains.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };
            #endregion

            #region [    IPV6    ]
            Post["/firewall/ipv6/filter/set"] = x => {
                string set      = Request.Form.Set;
                string type     = Request.Form.Type;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(set) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4FilterTable;
                var sets      = table.Sets.ToList();
                var tryGetSet = sets.FirstOrDefault(_ => _.Name == set && _.Type == type);
                if (tryGetSet == null)
                {
                    sets.Add(new FirewallSet {
                        Name = set, Type = type, Elements = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    sets.Remove(tryGetSet);
                    tryGetSet.Elements = elements.SplitToList().ToArray();
                    sets.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv6/filter/chain"] = x => {
                string chain    = Request.Form.Chain;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(chain) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4FilterTable;
                var chains    = table.Chains.ToList();
                var tryGetSet = chains.FirstOrDefault(_ => _.Name == chain);
                if (tryGetSet == null)
                {
                    chains.Add(new FirewallChain {
                        Name = chain, Rules = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    chains.Remove(tryGetSet);
                    tryGetSet.Rules = elements.SplitToList().ToArray();
                    chains.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv6/nat/set"] = x => {
                string set      = Request.Form.Set;
                string type     = Request.Form.Type;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(set) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4NatTable;
                var sets      = table.Sets.ToList();
                var tryGetSet = sets.FirstOrDefault(_ => _.Name == set && _.Type == type);
                if (tryGetSet == null)
                {
                    sets.Add(new FirewallSet {
                        Name = set, Type = type, Elements = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    sets.Remove(tryGetSet);
                    tryGetSet.Elements = elements.SplitToList().ToArray();
                    sets.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };

            Post["/firewall/ipv6/nat/chain"] = x => {
                string chain    = Request.Form.Chain;
                string elements = Request.Form.Elements;
                if (string.IsNullOrEmpty(chain) || string.IsNullOrEmpty(elements))
                {
                    return(HttpStatusCode.BadRequest);
                }
                var table     = FirewallConfiguration.Get().Ipv4NatTable;
                var chains    = table.Chains.ToList();
                var tryGetSet = chains.FirstOrDefault(_ => _.Name == chain);
                if (tryGetSet == null)
                {
                    chains.Add(new FirewallChain {
                        Name = chain, Rules = elements.SplitToList().ToArray()
                    });
                }
                else
                {
                    chains.Remove(tryGetSet);
                    tryGetSet.Rules = elements.SplitToList().ToArray();
                    chains.Add(tryGetSet);
                }
                FirewallConfiguration.Start();
                return(HttpStatusCode.OK);
            };
            #endregion
        }