获取控制台应用程序而不是服务的Windows防火墙提示符

本文关键字:Windows 防火墙 提示符 服务 控制台 应用程序 获取 | 更新日期: 2023-09-27 18:13:33

我有一个c# . net应用程序,它从网络上的其他设备接收TCP和UDP流。

当我将它作为控制台应用程序运行时,Windows防火墙提示我:"Windows防火墙已阻止此程序的某些功能",并要求我允许vshost32.exe在网络上通信。

我同意,这个应用程序运行得很好。

然而,当我运行应用程序作为一个服务(我有一个单独的控制台和服务包装器),我没有这样的提示,我只能让它工作,如果关闭防火墙。

这是服务的期望吗?()

另外,我读了一些代码片段,建议您可以手动添加例外到Windows防火墙列表。这只适用于主机应用,还是也适用于服务?

我的一些代码监听端口,以防这是有用的…

        //
        // Setup UDP listening
        //
        if (protocol == "UDP")
        {
            m_udp = new UdpConn("RedwallReceiver UDP", m_local, new NetAddress());
            m_udp.Receive(new VDataHandler(ReceiveData));
        }
        //
        // Setup TCP listening
        //
        if (protocol == "TCP")
        {
            m_listener = new TcpListener(m_local);
            m_listener.Start();
            m_listener.BeginAcceptSocket(AcceptSocket, null);
        }

获取控制台应用程序而不是服务的Windows防火墙提示符

服务在受限制的环境中执行,并且被允许很少或根本不与UI交互。他的回答涵盖了所有的推理,下面是如何达到同样的目的。

我建议在您的解决方案中添加一个额外的项目(让我们称之为Configurator),它可以作为安装过程的一部分启动。据我所知,向防火墙添加规则需要管理权限。步骤如下:

  • 创建Configurator项目作为控制台或WinForms应用程序。这里不需要UI
  • Configurator项目添加应用程序清单文件。右键单击项目,添加>新项目>应用程序清单文件。修改<requestedExecutionLevel>标签为<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  • Configurator项目的输出添加到您的设置/部署项目。
  • 选择部署项目并导航到Custom Actions选项卡。在Commit节点下添加一个新的自定义动作,并使其指向Configurator项目的输出。
  • Configurator项目中,从COM引用中添加NetFwTypeLib的引用。
  • 将以下代码添加到Configurator项目中。

修改Configurator项目的Main方法,使其返回一个int(0表示成功,非0表示失败),并使用以下代码。请注意,我是从我的项目直接粘贴的,所以你可能需要修复一些声明错误,等等。

