private bool MakeCustom(Program prog, UInt64 expiration, ConEntry entry = null) { FirewallRule rule = new FirewallRule() { guid = null, Profile = (int)FirewallRule.Profiles.All, Interface = (int)FirewallRule.Interfaces.All, Enabled = true }; rule.SetProgID(prog.ID); rule.Name = FirewallManager.MakeRuleName(FirewallManager.CustomName, expiration != 0, prog.Description); rule.Grouping = FirewallManager.RuleGroup; if (entry != null) { rule.Direction = entry.Entry.FwEvent.Direction; rule.Protocol = (int)entry.Entry.FwEvent.Protocol; switch (entry.Entry.FwEvent.Protocol) { /*case (int)FirewallRule.KnownProtocols.ICMP: * case (int)FirewallRule.KnownProtocols.ICMPv6: * * break;*/ case (int)FirewallRule.KnownProtocols.TCP: case (int)FirewallRule.KnownProtocols.UDP: rule.LocalPorts = "*"; rule.RemotePorts = entry.Entry.FwEvent.RemotePort.ToString(); break; } rule.LocalAddresses = "*"; rule.RemoteAddresses = entry.Entry.FwEvent.RemoteAddress.ToString(); } else { rule.Direction = FirewallRule.Directions.Bidirectiona; } RuleWindow ruleWnd = new RuleWindow(new List <Program>() { prog }, rule); if (ruleWnd.ShowDialog() != true) { return(false); } if (!App.client.UpdateRule(rule, expiration)) { MessageBox.Show(Translate.fmt("msg_rule_failed"), App.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation); return(false); } return(true); }
private void btnOK_Click(object sender, RoutedEventArgs e) { if (!checkAll()) { return; } Rule.Name = txtName.Text; Rule.Grouping = cmbGroup.Text; Rule.Description = txtInfo.Text; if (ProgIDChanged) { Rule.SetProgID((cmbProgram.SelectedItem as ContentControl).Tag as ProgramID); } Rule.Action = (FirewallRule.Actions)(cmbAction.SelectedItem as ContentControl).Tag; if (radProfileAll.IsChecked == true || (chkPrivate.IsChecked == true && chkDomain.IsChecked == true && chkPublic.IsChecked == true)) { Rule.Profile = (int)FirewallRule.Profiles.All; } else { Rule.Profile = (int)FirewallRule.Profiles.Undefined; if (chkPrivate.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Private; } if (chkDomain.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Domain; } if (chkPublic.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Public; } } if (radProfileAll.IsChecked == true || (chkPrivate.IsChecked == true && chkDomain.IsChecked == true && chkPublic.IsChecked == true)) { Rule.Profile = (int)FirewallRule.Profiles.All; } else { Rule.Profile = (int)FirewallRule.Profiles.Undefined; if (chkPrivate.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Private; } if (chkDomain.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Domain; } if (chkPublic.IsChecked == true) { Rule.Profile |= (int)FirewallRule.Profiles.Public; } } if (radNicAll.IsChecked == true || (chkLAN.IsChecked == true && chkVPN.IsChecked == true && chkWiFi.IsChecked == true)) { Rule.Interface = (int)FirewallRule.Interfaces.All; } else { Rule.Interface = 0; if (chkLAN.IsChecked == true) { Rule.Interface |= (int)FirewallRule.Interfaces.Lan; } if (chkVPN.IsChecked == true) { Rule.Interface |= (int)FirewallRule.Interfaces.RemoteAccess; } if (chkWiFi.IsChecked == true) { Rule.Interface |= (int)FirewallRule.Interfaces.Wireless; } } Rule.Direction = (FirewallRule.Directions)(cmbDirection.SelectedItem as ContentControl).Tag; Rule.Protocol = curProtocol.Value; if (Rule.Protocol == (int)FirewallRule.KnownProtocols.TCP || Rule.Protocol == (int)FirewallRule.KnownProtocols.UDP) { if (cmbRemotePorts.SelectedItem != null) { Rule.RemotePorts = (string)((cmbRemotePorts.SelectedItem as ContentControl).Tag); } else { Rule.RemotePorts = cmbRemotePorts.Text.Replace(" ", ""); // white spaces are not valid } if (cmbLocalPorts.SelectedItem != null) { Rule.LocalPorts = (string)((cmbLocalPorts.SelectedItem as ContentControl).Tag); } else { Rule.LocalPorts = cmbLocalPorts.Text.Replace(" ", ""); // white spaces are not valid } } else if (Rule.Protocol == (int)FirewallRule.KnownProtocols.ICMP || Rule.Protocol == (int)FirewallRule.KnownProtocols.ICMPv6) { if (cmbICMP.SelectedItem != null) { Rule.SetIcmpTypesAndCodes((cmbICMP.SelectedItem as ContentControl).Tag as string); } else { Rule.SetIcmpTypesAndCodes(cmbICMP.Text); } } Rule.RemoteAddresses = addrDest.Address; Rule.LocalAddresses = addrSrc.Address; this.DialogResult = true; }