2015-05-30 57 views
1

我需要在域網絡防火牆中爲我的應用程序打開特定端口。如何在域網絡中打開端口防火牆

我曾嘗試這樣的代碼:

INetFwOpenPorts ports; 
INetFwOpenPort port = (INetFwOpenPort)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FWOpenPort")); ; 

port.Port = 8000; /* port no */ 
port.Name = "Application1"; /*name of the application using the port */ 
port.Enabled = true; /* enable the port */ 
port.Scope = NetFwTypeLib.NET_FW_SCOPE_.NET_FW_SCOPE_ALL; 
port.Protocol = NetFwTypeLib.NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP;//.NET_FW_IP_PROTOCO L_TCP; 

Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false); 
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType); 
ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts; 
ports.Add(port); 

,但它不工作!並且我的數據不會發送,直到Windows防火牆中的域網絡爲ON!

回答

0

在第一步驟中,添加一個參考:

C:\ Windows \ System32下\ FirewallAPI.dll

下面是類具有方法:

  • GloballyOpenPort - 網絡窗口防火牆中的開放端口
  • SetProfilesForRule - Set profil ES DOMAIN,私有,公共爲規則
public class Firewall 
{ 
    private const string CLSID_FIREWALL_MANAGER = "{304CE942-6E39-40D8-943A-B913C40C9CD4}"; 
    private const string PROGID_AUTHORIZED_APPLICATION = "HNetCfg.FwAuthorizedApplication"; 
    private const string PROGID_OPEN_PORT = "HNetCfg.FWOpenPort"; 
    private const string PROGID_POLITCY2 = "HNetCfg.FwPolicy2"; 

    [Flags] 
    public enum PROFILE { DOMAIN = 1, PRIVATE = 2, PUBLIC = 5 }; 

    /// <summary> 
    /// Create instance of the INetFwMgr that provides access to the firewall settings for a computer. 
    /// </summary> 
    /// <returns></returns> 
    private static INetFwMgr GetFirewallManager() 
    { 
     Type objectType = Type.GetTypeFromCLSID(new Guid(CLSID_FIREWALL_MANAGER)); 

     return Activator.CreateInstance(objectType) as NetFwTypeLib.INetFwMgr; 
    } 


    /// <summary> 
    /// Enable firewall 
    /// </summary> 
    public static void Enable() 
    { 
     INetFwMgr manager = Firewall.GetFirewallManager(); 

     bool isFirewallEnabled = manager.LocalPolicy.CurrentProfile.FirewallEnabled; 

     if (isFirewallEnabled == false) 
      manager.LocalPolicy.CurrentProfile.FirewallEnabled = true; 
    } 


    /// <summary> 
    /// Authorize application 
    /// </summary> 
    /// <param name="title"></param> 
    /// <param name="applicationPath"></param> 
    /// <param name="scope"></param> 
    /// <param name="ipVersion"></param> 
    /// <returns></returns> 
    public static bool AuthorizeApplication(string title, string applicationPath, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion) 
    { 
     // Create the type from prog id 
     Type type = Type.GetTypeFromProgID(PROGID_AUTHORIZED_APPLICATION); 

     // Create instance that provides access to the properties of an application that has been authorized have openings in the firewall. 
     INetFwAuthorizedApplication auth = Activator.CreateInstance(type) as INetFwAuthorizedApplication; 
     auth.Name = title; 
     auth.ProcessImageFileName = applicationPath; 
     auth.Scope = scope; 
     auth.IpVersion = ipVersion; 
     auth.Enabled = true; 


     INetFwMgr manager = GetFirewallManager(); 
     try 
     { 
      manager.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(auth); 
     } 
     catch (Exception ex) 
     { 
      return false; 
     } 
     return true; 
    } 

    /// <summary> 
    /// Open port in network windows firewall 
    /// </summary> 
    /// <param name="name"></param> 
    /// <param name="portNo"></param> 
    /// <param name="scope"></param> 
    /// <param name="protocol"></param> 
    /// <param name="ipVersion"></param> 
    /// <returns></returns> 
    public static bool GloballyOpenPort(string name, int portNo, 
             NET_FW_SCOPE_ scope, NET_FW_IP_PROTOCOL_ protocol, NET_FW_IP_VERSION_ ipVersion) 
    { 
     INetFwMgr manager = GetFirewallManager(); 
     try 
     { 
      // Check if port does not exists. 
      bool exists = false; 
      foreach (INetFwOpenPort openPort in manager.LocalPolicy.CurrentProfile.GloballyOpenPorts) 
      { 
       if (openPort.Name == name && openPort.Port == portNo) 
       { 
        exists = true; 
        break; 
       } 
      } 

      if (!exists) 
      { 
       // Create the type from prog id 
       Type type = Type.GetTypeFromProgID(PROGID_OPEN_PORT); 
       // Create instance that provides access to the properties of a port that has been opened in the firewall. 
       INetFwOpenPort port = Activator.CreateInstance(type) as INetFwOpenPort; 

       // Set properties for port 
       port.Name = name; 
       port.Port = portNo; 
       port.Scope = scope; 
       port.Protocol = protocol; 
       port.IpVersion = ipVersion; 

       // Add open port to windows firewall 
       manager.LocalPolicy.CurrentProfile.GloballyOpenPorts.Add(port); 
      } 
     } 
     catch (Exception ex) 
     { 
      return false; 
     } 
     return true; 
    } 

    /// <summary> 
    /// Set profiles for rule 
    /// </summary> 
    /// <param name="name">Name of rule</param> 
    /// <param name="profiles">bitmask value: 3 - public; 2 - private; 1 - domain</param> 
    /// <returns></returns> 
    public static bool SetProfilesForRule(string name, int profiles) 
    { 
     try 
     { 
      // Create the type from prog id 
      Type typePolicy2 = Type.GetTypeFromProgID(PROGID_POLITCY2); 
      // Create instance that allows an application or service to access the firewall policy. 
      INetFwPolicy2 policy2 = Activator.CreateInstance(typePolicy2) as INetFwPolicy2; 

      // Set profiles for rule      
      policy2.Rules.Item(name).Profiles = profiles; 
     } 
     catch (Exception ex) 
     { 
      return false; 
     } 
     return true; 
    } 
} 

用於規則設定型材invoke方法的實施例:

在方法第一個參數是規則的名稱

第二參數是我們可以通過枚舉類型將其設置爲位掩碼類型簡介

int profile =(int)(PROFILE.DOMAIN | PROFILE.PRIVATE | PROFILE.PUBLIC); SetProfilesForRule(「RuleName」,profile);

相關問題