将 C# 应用程序移植到 Mono,需要使用 FirewallAPI.有没有单声道等价物

本文关键字:FirewallAPI 没有单 等价物 声道 应用程序 Mono | 更新日期: 2023-09-27 18:37:09

我已经编写了一个Windows服务,我需要将其移植到Mono,以便它可以在Mac/Linux平台上使用。

它利用了防火墙API.dll(我认为这是实际名称... 其他名称是NetFwTypeLb,NATUPNPLib和NETCONLib。

我一直在谷歌搜索,试图找到一种在Mac/Linux平台上实现它的方法,但我找不到可以用来做到这一点的方法。

这可能吗? 并将另一个问题与这个问题结合起来:Mac/Linux平台是否允许轻松安装和运行服务(我认为也称为"守护程序")?

谢谢马德琳

请注意,这是我正在使用的当前代码,我从另一个 StackOverflow 问题中得到了它:

public class Firewall
{
    public static INetFwMgr WinFirewallManager()
    {
        Type type = Type.GetTypeFromCLSID(
            new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}"));
        return Activator.CreateInstance(type) as INetFwMgr;
    }
    public bool AuthorizeProgram(string title, string path,
        NET_FW_SCOPE_ scope, NET_FW_IP_VERSION_ ipver)
    {
        Type type = Type.GetTypeFromProgID("HNetCfg.FwAuthorizedApplication");
        INetFwAuthorizedApplication authapp = Activator.CreateInstance(type)
            as INetFwAuthorizedApplication;
        authapp.Name = title;
        authapp.ProcessImageFileName = path;
        authapp.Scope = scope;
        authapp.IpVersion = ipver;
        authapp.Enabled = true;
        EventLog.WriteEntry("MachineVerification", authapp.Name + " " + authapp.Scope + " " + authapp.IpVersion);
        INetFwMgr mgr = WinFirewallManager();
        try
        {
            mgr.LocalPolicy.CurrentProfile.AuthorizedApplications.Add(authapp);
            EventLog.WriteEntry("MachineVerification", authapp.Name + " " + authapp.Scope + " " + authapp.IpVersion);
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("MachineVerification", "MROW!" + ex.Message);
            return false;
        }
        return true;
    }
}

将 C# 应用程序移植到 Mono,需要使用 FirewallAPI.有没有单声道等价物

当我弄清楚这一切时,我忘了回答这个问题!

在 OS X 上,无需设置防火墙例外。 OS X 将要求用户授予您的应用程序访问互联网的权限。

我不确定Linux,但是Mono在Linux上的覆盖率要高得多,所以我相信以前有人回答过Linux的这个问题。