private static int Main (string [] args)
{
    var application = new NetFwAuthorizedApplication()
    {
        Name = "MyService",
        Enabled = true,
        RemoteAddresses = "*",
        Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL,
        IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY,
        ProcessImageFileName = "ServiceAssemblyName.dll",
    };
    return (FirewallUtilities.AddApplication(application, out exception) ? 0 : -1);
}
namespace MySolution.Configurator.Firewall
{
    using System;
    using System.Linq;
    using NetFwTypeLib;
    public sealed class NetFwAuthorizedApplication:
        INetFwAuthorizedApplication
    {
        public string Name { get; set; }
        public bool Enabled { get; set; }
        public NET_FW_SCOPE_ Scope { get; set; }
        public string RemoteAddresses { get; set; }
        public string ProcessImageFileName { get; set; }
        public NET_FW_IP_VERSION_ IpVersion { get; set; }
        public NetFwAuthorizedApplication ()
        {
            this.Name = "";
            this.Enabled = false;
            this.RemoteAddresses = "";
            this.ProcessImageFileName = "";
            this.Scope = NET_FW_SCOPE_.NET_FW_SCOPE_ALL;
            this.IpVersion = NET_FW_IP_VERSION_.NET_FW_IP_VERSION_ANY;
        }
        public NetFwAuthorizedApplication (string name, bool enabled, string remoteAddresses, NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipVersion, string processImageFileName)
        {
            this.Name = name;
            this.Scope = scope;
            this.Enabled = enabled;
            this.IpVersion = ipVersion;
            this.RemoteAddresses = remoteAddresses;
            this.ProcessImageFileName = processImageFileName;
        }
        public static NetFwAuthorizedApplication FromINetFwAuthorizedApplication (INetFwAuthorizedApplication application)
        {
            return (new NetFwAuthorizedApplication(application.Name, application.Enabled, application.RemoteAddresses, application.Scope, application.IpVersion, application.ProcessImageFileName));
        }
    }
}
namespace MySolution.Configurator.Firewall
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using NetFwTypeLib;
    public static class FirewallUtilities
    {
        public static bool GetApplication (string processImageFileName, out INetFwAuthorizedApplication application, out Exception exception)
        {
            var result = false;
            var comObjects = new Stack<object>();
            exception = null;
            application = null;
            if (processImageFileName == null) { throw (new ArgumentNullException("processImageFileName")); }
            if (processImageFileName.Trim().Length == 0) { throw (new ArgumentException("The argument [processImageFileName] cannot be empty.", "processImageFileName")); }
            try
            {
                var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true);
                try
                {
                    var manager = (INetFwMgr) Activator.CreateInstance(type);
                    comObjects.Push(manager);
                    try
                    {
                        var policy = manager.LocalPolicy;
                        comObjects.Push(policy);
                        var profile = policy.CurrentProfile;
                        comObjects.Push(profile);
                        var applications = profile.AuthorizedApplications;
                        comObjects.Push(applications);
                        foreach (INetFwAuthorizedApplication app in applications)
                        {
                            comObjects.Push(app);
                            if (string.Compare(app.ProcessImageFileName, processImageFileName, true, CultureInfo.InvariantCulture) == 0)
                            {
                                result = true;
                                application = NetFwAuthorizedApplication.FromINetFwAuthorizedApplication(app);
                                break;
                            }
                        }
                        if (!result) { throw (new Exception("The requested application was not found.")); }
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }
                finally
                {
                    while (comObjects.Count > 0)
                    {
                        ComUtilities.ReleaseComObject(comObjects.Pop());
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
            }
            return (result);
        }
        public static bool AddApplication (INetFwAuthorizedApplication application, out Exception exception)
        {
            var result = false;
            var comObjects = new Stack<object>();
            exception = null;
            if (application == null) { throw (new ArgumentNullException("application")); }
            try
            {
                var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true);
                try
                {
                    var manager = (INetFwMgr) Activator.CreateInstance(type);
                    comObjects.Push(manager);
                    try
                    {
                        var policy = manager.LocalPolicy;
                        comObjects.Push(policy);
                        var profile = policy.CurrentProfile;
                        comObjects.Push(profile);
                        var applications = profile.AuthorizedApplications;
                        comObjects.Push(applications);
                        applications.Add(application);
                        result = true;
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }
                finally
                {
                    while (comObjects.Count > 0)
                    {
                        ComUtilities.ReleaseComObject(comObjects.Pop());
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
            }
            return (result);
        }
        public static bool RemoveApplication (string processImageFileName, out Exception exception)
        {
            var result = false;
            var comObjects = new Stack<object>();
            exception = null;
            if (processImageFileName == null) { throw (new ArgumentNullException("processImageFileName")); }
            if (processImageFileName.Trim().Length == 0) { throw (new ArgumentException("The argument [processImageFileName] cannot be empty.", "processImageFileName")); }
            try
            {
                var type = Type.GetTypeFromProgID("HNetCfg.FwMgr", true);
                try
                {
                    var manager = (INetFwMgr) Activator.CreateInstance(type);
                    comObjects.Push(manager);
                    try
                    {
                        var policy = manager.LocalPolicy;
                        comObjects.Push(policy);
                        var profile = policy.CurrentProfile;
                        comObjects.Push(profile);
                        var applications = profile.AuthorizedApplications;
                        comObjects.Push(applications);
                        applications.Remove(processImageFileName);
                        result = true;
                    }
                    catch (Exception e)
                    {
                        exception = e;
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }
                finally
                {
                    while (comObjects.Count > 0)
                    {
                        ComUtilities.ReleaseComObject(comObjects.Pop());
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
            }
            finally
            {
            }
            return (result);
        }
    }
}