如何在域网络windows防火墙中打开端口

本文关键字:防火墙 windows 网络 | 更新日期: 2023-09-27 17:51:09

我需要在域网络防火墙中为我的应用程序打开特定的端口。

我试过这个代码:

 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!

如何在域网络windows防火墙中打开端口

第一步,添加引用:

C: ' Windows ' System32系统' FirewallAPI.dll

下面的类有方法:

  • GloballyOpenPort -打开网络windows防火墙的端口
  • SetProfilesForRule -设置配置文件域,私有,公共规则
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;
    }
}

为规则设置概要文件的调用方法示例:

方法中的第一个参数是规则的名称

第二个参数是int型的配置文件类型,我们可以将其设置为来自enum类型profile

的位掩码

int profile = (int)(profile;域名|配置文件。私人|PROFILE.PUBLIC);SetProfilesForRule("RuleName",简介);