Esempio n. 1
0
        private void NATdeleteclick(object sender, MouseButtonEventArgs e)
        {
            //if (NATComboBox.SelectedItem.ToString() == "DNAT")
            //{
            //    StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getDNATRule_list().Remove((DNATdataGrid.SelectedItem as DNATRuleDataTable));
            //    NATApplyEnabled();
            //}
            //if(NATComboBox.SelectedItem.ToString() == "SNAT")
            //{
            //    StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getSNATRule_list().Remove((SNATdataGrid.SelectedItem as SNATRuleDataTable));
            //    NATApplyEnabled();
            //}
            string dev_ip = (from devices in StaticGlobal.fwdev_list
                             where devices.getDev_MAC() == StaticGlobal.firewallmac
                             select devices).ToList <FWDeviceForm>()[0].getProtecDevIP_list()[0];
            IConfigureNAT fw = new ConfigureNAT();

            string origin_devIP = (DNATdataGrid.SelectedItem as SNATRuleDataTable).origin_devIP.ToString();
            string EthIP        = (DNATdataGrid.SelectedItem as SNATRuleDataTable).EthIP.ToString();
            string origin_dport = (DNATdataGrid.SelectedItem as DNATRuleDataTable).origin_dport.ToString();
            string map_port     = (DNATdataGrid.SelectedItem as DNATRuleDataTable).map_port.ToString();

            if (fw.ConfigDNAT(dev_ip, origin_devIP, origin_dport, EthIP, map_port, false))
            {
                UserMessageBox.Show("提示", "规则删除成功!");
            }
        }
Esempio n. 2
0
        private void NATApplying()
        {
            Dispatcher.Invoke(new Action(() =>
            {
                NewNATRulebutton.IsEnabled = false;
                NewNATRulebutton.IsEnabled = false;
                SNATdataGrid.IsEnabled     = false;
                DNATdataGrid.IsEnabled     = false;
            }));
            //找出需要删除的规则
            var SNATdeleteRules = StaticGlobal.SNAToldrules.Where(deleteRule => !StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getSNATRule_list().Contains(deleteRule)).ToList();
            var DNATdeleteRules = StaticGlobal.DNAToldrules.Where(deleteRule => !StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getDNATRule_list().Contains(deleteRule)).ToList();

            //找出需要增加的规则
            var SNATaddRules = StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getSNATRule_list().Where(addRule => !StaticGlobal.SNAToldrules.Contains(addRule)).ToList();
            var DNATaddRules = StaticGlobal.FireWalldevices[StaticGlobal.firewallindex].getDNATRule_list().Where(addRule => !StaticGlobal.DNAToldrules.Contains(addRule)).ToList();

            bool   SNATapplyflag = true;  bool DNATapplyflag = true;
            string dev_ip = (from devices in StaticGlobal.fwdev_list
                             where devices.getDev_MAC() == StaticGlobal.firewallmac
                             select devices).ToList <FWDeviceForm>()[0].getProtecDevIP_list()[0];
            IConfigureNAT NATrules = new ConfigureNAT();

            /*添加NAT规则*/
            for (int i = 0; i < SNATaddRules.Count(); i++)
            {
                string origin_devIP = SNATaddRules[i].origin_devIP;
                string EthName      = SNATaddRules[i].EthName;
                string EthIP        = SNATaddRules[i].EthIP;
                string NATIP        = SNATaddRules[i].NATIP;
                if (NATrules.ConfigSNAT(dev_ip, EthName, origin_devIP, EthIP, true))
                {
                    SNATapplyflag = true;
                }
                else
                {
                    SNATapplyflag = false;
                }
            }
            for (int i = 0; i < DNATaddRules.Count(); i++)
            {
                string origin_dstIP = DNATaddRules[i].origin_dstIP;
                string origin_dport = DNATaddRules[i].origin_dport;
                string map_IP       = DNATaddRules[i].map_IP;
                string map_port     = DNATaddRules[i].map_port;
                if (NATrules.ConfigDNAT(dev_ip, origin_dstIP, origin_dport, map_IP, map_port, true))
                {
                    DNATapplyflag = true;
                }
                else
                {
                    DNATapplyflag = false;
                }
            }
            /*删除NAT规则*/
            for (int i = 0; i < SNATdeleteRules.Count(); i++)
            {
                string origin_devIP = SNATdeleteRules[i].origin_devIP;
                string EthName      = SNATdeleteRules[i].EthName;
                string EthIP        = SNATdeleteRules[i].EthIP;
                string NATIP        = SNATdeleteRules[i].NATIP;
                if (NATrules.ConfigSNAT(dev_ip, EthName, origin_devIP, EthIP, false))
                {
                    SNATapplyflag = true;
                }
                else
                {
                    SNATapplyflag = false;
                }
            }
            for (int i = 0; i < DNATdeleteRules.Count(); i++)
            {
                string origin_dstIP = DNATdeleteRules[i].origin_dstIP;
                string origin_dport = DNATdeleteRules[i].origin_dport;
                string map_IP       = DNATdeleteRules[i].map_IP;
                string map_port     = DNATdeleteRules[i].map_port;
                if (NATrules.ConfigDNAT(dev_ip, origin_dstIP, origin_dport, map_IP, map_port, false))
                {
                    DNATapplyflag = true;
                }
                else
                {
                    DNATapplyflag = false;
                }
            }
            Dispatcher.Invoke(new Action(() =>
            {
                if (SNATapplyflag && DNATapplyflag)
                {
                    UserMessageBox.Show("提示", "所有规则已成功应用!");
                }
                else
                {
                    if (SNATapplyflag)
                    {
                        UserMessageBox.Show("提示", "所有SNAT规则已成功应用!");
                    }
                    if (DNATapplyflag)
                    {
                        UserMessageBox.Show("提示", "所有DNAT规则已成功应用!");
                    }
                    else
                    {
                        UserMessageBox.Show("提示", "部分规则未成功应用,请检查设备之间的连接!");
                    }
                }
                this.Close();
            }
                                         ));
            NATApplyThread.Abort();
        }