基本信息
源码名称:C# 操作防火墙(ipseccmd)示例源码
源码大小:0.13M
文件格式:.zip
开发语言:C#
更新时间:2018-03-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace fhq
{
    public partial class Form1 : Form
    {
        private delegate void FlushClient();//代理
        public int i_time, i_again_max;
        public Thread th;
        public string str_ip = "";
        public DateTime dt_writen = System.DateTime.Now;//开始运行时间取当前时间

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            i_time = Convert.ToInt32(textBox_time.Text);
            i_again_max = Convert.ToInt32(textBox_again_max.Text);
            bt_start_Click(this, e);
            bt_start.Enabled = false; 

        }

        private void CrossThreadFlush()
        {
            while (true)
            {
                //将sleep和无限循环放在等待异步的外面
                Thread.Sleep(i_time);
                ThreadFunction();
            }
            //FlushClient fc = new FlushClient(ThreadFunction);


            //fc.BeginInvoke(null, null);
        }
        private void ThreadFunction()
        {
            string strIP, result, ls_doscmd = "", ls_ip_temp = "", str_output = "";
            int position1, position2, i_again = 1, i = 1, i_seatch;

            EventLog sample = new EventLog();
            sample.Log = "application";

            if (this.textBox1.InvokeRequired)//等待异步
            {
                FlushClient fc = new FlushClient(ThreadFunction);
                this.Invoke(fc);//通过代理调用刷新方法
            }
            else
            {
                label5.Text = DateTime.Now.ToString();

                label4.Text = "0";
                EventLogEntryCollection mycollection = sample.Entries;
                foreach (EventLogEntry test in mycollection)
                {

                    if (test.EventID == 18456 && test.TimeWritten > dt_writen)
                    {
                        strIP = test.Message;
                        position1 = strIP.IndexOf(":");
                        position2 = strIP.IndexOf("]");
                        result = strIP.Substring(position1 2, position2 - position1 - 2);
                        if (result == ls_ip_temp)
                        {
                            i_again = 1;
                        }
                        ls_ip_temp = result;
                        if (i_again >= i_again_max && (result != "172.16.0.2" && result != "172.16.0.4" && result != "<local machine>"))
                        {
                            i_seatch = str_ip.IndexOf(result);
                            if (i_seatch < 0)
                            {
                                str_ip = " -f " result "=0::0 ";
                                ls_doscmd = "ipseccmd -w REG -p \"stopDBattack\" -r \"IPstop\" " str_ip "-n BLOCK -x ";
                                textBox_cmd.Text = ls_doscmd "[" DateTime.Now.ToString() "]" "\r\n";
                                str_output = Execute(ls_doscmd, 0);
                            }


                        }
                        //Debug.Print ("事件ID:" test.EventID "\n" 
                        //   "来源:" test.Source "类型:" test.EntryType "\n" result);
                        textBox1.Text = "IP:" result " 攻击时间:" test.TimeWritten str_output;
                        //textBox1.Text = textBox1.Text "事件ID:" test.EventID "\n" 
                        //    "来源:" test.Source "类型:" test.EntryType "\n" result;
                        dt_writen = test.TimeWritten;

                    }
                    label4.Text = (i ).ToString();
                    ;
                }

            }
        }




        private void bt_set_Click(object sender, EventArgs e)
        {
            i_time = Convert.ToInt32(textBox_time.Text);

        }


        private void bt_start_Click(object sender, EventArgs e)
        {
            th = new Thread(new ThreadStart(CrossThreadFlush));
            th.IsBackground = true;
            th.Start();
            bt_stop.Enabled = true;
            bt_start.Enabled = false;
        }

        private void bt_stop_Click(object sender, EventArgs e)
        {
            th.Abort();
            bt_start.Enabled = true;
            bt_stop.Enabled = false;
        }

        public static string Execute(string dosCommand, int outtime)
        {
            string output = "";
            if (dosCommand != null && dosCommand != "")
            {
                Process process = new Process();//创建进程对象
                ProcessStartInfo startinfo = new ProcessStartInfo();//创建进程时使用的一组值,如下面的属性
                startinfo.FileName = "cmd.exe";//设定需要执行的命令程序
                //以下是隐藏cmd窗口的方法
                startinfo.Arguments = "/c" dosCommand;//设定参数,要输入到命令程序的字符,其中“/c"表示执行完命令后马上退出
                startinfo.UseShellExecute = false;//不使用系统外壳程序启动
                startinfo.RedirectStandardInput = false;//不重定向输入
                startinfo.RedirectStandardOutput = true;//重定向输出,而不是默认的显示在dos控制台上
                startinfo.CreateNoWindow = true;//不创建窗口
                process.StartInfo = startinfo;

                try
                {
                    if (process.Start())//开始进程
                    {
                        if (outtime == 0)
                        { process.WaitForExit(); }
                        else
                        { process.WaitForExit(outtime); }
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出
                    }
                }
                catch
                {

                }
                finally
                {
                    if (process != null)
                    { process.Close(); }
                }
            }
            return output;
        }

        private void bt_again_max_Click(object sender, EventArgs e)
        {
            i_again_max = Convert.ToInt32(textBox_again_max.Text);
        }




    }
}