放开防火墙拦截的超时的黑名单IP

复制代码
 /// <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失败!");
                }
            }
        }
复制代码

 

posted @   极客船长  阅读(153)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示