我们有很多机器,当其中一个客户要求阻止某些IP时,它可能会非常痛苦。我们运行游戏服务器,因此通常需要阻止的IP可以是任何IP,任何端口等。
我想编写一个小应用程序来简化在Server 2008中添加IP禁令。有没有什么好方法可以做到这一点,无论是通过IPSec还是Windows防火墙?有些机器关闭了防火墙,因此首选IPSec,但两者都可以。
答案 0 :(得分:3)
非常感谢您的链接。我能够使用以下代码实现此目的。您仍然需要获取要使用的FWManager对象。
private void btnBlock_Click(object sender, EventArgs e)
{
String IP = txtAddress.Text;
txtAddress.Clear();
if (IsAddressValid(IP))
{
INetFwRule2 firewallRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Name = "BrutalNT: IP Access Block " + txtAddress.Text;
firewallRule.Description = "Block Incoming Connections from IP Address.";
firewallRule.Action = NET_FW_ACTION_.NET_FW_ACTION_BLOCK;
firewallRule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.RemoteAddresses = txtAddress.Text;
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(firewallRule);
String msg = "IP Address \"" + IP + "\" Blocked Successfully!";
MessageBox.Show(msg, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
String msg = "IP Address \"" + IP + "\" was Invalid!";
MessageBox.Show(msg, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
答案 1 :(得分:0)
前段时间我也在寻找类似的解决方案。最后我们选择了一种不同的方法来解决我们的问题,但我还记得有一个用于Windows防火墙的API。 不幸的是,我没有保存网址,但你可以谷歌“C#windows防火墙API”。这里有几个链接:
修改强> 类似的问题:What are my options for adding and removing IPSec policies on Windows Server with C#?
祝你好运!