/// <summary>
/// 放开超过指定时间的黑名单IP
/// </summary>
public static void ResetFireWallBlackIp(string ruleName, string hostip, string expireTS)
{
DateTime dt = DateTime.Parse(expireTS);
TimeSpan ts = new TimeSpan(dt.Hour, dt.Minute, dt.Second);
string specDate = DateTime.Now.Subtract(ts).ToString("yyyy-MM-dd HH:mm:ss");
string sql = "select blackip from Filter_BlackIP where hostip='" + hostip + "' and createtime<='" + specDate + "';";
DataSet ds = DB.DataSet(connstr, sql);
if (DataHelper.ExistsDataSet(ds))
{
List<string> blackIpList = (from d in ds.Tables[0].AsEnumerable() select d.Field<string>("blackip") + "/255.255.255.255").ToList();
sql = "delete from Filter_BlackIP where hostip='" + hostip + "' and createtime<='" + specDate + "';";
int res = DB.Query(connstr, sql);
if (res > 0)
{
//清除防火墙黑名单IP
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
var rule = firewallPolicy.Rules.Item(ruleName);
List<string> allBlackIpList = rule.RemoteAddresses.Split(',').ToList();
List<string> remainIPlist = allBlackIpList.Except(blackIpList).ToList();
string ips = string.Join(",", remainIPlist);
rule.RemoteAddresses = ips;
LogHelper.WriteLog("info", "ResetFireWallBlackIp", "重置黑名单" + ips + "成功");
}
else
{
LogHelper.WriteLog("error", "ResetFireWallBlackIp", "重置黑名单IP失败!");
}
}
}