Example #1
0
        public static void UpdateRule(string name, Windows.Models.Rule updatedRule)
        {
            Policy policy      = new Policy();
            var    locatedRule = policy.Rules.FirstOrDefault(r => r.Name == name);

            if (locatedRule == null)
            {
                throw new ArgumentOutOfRangeException(nameof(name), "No rule with that name exists");
            }

            /// See <see cref="FirewallAPI.Rule"/> for description of native type
            locatedRule.Name            = updatedRule.Name;
            locatedRule.Action          = (FirewallAPI.Action)updatedRule.Action;
            locatedRule.Protocol        = (int)updatedRule.Protocol;
            locatedRule.Direction       = (FirewallAPI.RuleDirection)updatedRule.Direction;
            locatedRule.LocalAddresses  = updatedRule.LocalAddresses;
            locatedRule.LocalPorts      = updatedRule.LocalPorts;
            locatedRule.RemoteAddresses = updatedRule.RemoteAddresses;
            locatedRule.RemotePorts     = updatedRule.RemotePorts;
        }
Example #2
0
        public static void NewRule(Windows.Models.Rule rule)
        {
            // There is currently no strongly typed way to do this with
            // the firewall API developed. FirewallAPI.Rule is backed by
            // a COM type which will commit immediately. As such, the individual
            // properties are past instead.

            // TODO: Windows Firewall mapping: This is a bit of a dirty way to do the mapping
            FirewallAPI.Action action = (FirewallAPI.Action)rule.Action;
            int protocol = (int)rule.Protocol;

            FirewallAPI.RuleDirection direction = (FirewallAPI.RuleDirection)rule.Direction;

            Policy policy = new Policy();

            policy.Rules.Add(rule.Name, action, protocol, direction,
                             rule.LocalAddresses, rule.LocalPorts, rule.RemoteAddresses, rule.RemotePorts);


            //NewRuleFirewallClient.Execute(rule.Name, (int) rule.Protocol, rule.LocalPorts, action);
        }