public FwErrorCode IsPortEnabled(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, ref bool bEnable) { if (_mFirewallProfile == null) { return(FwErrorCode.FwErrInitialized); } // Retrieve the open ports collection INetFwOpenPorts fwOpenPorts = _mFirewallProfile.GloballyOpenPorts; if (fwOpenPorts == null) { return(FwErrorCode.FwErrGlobalOpenPorts); } // Get the open port try { INetFwOpenPort fwOpenPort = fwOpenPorts.Item(nPortNumber, ipProtocol); bEnable = fwOpenPort?.Enabled == true; } catch (System.IO.FileNotFoundException) { bEnable = false; } return(FwErrorCode.FwNoerror); }
public bool RemovePort(int port, NET_FW_IP_PROTOCOL_ ipProtocol) { DoDisposeCheck(); DoInitializationCheck(); bool changed = false; if (IsPortEnabled(port, ipProtocol)) { // Retrieve the collection of globally open ports INetFwOpenPorts firewallOpenPorts = mFirewallProfile.GloballyOpenPorts; if (firewallOpenPorts == null) { throw new FirewallException("Failed to get globally open ports."); } try { firewallOpenPorts.Remove(port, ipProtocol); changed = true; } catch (Exception ex) { throw new FirewallException("Failed to remove port.", ex); } } return(changed); }
/// <summary> /// Creates a new port entry in the firewall collection. /// </summary> /// <param name="port">The port number.</param> /// <param name="name">The name of the port.</param> /// <param name="protocol">The protocol used.</param> /// <param name="scope">The scope of the control.</param> public void OpenFirewallPort(int port, string name, NET_FW_IP_PROTOCOL_ protocol, NET_FW_SCOPE_ scope) { // Set the current access profile. SetProfile(); // Get the current globall // open port profile control. INetFwOpenPorts openPorts = fwProfile.GloballyOpenPorts; // Create a new instance of the // open new port type. INetFwOpenPort openPort = (INetFwOpenPort)GetInstance("INetOpenPort"); // Assign the port specifications. openPort.Port = port; openPort.Name = name; openPort.Scope = scope; openPort.Protocol = protocol; // Add the new port to the // collection of ports. openPorts.Add(openPort); openPorts = null; }
public bool AddPort(ushort portNumber, String appName) { bool result = false; try { INetFwMgr fwMgr = (INetFwMgr)getInstance("INetFwMgr"); INetFwPolicy fwPolicy = fwMgr.LocalPolicy; INetFwProfile fwProfile = fwPolicy.CurrentProfile; INetFwOpenPorts ports = fwProfile.GloballyOpenPorts; INetFwOpenPort port = (INetFwOpenPort)getInstance("INetOpenPort"); port.Port = portNumber; /* port no */ port.Name = appName; /*name of the application using the port */ port.Enabled = true; /* enable the port */ /*other properties like Protocol, IP Version can also be set accordingly * now add this to the GloballyOpenPorts collection */ Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; ports.Add(port); result = true; } catch (UnauthorizedAccessException ex) { result = false; } return(result); }
public FW_ERROR_CODE IsPortEnabled(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, ref bool bEnable) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } // Retrieve the open ports collection INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } // Get the open port try { INetFwOpenPort FWOpenPort = FWOpenPorts.Item(nPortNumber, ipProtocol); if (FWOpenPort != null) { bEnable = FWOpenPort.Enabled; } else { bEnable = false; } } catch (FileNotFoundException) { bEnable = false; } return(FW_ERROR_CODE.FW_NOERROR); }
public bool IsPortEnabled(int port, NET_FW_IP_PROTOCOL_ ipProtocol) { DoDisposeCheck(); DoInitializationCheck(); bool result = false; // Retrieve the open ports collection INetFwOpenPorts firewallOpenPorts = mFirewallProfile.GloballyOpenPorts; if (firewallOpenPorts == null) { throw new FirewallException("Failed to get globally open ports."); } // Get the open port try { INetFwOpenPort firewallOpenPort = firewallOpenPorts.Item(port, ipProtocol); if (firewallOpenPort != null) { result = firewallOpenPort.Enabled; } } catch (Exception) { } return(result); }
internal static void ClosePortInFirewall(int port) { if (!FirewallEnabled) { return; } //Remove the Logging Port from the Windows Firewall exception list //NOTE: this will not work if the user is not an Admin INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); //HACK: Verify because having the firewall dialog enabled //does not result in a UnauthorizedAccess exception, even if the operation fails bool removed = true; INetFwOpenPorts openPorts = fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts; foreach (INetFwOpenPort openPort in openPorts) { if (openPort.Port == port) { removed = false; break; } } if (!removed) { throw new UnauthorizedAccessException(); } }
protected internal void OpenFirewall() { INetFwAuthorizedApplications authApps = null; INetFwAuthorizedApplication authApp = null; INetFwOpenPorts openPorts = null; INetFwOpenPort openPort = null; try { if (isAppFound(Application.ProductName + " Server") == false) { SetProfile(); authApps = fwProfile.AuthorizedApplications; authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication; authApp.Name = Application.ProductName + " Server"; authApp.ProcessImageFileName = Application.ExecutablePath; authApps.Add(authApp); } if (isPortFound(portsSocket[0]) == false) { SetProfile(); openPorts = fwProfile.GloballyOpenPorts; openPort = GetInstance("INetOpenPort") as INetFwOpenPort; openPort.Port = portsSocket[0]; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = portsName[0]; openPorts.Add(openPort); } if (isPortFound(portsSocket[1]) == false) { SetProfile(); openPorts = fwProfile.GloballyOpenPorts; openPort = GetInstance("INetOpenPort") as INetFwOpenPort; openPort.Port = portsSocket[1]; openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; openPort.Name = portsName[1]; openPorts.Add(openPort); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (authApps != null) { authApps = null; } if (authApp != null) { authApp = null; } if (openPorts != null) { openPorts = null; } if (openPort != null) { openPort = null; } } }
/// /// Opens the given ports on windows firewall. Also opens entirely the given application on the firewall. /// Please remember that you need administrative rights to use this function. /// /// The path of the application. Please include the ending \ /// The name of the executable to open /// The ports that you wish to open individually public void openFirewall(string executableFilePath, string applicationName, int[] portsToOpen) { ///////////// Firewall Authorize Application //////////// try { setProfile(); INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications; INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)getInstance("INetAuthApp"); app.Name = applicationName; app.ProcessImageFileName = executableFilePath; apps.Add(app); apps = null; //////////////// Open Needed Ports ///////////////// INetFwOpenPorts openports = fwProfile.GloballyOpenPorts; foreach (int port in portsToOpen) { INetFwOpenPort openport = (INetFwOpenPort)getInstance("INetOpenPort"); openport.Port = port; openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; openport.Name = applicationName + " Operation Port (" + port.ToString() + ")"; openports.Add(openport); } openports = null; } catch (Exception) { // throw; } Console.WriteLine("Firewall : Open Ports"); }
public Firewall() { policyManager = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); manager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); profile = manager.LocalPolicy.CurrentProfile; openPorts = profile.GloballyOpenPorts; }
public MainWindow() { InitializeComponent(); // Open the port in the firewall Type type = Type.GetTypeFromProgID("HNetCfg.FWOpenPort"); INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; port.Port = 19283; port.Name = "Mayhem"; port.Enabled = true; Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); INetFwOpenPorts ports = mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; ports.Add(port); // Add the ACL string name = WindowsIdentity.GetCurrent().Name; SecurityIdentity sid = SecurityIdentity.SecurityIdentityFromName(name); string acl = "D:(A;;GA;;;" + sid + ")"; Debug.WriteLine(acl); SetHttpNamespaceAcl("http://+:19283/", acl); Close(); }
public static void CloseFirewallPort(string port) { Type netFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(netFwMgrType); INetFwOpenPorts openPorts = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; openPorts.Remove(Convert.ToInt32(port), NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); }
private static INetFwOpenPorts GetOpenPorts() { INetFwMgr manager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); INetFwProfile profile = manager.LocalPolicy.CurrentProfile; INetFwOpenPorts openPorts = profile.GloballyOpenPorts; return(openPorts); }
public FW_ERROR_CODE AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName) { if (m_FirewallProfile == null) { return(FW_ERROR_CODE.FW_ERR_INITIALIZED); } bool bEnablePort = true; FW_ERROR_CODE nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FW_ERROR_CODE.FW_NOERROR) { return(nError); } // Only add the port, if it isn't added to the collection if (bEnablePort == false) { // Retrieve the collection of globally open ports INetFwOpenPorts FWOpenPorts = m_FirewallProfile.GloballyOpenPorts; if (FWOpenPorts == null) { return(FW_ERROR_CODE.FW_ERR_GLOBAL_OPEN_PORTS); } // Create an instance of an open port Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); var FWOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort); if (FWOpenPort == null) { return(FW_ERROR_CODE.FW_ERR_CREATE_PORT_INSTANCE); } // Set the port number FWOpenPort.Port = nPortNumber; // Set the IP Protocol FWOpenPort.Protocol = ipProtocol; // Set the registered name FWOpenPort.Name = strRegisterName; try { FWOpenPorts.Add(FWOpenPort); } catch { return(FW_ERROR_CODE.FW_ERR_ADD_TO_COLLECTION); } } else { return(FW_ERROR_CODE.FW_ERR_SAME_PORT_EXIST); } return(FW_ERROR_CODE.FW_NOERROR); }
internal static void OpenPortInFirewall(int port, string description, string processExePath) { if (!FirewallEnabled) { return; } INetFwMgr fwManager = (INetFwMgr)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwMgr")); //On Longhorn if there is this application is blocked, opening the port //won't do any good. Because this happens when you dismiss the firewall //dialog we make sure the app is not explicitly blocked in case someone //accidentily ran the app under LUA and dismissed the dialog. INetFwAuthorizedApplications apps = fwManager.LocalPolicy.CurrentProfile.AuthorizedApplications; string currentAppPath = processExePath; foreach (INetFwAuthorizedApplication app in apps) { if (app.ProcessImageFileName.ToLowerInvariant() == currentAppPath.ToLowerInvariant() && !app.Enabled) { apps.Remove(currentAppPath); break; } } //Make sure that the port is Logging Port is open in the Windows Firewall so that we dont get dialogs on this //NOTE: this will not work if the user is not an Admin INetFwOpenPort portExclusion = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); portExclusion.Port = port; portExclusion.Enabled = true; portExclusion.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY; portExclusion.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; portExclusion.Name = description; portExclusion.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(portExclusion); //HACK: Verify because having the firewall dialog enabled //does not result in a UnauthorizedAccess exception, even if the operation fails bool added = false; INetFwOpenPorts openPorts = fwManager.LocalPolicy.CurrentProfile.GloballyOpenPorts; foreach (INetFwOpenPort openPort in openPorts) { if (openPort.Port == port) { added = true; break; } } if (!added) { throw new UnauthorizedAccessException(); } }
/// <summary> /// Apre tutte le porte sia TCP che UDP per un determinato Exe /// </summary> /// <param name="appName">Nome che comparira' nella regola del Firewall</param> /// <param name="exePath">Percorso alleseguibile da sbloccare sul Firewall</param> protected internal void OpenFirewallExe(string appName = "", string exePath = "") { INetFwAuthorizedApplications authApps = null; INetFwAuthorizedApplication authApp = null; INetFwOpenPorts openPorts = null; INetFwOpenPort openPort = null; if (appName == "") { appName = Application.ProductName; } if (exePath == "") { exePath = Application.ExecutablePath; } try { if (isAppFound(Application.ProductName) == false) { SetProfile(); authApps = fwProfile.AuthorizedApplications; authApp = GetInstance("INetAuthApp") as INetFwAuthorizedApplication; authApp.Name = appName; authApp.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL; authApp.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY; authApp.ProcessImageFileName = exePath; authApps.Add(authApp); logger.Info("Aggiunta regola generale per " + appName + ": " + exePath); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (authApps != null) { authApps = null; } if (authApp != null) { authApp = null; } if (openPorts != null) { openPorts = null; } if (openPort != null) { openPort = null; } } }
public static void RemoveRdpRule() { INetFwOpenPorts openPorts = GetOpenPorts(); openPorts.Remove(RdpTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); openPorts.Remove(RdpUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP); INetFwPolicy2 policyManager = GetPolicyManager(); policyManager.Rules.Remove(RdpRuleName); }
internal static void ClosePort(int port) { if (!IsOpenPort(port)) { return; } INetFwOpenPorts openPorts = FirewallProfile.GloballyOpenPorts; openPorts.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); }
public static void ClosePort(int port) { EnsureSetup(); if (!IsPortOpen(port)) { return; } INetFwOpenPorts ports = sm_fwProfile.GloballyOpenPorts; ports.Remove(port, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); }
public FwErrorCode AddPort(int nPortNumber, NET_FW_IP_PROTOCOL_ ipProtocol, string strRegisterName) { if (_mFirewallProfile == null) { return(FwErrorCode.FwErrInitialized); } bool bEnablePort = true; FwErrorCode nError = IsPortEnabled(nPortNumber, ipProtocol, ref bEnablePort); if (nError != FwErrorCode.FwNoerror) { return(nError); } // Only add the port, if it isn't added to the collection if (bEnablePort == false) { // Retrieve the collection of globally open ports INetFwOpenPorts fwOpenPorts = _mFirewallProfile.GloballyOpenPorts; if (fwOpenPorts == null) { return(FwErrorCode.FwErrGlobalOpenPorts); } // Create an instance of an open port Type typeFwPort = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); var fwOpenPort = (INetFwOpenPort)Activator.CreateInstance(typeFwPort); // Set the port number fwOpenPort.Port = nPortNumber; // Set the IP Protocol fwOpenPort.Protocol = ipProtocol; // Set the registered name fwOpenPort.Name = strRegisterName; try { fwOpenPorts.Add(fwOpenPort); } catch { return(FwErrorCode.FwErrAddToCollection); } } else { return(FwErrorCode.FwErrSamePortExist); } return(FwErrorCode.FwNoerror); }
public void FirewallOpenPort(int port, string protocol, string appName) { ///////////// Firewall Authorize Application //////////// String imageFilename = Utils.EmuleAdunanzAExeGetPath(); setProfile(); INetFwAuthorizedApplications apps = fwProfile.AuthorizedApplications; INetFwAuthorizedApplication app = (INetFwAuthorizedApplication)GetInstance("INetAuthApp"); app.Name = appName; app.ProcessImageFileName = imageFilename; apps.Add(app); //////////////// Open Needed Ports ///////////////// INetFwOpenPorts openports = fwProfile.GloballyOpenPorts; INetFwOpenPort openport = (INetFwOpenPort)GetInstance("INetOpenPort"); openport.Port = port; try { if (protocol.ToLower() == "udp") { openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; } else if (protocol.ToLower() == "tcp") { openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; } else { openport.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_ANY; } openport.Name = appName + " " + protocol; openports.Add(openport); } catch (Exception ex) { logger.Error(ex.Message); } finally { if (apps != null) { apps = null; } if (openport != null) { openport = null; } logger.Info("Firewall: aggiunta regola per {0} su porta {1}: {2}", appName, port, protocol); } } // openFirewall
protected internal void OpenFirewallPort(int port, string protocol, string appName) { INetFwAuthorizedApplications authApps = null; INetFwAuthorizedApplication authApp = null; INetFwOpenPorts openPorts = null; INetFwOpenPort openPort = null; try { if (isPortFound(port) == false) { SetProfile(); openPorts = fwProfile.GloballyOpenPorts; openPort = GetInstance("INetOpenPort") as INetFwOpenPort; openPort.Port = port; if (protocol.ToLower() == "udp") { openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP; } else if (protocol.ToLower() == "tcp") { openPort.Protocol = NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP; } openPort.Name = appName + " " + protocol.ToUpper(); openPorts.Add(openPort); logger.Info("Firewall: Aggiunta regola per " + appName + ": " + protocol.ToUpper()); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (authApps != null) { authApps = null; } if (authApp != null) { authApp = null; } if (openPorts != null) { openPorts = null; } if (openPort != null) { openPort = null; } } }
/// <summary> /// Removes the port entry from the firewall collection. /// </summary> /// <param name="port">The port number.</param> /// <param name="protocol">The protocol used.</param> public void CloseFirewallPort(int port, NET_FW_IP_PROTOCOL_ protocol) { // Set the current access profile. SetProfile(); // Get the current globall // open port profile control. INetFwOpenPorts openPorts = fwProfile.GloballyOpenPorts; // Remove the specified port from the collection. openPorts.Remove(port, protocol); openPorts = null; }
protected internal bool isPortFound(int portNumber) { bool boolResult = false; INetFwOpenPorts ports = null; Type progID = null; INetFwMgr firewall = null; INetFwOpenPort currentPort = null; try { // TODO: aprire in pubblico e privato // ref: http://stackoverflow.com/questions/15409790/adding-an-application-firewall-rule-to-both-private-and-public-networks-via-win7 progID = Type.GetTypeFromProgID("HNetCfg.FwMgr"); firewall = Activator.CreateInstance(progID) as INetFwMgr; ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts; IEnumerator portEnumerate = ports.GetEnumerator(); while ((portEnumerate.MoveNext())) { currentPort = portEnumerate.Current as INetFwOpenPort; if (currentPort.Port == portNumber) { boolResult = true; break; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (ports != null) { ports = null; } if (progID != null) { progID = null; } if (firewall != null) { firewall = null; } if (currentPort != null) { currentPort = null; } } return(boolResult); }
public bool AddPort(int port, NET_FW_IP_PROTOCOL_ ipProtocol, string registeredName) { DoDisposeCheck(); DoInitializationCheck(); if (string.IsNullOrEmpty(registeredName)) { ThrowHelper.ThrowArgumentNullException("registeredName"); } bool result = false; if (!IsPortEnabled(port, ipProtocol)) { // Retrieve the collection of globally open ports INetFwOpenPorts firewallOpenPorts = mFirewallProfile.GloballyOpenPorts; if (firewallOpenPorts == null) { throw new FirewallException("Failed to get globally open ports."); } // Create an instance of an open port Type firewallPortType = Type.GetTypeFromCLSID(new Guid("{0CA545C6-37AD-4A6C-BF92-9F7610067EF5}")); INetFwOpenPort firewallOpenPort = (INetFwOpenPort)Activator.CreateInstance(firewallPortType); if (firewallOpenPort == null) { throw new FirewallException("Failed to create port instance."); } // Set the port number firewallOpenPort.Port = port; // Set the IP Protocol firewallOpenPort.Protocol = ipProtocol; // Set the registered name firewallOpenPort.Name = registeredName; try { firewallOpenPorts.Add(firewallOpenPort); result = true; } catch (Exception ex) { throw new FirewallException("Failed to add port.", ex); } } return(result); }
protected internal bool isPortFound(int portNumber) { bool boolResult = false; INetFwOpenPorts ports = null; Type progID = null; INetFwMgr firewall = null; INetFwOpenPort currentPort = null; try { progID = Type.GetTypeFromProgID("HNetCfg.FwMgr"); firewall = Activator.CreateInstance(progID) as INetFwMgr; ports = firewall.LocalPolicy.CurrentProfile.GloballyOpenPorts; IEnumerator portEnumerate = ports.GetEnumerator(); while ((portEnumerate.MoveNext())) { currentPort = portEnumerate.Current as INetFwOpenPort; if (currentPort.Port == portNumber) { boolResult = true; break; } } } catch (Exception ex) { //MessageBox.Show(ex.Message); Console.Write(ex.Message); } finally { if (ports != null) { ports = null; } if (progID != null) { progID = null; } if (firewall != null) { firewall = null; } if (currentPort != null) { currentPort = null; } } return(boolResult); }
internal INetFwOpenPort ReadPort(string name) { INetFwOpenPorts ports = firewallManager.LocalPolicy.CurrentProfile.GloballyOpenPorts; foreach (INetFwOpenPort port in ports) { System.Diagnostics.Debug.Print(port.Name); if (port.Name == name) { return(port); } } return(null); }
/// <summary> /// 仅供单元测试 /// </summary> public static void RemoveRdpRule() { try { INetFwOpenPorts openPorts = GetOpenPorts(); openPorts.Remove(RdpTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); openPorts.Remove(RdpUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP); INetFwPolicy2 policyManager = GetPolicyManager(); policyManager.Rules.Remove(RdpRuleName); } catch (Exception e) { Logger.ErrorDebugLine(e); } }
private static void OpenPort(string name, int port, NET_FW_IP_PROTOCOL_ protocol, NET_FW_SCOPE_ scope) { INetFwOpenPorts openPorts = GetOpenPorts(); if (openPorts.OfType <INetFwOpenPort>().Where(x => x.Name == name).Count() == 0) { INetFwOpenPort openPort = (INetFwOpenPort)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwOpenPort")); openPort.Port = port; openPort.Protocol = protocol; openPort.Scope = scope; openPort.Name = name; openPorts.Add(openPort); } }
public static void RemoveMinerClientRule() { INetFwOpenPorts openPorts = GetOpenPorts(); openPorts.Remove(MinerClientTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); openPorts.Remove(MinerClientUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP); openPorts.Remove(NTMinerDaemonTcpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP); openPorts.Remove(NTMinerDaemonUdpPort, NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP); INetFwPolicy2 policyManager = GetPolicyManager(); policyManager.Rules.Remove(MinerClientRuleName); policyManager.Rules.Remove(NTMinerDaemonRuleName); }