将一个C#应用程序移植到Mono,需要使用FirewallAPI。是否有与Mono等效的解决方案?

3

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

它使用了FirewallAPI.dll(我想这应该是实际名称...)。其他名称为NetFwTypeLb、 NATUPNPLib和NETCONLib。

我一直在通过谷歌搜索尝试找到在Mac / Linux平台上实现这一点的方法,但我找不到可用的方法。

这样做是否可行?并且与此问题结合的另一个问题是:Mac / Linux平台是否允许安装和运行服务(我认为也称为“守护进程”)?

谢谢, Madeline

只是为了说明,以下是我正在使用的当前代码,我从另一个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;
    }
}

对于守护进程,请查看使用Mono构建的mono-service - Mike Christensen
1个回答

0

我在弄清楚所有问题时忘记回答这个问题了!

在OS X上,无需创建防火墙例外。OS X会要求用户允许您的应用程序访问互联网。

但是我不确定Linux是否也是如此,但是在Linux上,Mono覆盖率要高得多,因此我相信有人已经回答过这个问题了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接