示例#1
0
 private void RulesOnItemsModified(object sender, ActiveCollectionChangedEventArgs <IRule> e)
 {
     lock (_rules)
     {
         if (e.ActionType == ActiveCollectionChangeType.Added)
         {
             var rule = e.Item as StandardRule;
             if (rule != null)
             {
                 UnderlyingObject.Rules.Add(rule.UnderlyingObject);
             }
         }
         else if (e.ActionType == ActiveCollectionChangeType.Removed)
         {
             var rule = e.Item as StandardRule;
             if (rule != null)
             {
                 UnderlyingObject.Rules.Remove(rule.UnderlyingObject.Name);
             }
         }
     }
     SyncRules();
 }
 private void RulesOnItemsModified(object sender, ActiveCollectionChangedEventArgs <IRule> e)
 {
     lock (_rules)
     {
         if (e.ActionType == ActiveCollectionChangeType.Added)
         {
             if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Public))
             {
                 throw new FirewallAPIv1NotSupportedException();
             }
             // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
             if (e.Item is ApplicationRule)
             {
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Domain))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN)
                     .AuthorizedApplications.Add(((ApplicationRule)e.Item).UnderlyingObject);
                 }
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Private))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD)
                     .AuthorizedApplications.Add(((ApplicationRule)e.Item).UnderlyingObject);
                 }
             }
             else if (e.Item is PortRule)
             {
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Domain))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN)
                     .GloballyOpenPorts.Add(((PortRule)e.Item).UnderlyingObject);
                 }
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Private))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD)
                     .GloballyOpenPorts.Add(((PortRule)e.Item).UnderlyingObject);
                 }
             }
         }
         else if (e.ActionType == ActiveCollectionChangeType.Removed)
         {
             if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Public))
             {
                 throw new FirewallAPIv1NotSupportedException();
             }
             // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
             if (e.Item is ApplicationRule)
             {
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Domain))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN)
                     .AuthorizedApplications.Remove(
                         ((ApplicationRule)e.Item).UnderlyingObject.ProcessImageFileName);
                 }
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Private))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD)
                     .AuthorizedApplications.Remove(
                         ((ApplicationRule)e.Item).UnderlyingObject.ProcessImageFileName);
                 }
             }
             else if (e.Item is PortRule)
             {
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Domain))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_DOMAIN)
                     .GloballyOpenPorts.Remove(((PortRule)e.Item).UnderlyingObject.Port,
                                               ((PortRule)e.Item).UnderlyingObject.Protocol);
                 }
                 if (EnumHelper.HasFlag(e.Item.Profiles, FirewallProfiles.Private))
                 {
                     UnderlyingObject.LocalPolicy.GetProfileByType(NET_FW_PROFILE_TYPE_.NET_FW_PROFILE_STANDARD)
                     .GloballyOpenPorts.Remove(((PortRule)e.Item).UnderlyingObject.Port,
                                               ((PortRule)e.Item).UnderlyingObject.Protocol);
                 }
             }
         }
     }
     SyncRules();
 }