Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled        = false;
            numericUpDown1.Enabled = false;
            numericUpDown2.Enabled = false;
            checkBox2.Enabled      = false;
            checkBox1.Enabled      = false;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ///
                /// Копирую таблицы со структурой
                /// Правила - двумерная структура
                /// Сетевые объекты - трёхмерная (двумерная таблица с объектом-списком)
                /// Сервисы - двумерная таблица
                ///

                Rulesd_Delegate d = null;
                d = new Rulesd_Delegate(Rulesd);
                IAsyncResult R = null;
                R     = d.BeginInvoke(openFileDialog1.FileName, null, null);
                Rules = d.EndInvoke(R);

                /*
                 * Это N правил + 1 отрезаем кончающие правила
                 * Далее начальные
                 */

                if (checkBox2.Checked &&
                    numericUpDown1.Value < numericUpDown2.Value &&
                    numericUpDown2.Value < Rules.Count)
                {
                    Rules.RemoveRange(Convert.ToInt32(numericUpDown2.Value) + 1, Rules.Count - Convert.ToInt32(numericUpDown2.Value) - 1);
                    Rules.RemoveRange(0, Convert.ToInt32(numericUpDown1.Value));
                }
                else
                if (checkBox2.Checked)
                {
                    MessageBox.Show("Проверьте правильно ли Вы выставили значения диапазона интересующих правил");
                    return;
                }

                AddTextToStatus("Скопировал Таблицу правил");

                NetworkObjd_Delegate ND = null;
                ND = new NetworkObjd_Delegate(NetworkObjd);
                IAsyncResult NR = null;
                NR         = ND.BeginInvoke(openFileDialog1.FileName, null, null);
                NetworkObj = ND.EndInvoke(NR);

                AddTextToStatus("Скопировал Таблицу Сетевых имён");

                for (int k = 0; k < NetworkObj.Count; k++)
                {
                    for (int l = 0; l < NetworkObj[l].Count; l++)
                    {
                        NetworkObj[k][l] = NetworkObj[k][l].Distinct();
                    }
                }

                Servicesd_Delegate SD = null;
                SD = new Servicesd_Delegate(Servicesd);
                IAsyncResult SR = null;
                SR       = SD.BeginInvoke(openFileDialog1.FileName, null, null);
                Services = SD.EndInvoke(SR);

                AddTextToStatus("Скопировал Таблицу сервисов");

                ///
                /// Открываем книгу Excel.
                ///
                Microsoft.Office.Interop.Excel.Application ObjExcel = new Microsoft.Office.Interop.Excel.Application();
                Workbook  WB;
                Worksheet WS;
                WB = ObjExcel.Workbooks.Add(System.Reflection.Missing.Value);
                WS = (Worksheet)WB.Sheets[1];
                int i = 1;

                AddTextToStatus("Открыл ExCel");

                progressBar1.Maximum = Rules.Count;
                progressBar1.Value   = 0;

                try
                {
                    foreach (List <string> row in Rules)
                    {
                        if (i == 1)
                        {
                            WS.Cells[i, 1].Value  = "Сервис";
                            WS.Cells[i, 2].Value  = "Сегмент источника";
                            WS.Cells[i, 3].Value  = "IP Источника";
                            WS.Cells[i, 4].Value  = "Сегмент назначения";
                            WS.Cells[i, 5].Value  = "IP назначения";
                            WS.Cells[i, 6].Value  = "Порты";
                            WS.Cells[i, 7].Value  = "Решение";
                            WS.Cells[i, 8].Value  = "Установлено";
                            WS.Cells[i, 9].Value  = "Время";
                            WS.Cells[i, 10].Value = "Комментарий";
                            i++;
                            continue;
                        }

                        string[] split = { "\r\n", "\r", "\n" };

                        ///
                        /// Это для Source
                        /// Есть структура данных ResolvedNetObj, которая содержит
                        /// отрезолвленные члены группового объекта
                        /// Создадим список из структуры ResolvedNetObj который будет содержать
                        /// все объекты ячейки источник из политики
                        ///

                        List <string>         CurrentSourceNetObj = row[2].Split(split, StringSplitOptions.RemoveEmptyEntries).ToList();
                        List <ResolvedNetObj> CurrentSrc          = GetResolvedNetObj(CurrentSourceNetObj);
                        AddTextToStatus("Отрезолвил Объекты Источники для " + i.ToString() + " правила");

                        foreach (ResolvedNetObj oneNetObj in CurrentSrc)
                        {
                            if (oneNetObj.ResolvedObj.Count > 1)
                            {
                                foreach (List <string> OneResNetObj in oneNetObj.ResolvedObj)
                                {
                                    if (ToNodeList.Where(n => n.NetObjName == OneResNetObj[0]).Count() == 0)
                                    {
                                        List <ResolvedNetObj> tmpone = GetResolvedNetObj(new List <string> {
                                            OneResNetObj[0]
                                        });
                                        ToNodeList.AddRange(tmpone);
                                        if (ToNodeList.Where(n => n.NetObjName == oneNetObj.NetObjName).Count() == 0)
                                        {
                                            ToNodeList.Add(oneNetObj);
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            if (ToNodeList.Where(n => n.NetObjName == oneNetObj.NetObjName).Count() == 0)
                            {
                                ToNodeList.Add(oneNetObj);
                            }
                        }

                        ///
                        /// Это для Destination
                        ///

                        List <string>         CurrentDestNetObj = row[3].Split(split, StringSplitOptions.RemoveEmptyEntries).ToList();
                        List <ResolvedNetObj> CurrentDst        = GetResolvedNetObj(CurrentDestNetObj);
                        AddTextToStatus("Отрезолвил Объекты Назначения для " + i.ToString() + " правила");

                        foreach (ResolvedNetObj oneNetObj in CurrentDst)
                        {
                            if (oneNetObj.ResolvedObj.Count > 1)
                            {
                                foreach (List <string> OneResNetObj in oneNetObj.ResolvedObj)
                                {
                                    if (ToNodeList.Where(n => n.NetObjName == OneResNetObj[0]).Count() == 0)
                                    {
                                        List <string> one = new List <string> {
                                            OneResNetObj[0]
                                        };
                                        List <ResolvedNetObj> tmpone = GetResolvedNetObj(one);
                                        ToNodeList.AddRange(tmpone);
                                        if (ToNodeList.Where(n => n.NetObjName == oneNetObj.NetObjName).Count() == 0)
                                        {
                                            ToNodeList.Add(oneNetObj);
                                        }
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                }
                            }
                            else
                            if (ToNodeList.Where(n => n.NetObjName == oneNetObj.NetObjName).Count() == 0)
                            {
                                ToNodeList.Add(oneNetObj);
                            }
                        }

                        ///
                        /// Это для портов
                        ///

                        List <string>         CurrenPortObj          = row[5].Split(split, StringSplitOptions.RemoveEmptyEntries).ToList();
                        List <List <string> > CurrentPortObjResolved = new List <List <string> >();

                        progressBar2.Maximum = CurrenPortObj.Count;
                        progressBar2.Value   = 0;

                        foreach (string OnePortObj in CurrenPortObj)
                        {
                            string s = OnePortObj.Replace("Not ", "").Trim();
                            List <List <string> > tmp = ResolvePorts(s);
                            if (OnePortObj.Contains("Not "))
                            {
                                for (int b = 0; b < tmp.Count; b++)
                                {
                                    tmp[b][0] = "Not " + tmp[b][0];
                                }
                            }

                            CurrentPortObjResolved.AddRange(tmp);

                            if (progressBar2.InvokeRequired)
                            {
                                progressBar2.BeginInvoke(new MethodInvoker(() => progressBar2.Value++));
                            }
                            else
                            {
                                progressBar2.Value++;
                            }
                            System.Windows.Forms.Application.DoEvents();
                        }

                        AddTextToStatus("Отрезолвил Объекты порты для " + i.ToString() + " правила");

                        for (int a = 0; a < row.Count; a++)
                        {
                            if (a == 1)
                            {
                                WS.Cells[i, 1].Value = row[a];
                            }

                            if (a == 2)
                            {
                                AddNetworkObj(CurrentSrc, WB, WS, i, 2);
                            }


                            if (a == 3)
                            {
                                AddNetworkObj(CurrentDst, WB, WS, i, 4);
                            }

                            if (a == 5)
                            {
                                string ThisCell = "";
                                foreach (List <string> PortTableRow in CurrentPortObjResolved)
                                {
                                    ThisCell += PortTableRow[0] + " (" + PortTableRow[1] + "/" + PortTableRow[2] + ")" + Environment.NewLine;
                                }
                                WS.Cells[i, 6].Value = ThisCell.Replace("Icmp (Icmp/)", "ICMP")
                                                       .Replace("(ALL_DCE_RPC/DCE-RPC)", "(Все DCE-RPC)")
                                                       .Replace("Any (Any/)", "Any")
                                                       .Replace("l_ALL_DCE_RPC(l_ALL_DCE_RPC / DCE - RPC)", "l_ALL_DCE_RPC (Все DCE-RPC)")
                                                       .Trim();
                            }

                            if (a == 6)
                            {
                                WS.Cells[i, 7].Value = row[a];
                            }

                            if (a == 8)
                            {
                                WS.Cells[i, 8].Value = row[a].Replace("\r\n", "\n").Replace("\n\r", "\n").Replace("\r\r", "\n").Replace("\n\n", "\n").Trim();
                            }

                            if (a == 9)
                            {
                                WS.Cells[i, 9].Value = row[a];
                            }

                            if (a == 10)
                            {
                                WS.Cells[i, 10].Value = row[a].Replace("&nbsp;", "").Trim();
                            }

                            if (row[0].Contains("Disabled"))
                            {
                                Microsoft.Office.Interop.Excel.Range c1 = WS.Cells[i, 1];
                                Microsoft.Office.Interop.Excel.Range c2 = WS.Cells[i, 10];
                                Range oRange = (Microsoft.Office.Interop.Excel.Range)WS.get_Range(c1, c2);
                                oRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                oRange.Font.Bold      = true;
                            }

                            if (a == 4 || a == 7)
                            {
                                continue;
                            }
                        }

                        AddTextToStatus("Записал " + i.ToString() + " правило");
                        i++;

                        if (progressBar1.InvokeRequired)
                        {
                            progressBar1.BeginInvoke(new MethodInvoker(() => progressBar1.Value++));
                        }
                        else
                        {
                            progressBar1.Value++;
                        }
                        System.Windows.Forms.Application.DoEvents();
                    }

                    AddTextToStatus("Добавляю лист Node");
                    AddNodeList(WB);

                    if (checkBox3.Checked)
                    {
                        AddAllNodeList(WB);
                    }


                    if (CurrentErrorObjResolved.Count != 0)
                    {
                        PolycyConverter.Form3 form3 = new PolycyConverter.Form3();
                        form3.ShowDialog(this);
                    }
                    else
                    {
                        PolycyConverter.Form2 form2 = new PolycyConverter.Form2();
                        form2.ShowDialog(this);
                    }
                }
                catch (Exception theException)
                {
                    String errorMessage;
                    errorMessage = "Error: ";
                    errorMessage = String.Concat(errorMessage, theException.Message);
                    errorMessage = String.Concat(errorMessage, " Line: ");
                    errorMessage = String.Concat(errorMessage, theException.Source);

                    MessageBox.Show(errorMessage, "Error");

                    ObjExcel.Columns.AutoFit();
                    ObjExcel.Rows.AutoFit();

                    PolycyConverter.Form3 form3 = new PolycyConverter.Form3();
                    form3.ShowDialog(this);
                    return;
                }
                finally
                {
                    WS.Activate();
                    Microsoft.Office.Interop.Excel.Range c1 = WS.Cells[1, 2];
                    Microsoft.Office.Interop.Excel.Range c2 = WS.Cells[i + 1, 10];
                    Range oRange = (Microsoft.Office.Interop.Excel.Range)WS.get_Range(c1, c2);

                    oRange.Columns.AutoFit();
                    oRange.Rows.AutoFit();

                    WS.Application.ActiveWindow.SplitRow    = 1;
                    WS.Application.ActiveWindow.FreezePanes = true;

                    ObjExcel.Visible     = true;
                    ObjExcel.UserControl = true;

                    Rules.Clear();
                    NetworkObj.Clear();
                    Services.Clear();

                    button1.Enabled        = true;
                    numericUpDown1.Enabled = true;
                    numericUpDown2.Enabled = true;
                    checkBox2.Enabled      = true;
                    checkBox1.Enabled      = true;

                    Marshal.ReleaseComObject(WS);
                    WS = null;
                    Marshal.ReleaseComObject(WB);
                    WB = null;
                    Marshal.ReleaseComObject(ObjExcel);
                    ObjExcel = null;

                    GC.Collect();
                }
            }
        }