示例#1
0
        private void createRule(bool doAllow, bool isTemp)
        {
            bool success    = false;
            var  activeConn = ((CurrentConn)lstConnections.SelectedItem);

            if ((!_optionsView.IsProtocolChecked) && (_optionsView.IsLocalPortChecked || _optionsView.IsTargetPortChecked))
            {
                MessageBox.Show(Common.Properties.Resources.MSG_RULE_PROTOCOL_NEEDED, Common.Properties.Resources.MSG_DLG_ERR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string[] services = null;
            if (_optionsView.IsServiceRuleChecked)
            {
                if (activeConn.PossibleServices != null && activeConn.PossibleServices.Length > 0)
                {
                    ServicesForm sf = new ServicesForm(activeConn);
                    if ((bool)sf.ShowDialog())
                    {
                        services = sf.SelectedServices;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    services = new[] { activeConn.CurrentService };
                }
            }

            if (doAllow)
            {
                success = createAllowRule(activeConn, services, isTemp);
            }
            else
            {
                success = createBlockRule(activeConn, services, isTemp);
            }

            if (success)
            {
                LogHelper.Info("New rule for connection successfully created!");

                for (int i = ((App)Application.Current).Connections.Count - 1; i >= 0; i--)
                {
                    var      c   = ((App)Application.Current).Connections[i];
                    string[] svc = new string[0];
                    if (!String.IsNullOrEmpty(c.CurrentService))
                    {
                        svc = new[] { c.CurrentService };
                    }
                    if (FirewallHelper.GetMatchingRules(c.CurrentPath, c.CurrentAppPkgId, c.Protocol, c.Target, c.TargetPort, c.LocalPort, svc, c.CurrentLocalUserOwner, false).Any()) //FIXME: LocalPort may have multiple!)
                    {
                        LogHelper.Debug("Auto-removing a similar connection...");
                        ((App)Application.Current).Connections.Remove(c);
                    }
                }

                if (((App)Application.Current).Connections.Count == 0)
                {
                    LogHelper.Debug("No connections left; closing notification window.");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show(Common.Properties.Resources.MSG_RULE_FAILED, Common.Properties.Resources.MSG_DLG_ERR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
示例#2
0
        private void createRule(bool doAllow)
        {
            var  createTempRule            = (bool)togTempRule.IsChecked;
            var  createWithAdvancedOptions = !expand.IsExpanded;
            bool success;
            var  activeConn = (CurrentConn)lstConnections.SelectedItem;

            if (activeConn is null)
            {
                return;
            }

            if ((!OptionsView.IsProtocolChecked) && (OptionsView.IsLocalPortChecked || OptionsView.IsTargetPortChecked))
            {
                MessageBox.Show(Messages.MSG_RULE_PROTOCOL_NEEDED, Messages.MSG_DLG_ERR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            string[] services = null;
            if (OptionsView.IsServiceRuleChecked)
            {
                if (activeConn.PossibleServices != null && activeConn.PossibleServices.Length > 0)
                {
                    ServicesForm sf = new ServicesForm(activeConn);
                    if (!(bool)sf.ShowDialog())
                    {
                        return;
                    }
                    services = sf.SelectedServices;
                }
                else
                {
                    services = new[] { activeConn.CurrentService };
                }
            }

            var ruleName = String.Format(Messages.RULE_NAME_FORMAT, activeConn.CurrentServiceDesc ?? activeConn.Description);

            if (doAllow)
            {
                success = createAllowRule(activeConn, services, createWithAdvancedOptions, createTempRule, ruleName);
            }
            else
            {
                success = createBlockRule(activeConn, services, createWithAdvancedOptions, createTempRule, ruleName);
            }

            if (success)
            {
                LogHelper.Info("New rule for connection successfully created!");
                if (!createWithAdvancedOptions)
                {
                    SkipAllEntriesFromPath(activeConn.Path);
                }
                else
                {
                    SkipAllEntriesFromRules();
                }

                if (((App)Application.Current).Connections.Count == 0)
                {
                    LogHelper.Debug("No connections left; closing notification window.");
                    HideWindowState();
                }
            }
            else
            {
                MessageBox.Show(Messages.MSG_RULE_FAILED, Messages.MSG_DLG_ERR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }