Ejemplo n.º 1
0
        public bool IsEnabled(FirewallRuleDto rule)
        {
            var fwRule = ExtractFwRule(rule);

            if (fwRule != null)
            {
                return(fwRule.Enabled);
            }

            throw DontExistOrSeveralFound(rule);
        }
Ejemplo n.º 2
0
        public async Task <CommandResult <FirewallRuleDto> > Put([FromBody] FirewallRuleDto firewallRule)
        {
            if (!ModelState.IsValid)
            {
                return(new CommandResult <FirewallRuleDto>()
                {
                    IsSucceeded = false,
                    Message = "Validation problem. Check your values"
                });
            }

            return(await _mediator.Send(new Update.Command()
            {
                FirewallRule = firewallRule
            }));
        }
Ejemplo n.º 3
0
        public FirewallRuleDto CreateFirewallRule(CreateFirewallRuleDto rule)
        {
            var(parseOk, ips, proto, ports) = ParseRuleData(rule);
            if (parseOk)
            {
                if (Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")) is INetFwRule3 fwRule)
                {
                    fwRule.Name           = rule.Name;
                    fwRule.Enabled        = rule.CreateRuleEnabled;
                    fwRule.InterfaceTypes = "All";
                    fwRule.Action         = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
                    fwRule.Direction      = (NET_FW_RULE_DIRECTION_)rule.Direction;
                    fwRule.Profiles       = (int)rule.Profile;

                    if (!string.IsNullOrEmpty(ips))
                    {
                        fwRule.RemoteAddresses = ips;
                    }

                    if (proto != null)
                    {
                        fwRule.Protocol    = proto.Value;
                        fwRule.RemotePorts = ports;
                    }

                    if (!string.IsNullOrEmpty(rule.ProgramPath))
                    {
                        fwRule.ApplicationName = rule.ProgramPath;
                    }

                    firewallPolicy.Rules.Add(fwRule);
                    var firewallRuleDto = new FirewallRuleDto
                    {
                        FwRule      = fwRule,
                        Profile     = rule.Profile,
                        Direction   = rule.Direction,
                        Name        = rule.Name,
                        ProgramPath = rule.ProgramPath
                    };
                    UpdateRulesGroup(rule.Profile, rule.Direction, firewallRuleDto);

                    return(firewallRuleDto);
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private INetFwRule3 ExtractFwRule(FirewallRuleDto rule)
        {
            if (rule != null)
            {
                if (rule.FwRule != null)
                {
                    return(rule.FwRule);
                }

                if (TryGetRules(rule.Name, rule.Profile, rule.Direction, out var rules) && rules.Count() == 1)
                {
                    return(rules.Single().FwRule);
                }
            }

            return(null);
        }
Ejemplo n.º 5
0
        private RuleControl ProcessRule(RuleDto rule, List <RuleDto> removeRules, RuleControl prev)
        {
            var matchingFwRules      = firewallService.GetMatchingRules(rule.Name, rule.Profile, rule.Direction);
            var matchingFwRulesCount = matchingFwRules.Count();

            if (matchingFwRulesCount == 0)
            {
                DeleteRule(rule, removeRules);
            }
            else
            {
                FirewallRuleDto fwRule = null;
                if (matchingFwRulesCount > 1)
                {
                    using (var selectRule = new SelectRuleForm(rule, matchingFwRules))
                    {
                        if (selectRule.ShowDialog() == DialogResult.OK)
                        {
                            fwRule = selectRule.SelectedRule;
                        }
                    }
                }
                else
                {
                    fwRule = matchingFwRules.Single();
                }

                if (fwRule == null)
                {
                    DeleteRule(rule, removeRules);
                }
                else
                {
                    var ruleControl = prev == null
                        ? new RuleControl(rule, fwRule, FIRST_RULE_LOCATION, firewallService)
                        : new RuleControl(rule, fwRule, prev, firewallService);
                    return(ruleControl);
                }
            }

            return(null);
        }
Ejemplo n.º 6
0
        private RuleControl(
            RuleDto rule, FirewallRuleDto firewallRuleDto,
            RuleControl previous, Point?location,
            IFirewallService firewallService)
        {
            if (previous == null && location == null)
            {
                throw new ArgumentNullException(
                          $"{nameof(previous)},{nameof(location)}");
            }

            InitializeComponent();

            Previous             = previous;
            Rule                 = rule;
            this.firewallRuleDto = firewallRuleDto;
            this.firewallService = firewallService;

            MyInitializeComponent(location);
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Update(FirewallRuleDto model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var result = await _mediator.Send(new Update.Command()
            {
                FirewallRule = model
            });

            if (!result.IsSucceeded)
            {
                return(View(model));
            }

            Alert(AlertTypes.Success, "Firewall Rule successfully updated!");

            return(RedirectToAction(nameof(Index), new { id = model.TargetId }));
        }
Ejemplo n.º 8
0
 public RuleControl(
     RuleDto rule, FirewallRuleDto firewallRuleDto,
     Point location, IFirewallService firewallService)
     : this(rule, firewallRuleDto, null, location, firewallService)
 {
 }
Ejemplo n.º 9
0
 public RuleControl(
     RuleDto rule, FirewallRuleDto firewallRuleDto,
     RuleControl previous, IFirewallService firewallService)
     : this(rule, firewallRuleDto, previous, null, firewallService)
 {
 }
Ejemplo n.º 10
0
        private void UpdateRulesGroup(ProfileDto profile, RuleDirectionDto direction, FirewallRuleDto firewallRuleDto)
        {
            Group group = null;

            foreach (var g in groupedRules)
            {
                if (g.Profile == profile && g.Direction == direction)
                {
                    group = g; break;
                }
            }

            group = group ?? new Group
            {
                Direction = direction,
                Profile   = profile,
                Rules     = Enumerable.Empty <FirewallRuleDto>()
            };

            group.Rules = group.Rules
                          .Concat(new[] { firewallRuleDto })
                          .OrderBy(r => r.Name);
        }
Ejemplo n.º 11
0
 private static InvalidOperationException DontExistOrSeveralFound(FirewallRuleDto rule)
 {
     return(new InvalidOperationException("Rule " + rule?.Name + " doesn't exists."));
 }