/// <summary>
        /// 使用ICMP 与服务器建立连接 创建者: xb 创建时间:2018/05/10
        /// </summary>
        /// <returns></returns>
        public async Task <LogModel> ConnectToServerWithICMP(SiteModel site, ICMPRequest request)
        {
            #region 初始化log
            LogModel log = new LogModel
            {
                Site_id     = site.Id,
                Create_Time = DateTime.Now
            };
            #endregion
            // IP 不合法
            if (null == request.MyIPAddress)
            {
                log.TimeCost    = 7500;
                log.Status_code = "1001";
                log.Is_error    = true;
                log.Log_Record  = "Address Format Is Invalid!";
            }
            else
            {
                #region 根据设计的ICMP请求修改的   --xb
                bool icmpFlag = request.MakeRequest();
                //请求完毕
                RequestObj requestObj;                        //用于存储icmp请求结果的对象
                requestObj = DataHelper.GetProperty(request); // 处理下请求对象的数据
                                                              // 生成请求记录
                CreateLogWithRequestServerResult(log, requestObj);
                // 更新站点信息
                UpdateSiteStatus(site, log);
                #endregion
            }

            await Task.CompletedTask;
            return(log);
        }
        /// <summary>
        /// 使用ICMP 与服务器建立连接
        /// </summary>
        /// <returns></returns>
        public async Task <LogModel> ConnectToServerWithICMP(SiteModel site, ICMPRequest request)
        {
            #region 初始化log
            LogModel log = new LogModel
            {
                Site_id     = site.Id,
                Create_Time = DateTime.Now
            };
            #endregion
            await Task.CompletedTask;
            #region 根据设计的ICMP请求修改的   --xb
            bool icmpFlag = request.MakeRequest();
            //请求完毕
            RequestObj requestObj;                        //用于存储icmp请求结果的对象
            requestObj = DataHelper.GetProperty(request); // 处理下请求对象的数据
            // 生成请求记录
            CreateLogWithRequestServerResult(log, requestObj);
            // 更新站点信息
            UpdateSiteStatus(site, log);
            if (icmpFlag == false)
            {
                site.Is_success = 0;
            }
            #endregion

            return(log);
        }
Exemple #3
0
 /// <summary>
 /// 发送测试ICMP请求
 /// </summary>
 private async void SendICMPRequest()
 {
     IPAddress address = null;
     try
     {
         Task<IPAddress> getAddress = util.GetIPAddressAsync(model.SiteAddress);//获取IP地址
         await getAddress;
         address= getAddress.Result;
     }
     catch//域名解析失败
     {
         await new MessageDialog("The domain name is not exist ").ShowAsync();
         return;
     }
     if (address == null)//如果IPAddress为null说明域名错误
     {
         await new MessageDialog("The domain name is wrong ").ShowAsync();
         return;
     }
   
     ICMPRequest request = new ICMPRequest(address);
     request.MakeRequest();
     List<RequestObj> objs = request.Requests;
     foreach (RequestObj tmp in objs)//ICMPRequest一共发送五条请求,其中有一条有问题就返回错误信息
     {
         if (tmp.Status.Equals("1000"))
         {
             continue;
         }
         else
         {
             await new MessageDialog("Error:" + tmp.Status).ShowAsync();
             return;
         }
     }
     if (objs.Count == 5)//如果请求的个数不为5,并且所有请求都正常说明发生了未知的错误
     {
         await new MessageDialog("Success!").ShowAsync();
     }
     else
     {
         await new MessageDialog("Has a unknown error").ShowAsync();
     }
 }
Exemple #4
0
        /// <summary>
        /// 用于对icmp请求后获取的对象属性进分析,对5条信息 整合成为一条信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static RequestObj GetProperty(ICMPRequest request)
        {
            List <RequestObj> datas = new List <RequestObj>();

            datas = request.Requests;
            RequestObj obj = new RequestObj
            {
                TimeCost = 0
            };
            int count = datas.Count();

            for (int i = 0; i < count; i++)
            {
                if (obj.Color != null)
                {
                    if (obj.Color.Equals("2"))
                    {
                        obj.Color = "2";
                    }
                    else if (obj.Color.Equals("0"))
                    {
                        obj.Color = "0";
                    }
                    else if (obj.Color.Equals("1"))
                    {
                        obj.Color = "1";
                    }
                    else
                    {
                        obj.Color = "-1";
                    }
                }
                else
                {
                    obj.Color = datas[i].Color;
                }
                obj.TimeCost = short.Parse((short.Parse(((obj.TimeCost * i) + datas[i].TimeCost).ToString()) / short.Parse((i + 1).ToString())).ToString());
            }
            return(obj);
        }
Exemple #5
0
        /// <summary>
        /// 发送测试ICMP请求
        /// </summary>
        private async void SendICMPRequest()
        {
            IPAddress address;

            try//检测IP是否合法
            {
                address = IPAddress.Parse(model.SiteAddress);
            }
            catch
            {
                await new MessageDialog("It's a wrong Address").ShowAsync();
                return;
            }
            ICMPRequest request = new ICMPRequest(address);

            request.MakeRequest();
            List <RequestObj> objs = request.Requests;

            foreach (RequestObj tmp in objs)//ICMPRequest一共发送五条请求,其中有一条有问题就返回错误信息
            {
                if (tmp.Status.Equals("1000"))
                {
                    continue;
                }
                else
                {
                    await new MessageDialog("error:" + tmp.Status).ShowAsync();
                    return;
                }
            }
            if (objs.Count == 5)//如果请求的个数不为5,并且所有请求都正常说明发生了未知的错误
            {
                await new MessageDialog("success!").ShowAsync();
            }
            else
            {
                await new MessageDialog("has a unknown error").ShowAsync();
            }
        }
Exemple #6
0
        public static void StartICMPFlood()
        {
            try
            {
                IPEo = new IPEndPoint(Dns.GetHostEntry(sFHost).AddressList[0], 0);
            }
            catch
            {
                IPEo = new IPEndPoint(IPAddress.Parse(sFHost), 0);
            }

            tFloodingThread = new Thread[iThreads];
            tFloodingJob    = new ThreadStart[iThreads];
            ICMPClass       = new ICMPRequest[iThreads];

            for (int i = 0; i < iThreads; i++)
            {
                ICMPClass[i]       = new ICMPRequest(IPEo, iICMPSockets, iPSize);
                tFloodingJob[i]    = new ThreadStart(ICMPClass[i].Send);
                tFloodingThread[i] = new Thread(tFloodingJob[i]);
                tFloodingThread[i].Start();
            }
        }
Exemple #7
0
        public static void StartICMPFlood()
        {
            try
            {
                IPEo = new IPEndPoint(Dns.GetHostEntry(sFHost).AddressList[0], 0);
            }
            catch
            {
                IPEo = new IPEndPoint(IPAddress.Parse(sFHost), 0);
            }

            tFloodingThread = new Thread[iThreads];
            tFloodingJob = new ThreadStart[iThreads];
            ICMPClass = new ICMPRequest[iThreads];

            for (int i = 0; i < iThreads; i++)
            {
                ICMPClass[i] = new ICMPRequest(IPEo, iICMPSockets, iPSize);
                tFloodingJob[i] = new ThreadStart(ICMPClass[i].Send);
                tFloodingThread[i] = new Thread(tFloodingJob[i]);
                tFloodingThread[i].Start();
            }
        }
Exemple #8
0
        private async void BackGroundRequestTask(IBackgroundTaskInstance taskInstance)
        {
            MessageRemind toast    = new MessageRemind(); //初始化消息提醒
            var           sitelist = DBHelper.GetAllSite();
            var           len      = sitelist.Count;      //使用foreach会出现不在期望中的异常
            SiteModel     _presite = new SiteModel();

            _presite = DBHelper.GetSiteById(4);            //这里是指定了precheck的id为4
            var _precolor = _presite.Is_success;           //如果percheck为错误 就不进行请求了
            // 引入封装的工具类  --xb
            SiteDetailUtilImpl util = new SiteDetailUtilImpl();

            if (_precolor != 0)
            {
                //遍历sitelist 根据协议进行请求
                for (int i = 0; i < len; i++)
                {
                    // 获取站点对象   --xb
                    SiteModel siteElement = sitelist[i];
                    // 创建用于记录此次请求的Log对象   --xb
                    LogModel log = null;
                    if (!siteElement.Is_Monitor)
                    {
                        continue;
                    }
                    IPAddress _siteAddress_redress = await util.GetIPAddressAsync(siteElement.Site_address);

                    switch (siteElement.Protocol_type)//根据协议请求站点
                    {
                    // HTTP/HTTPS协议请求   --xb
                    // 目前HTTP与HTTPS没有做协议请求上的区分  --xb
                    case "HTTPS":
                    case "HTTP":
                        try
                        {
                            // 发起HTTP请求,生成请求记录并更新站点信息  --xb
                            log = await util.RequestHTTPSite(siteElement, HTTPRequest.Instance);
                        }
                        catch (Exception ex)
                        {
                            DBHelper.InsertErrorLog(ex);
                            log = null;
                        }
                        break;

                    // DNS协议请求   --xb
                    case "DNS":
                        // 发起DNS请求,生成请求记录并更新站点信息  --xb
                        log = await util.AccessDNSServer(siteElement, DNSRequest.Instance);

                        break;

                    // ICMP协议请求   --xb
                    case "ICMP":
                        ICMPRequest icmp = new ICMPRequest(_siteAddress_redress);
                        // 发起ICMP请求,生成请求记录并更新站点信息  --xb
                        log = await util.ConnectToServerWithICMP(siteElement, icmp);

                        break;

                    // FTP协议请求   --xb
                    case "FTP":
                        // 发起FTP请求,生成请求记录并更新站点信息  --xb
                        log = await util.AccessFTPServer(siteElement, FTPRequest.Instance);

                        break;

                    // SMTP协议请求   --xb
                    case "SMTP":
                        // 发起SMTP请求,生成请求记录并更新站点信息  --xb
                        SMTPRequest _smtpRequest = new SMTPRequest(siteElement.Site_address, siteElement.Server_port);
                        log = await util.AccessSMTPServer(siteElement, _smtpRequest);

                        break;

                    // 补充之前欠缺的Socket服务器请求   --xb
                    case "SOCKET":
                        // 初始化Socket请求对象
                        SocketRequest _socketRequest = new SocketRequest();
                        // 请求指定终端,并生成对应的请求记录,最后更新站点信息
                        log = await util.ConnectToServerWithSocket(siteElement, _socketRequest);

                        break;

                    // 补充之前欠缺的SSH服务器请求   --xb
                    case "SSH":
                        log = await util.AccessSSHServer(siteElement, new SSHRequest(siteElement.Site_address, SshLoginType.Anonymous));

                        break;

                    default:
                        break;
                    }
                    if (null != log)
                    {
                        // 将请求的记录插入数据库  --xb
                        LogDaoImpl logDao = new LogDaoImpl();
                        logDao.InsertOneLog(log);
                        // 如果请求失败,提醒用户  --xb
                        if (log.Is_error)
                        {
                            // 消息提醒  --xb
                            toast.ShowToast(siteElement);
                        }
                    }
                    // 说明此次请求处于异常状态,记录进数据库中
                    else
                    {
                        DBHelper.InsertErrorLog(new Exception("Insert Log failed!Beacuse log to insert is null"));
                    }
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// 用于对icmp请求后获取的对象属性进分析,对5条信息 整合成为一条信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static RequestObj GetProperty(ICMPRequest request)
        {
            int timecost;//提供时间计算变量
            List <RequestObj> datas = new List <RequestObj>();

            datas = request.Requests;
            RequestObj obj = new RequestObj
            {
                TimeCost = 0
            };
            int count = datas.Count();

            for (int i = 0; i < count; i++)
            {
                if (obj.Color != null)
                {
                    if (obj.Color.Equals("2"))
                    {
                        obj.Color  = "2";
                        obj.Status = "1002";
                    }
                    else if (obj.Color.Equals("0"))
                    {
                        obj.Color  = "0";
                        obj.Status = "1001";
                    }
                    else if (obj.Color.Equals("1"))
                    {
                        obj.Color  = "1";
                        obj.Status = "1000";
                    }
                    else
                    {
                        obj.Color  = "-1";
                        obj.Status = "1002";
                    }
                }
                else
                {
                    obj.Color = datas[i].Color;
                    switch (datas[i].Color)
                    {
                    case "0":
                        obj.Status = "1001";
                        break;

                    case "1":
                        obj.Status = "1000";
                        break;

                    case "-1":
                        obj.Status = "1002";
                        break;

                    case "2":
                        obj.Status = "1002";
                        break;
                    }
                }
                timecost     = (obj.TimeCost * i + datas[i].TimeCost) / (i + 1);
                obj.TimeCost = short.Parse(timecost.ToString());//计算消耗平均时间  防止short溢出 故使用int进行计算
            }
            return(obj);
        }
        private async void BackGroundRequestTask(IBackgroundTaskInstance taskInstance)
        {
            MessageRemind toast    = new MessageRemind(); //初始化消息提醒
            var           sitelist = DBHelper.GetAllSite();
            var           len      = sitelist.Count;      //使用foreach会出现不在期望中的异常
            SiteModel     _presite = new SiteModel();

            _presite = DBHelper.GetSiteById(4);            //这里是指定了precheck的id为4
            var _precolor = _presite.Last_request_result;  //如果percheck为错误 就不进行请求了

            if (_precolor != 0)
            {
                //遍历sitelist 根据协议进行请求
                for (int i = 0; i < len; i++)
                {
                    SiteModel si          = sitelist[i];
                    string    _protocol   = si.Protocol_type;
                    string    _address    = si.Site_address;
                    string    url         = _address;
                    bool      _is_Monitor = si.Is_Monitor;
                    if (!_is_Monitor)
                    {
                        continue;
                    }
                    if (!IPAddress.TryParse(url, out IPAddress reIP))
                    {
                        //如果输入的不是ip地址
                        //通过域名解析ip地址
                        //网址简单处理 去除http和https
                        var http  = url.StartsWith("http://");
                        var https = url.StartsWith("https://");
                        if (http)
                        {
                            url = url.Substring(7);//网址截取从以第一w
                        }
                        else if (https)
                        {
                            url = url.Substring(8);//网址截取从以第一w
                        }
                        IPAddress[] hostEntry = await Dns.GetHostAddressesAsync(url);

                        for (int m = 0; m < hostEntry.Length; m++)
                        {
                            if (hostEntry[m].AddressFamily == AddressFamily.InterNetwork)
                            {
                                reIP = hostEntry[m];
                                break;
                            }
                        }
                    }//根据地址解析出ipv4地址
                    switch (_protocol)//根据协议请求站点
                    {
                    case "HTTPS":
                        HTTPRequest hTTPs = HTTPRequest.Instance;
                        hTTPs.ProtocolType = TransportProtocol.https;    //更改协议类型
                        hTTPs.Uri          = _address;
                        bool httpsFlag = await hTTPs.MakeRequest();

                        //请求完毕
                        //处理数据
                        si.Request_interval = hTTPs.TimeCost;
                        si.Request_count   += 1;
                        if ("1002".Equals(hTTPs.Status))    //定义的超时状态码
                        {
                            //请求超时
                            si.Last_request_result = -1;
                        }
                        else
                        {
                            SiteDetailViewModel util = new SiteDetailViewModel(); //用于查看状态码
                            bool match = util.SuccessCodeMatch(si, hTTPs.Status); //匹配用户设定状态码
                            if (match)                                            //匹配为成功  否则为失败
                            {
                                si.Last_request_result = 1;
                            }
                            else
                            {
                                si.Last_request_result = 0;
                                toast.ShowToast(si);
                            }
                        }
                        break;

                    case "HTTP":
                        HTTPRequest hTTP = HTTPRequest.Instance;    //默认协议类型http
                        hTTP.Uri          = _address;
                        hTTP.ProtocolType = TransportProtocol.http;
                        bool httpFlag = await hTTP.MakeRequest();

                        //请求完毕
                        //处理数据
                        si.Request_interval = hTTP.TimeCost;
                        si.Request_count   += 1;
                        if ("1002".Equals(hTTP.Status))
                        {
                            //请求超时
                            si.Last_request_result = -1;
                        }
                        else
                        {
                            SiteDetailUtilImpl util = new SiteDetailUtilImpl();
                            bool match = util.SuccessCodeMatch(si, hTTP.Status);    //匹配用户设定状态码
                            if (match)
                            {
                                si.Last_request_result = 1;
                            }
                            else
                            {
                                si.Last_request_result = 0;
                            }
                        }
                        if (httpFlag == false)
                        {
                            toast.ShowToast(si);
                            si.Last_request_result = 0;
                        }
                        break;

                    case "DNS":
                        string     baiduDomain = "www.baidu.com";//设定一个网站供dns服务器进行解析
                        DNSRequest dNS         = new DNSRequest(reIP, baiduDomain);
                        bool       dnsFlag     = await dNS.MakeRequest();

                        //请求完毕
                        if ("1000".Equals(dNS.Status))
                        {
                            //dns正常
                            si.Last_request_result = 1;
                        }
                        else if ("1001".Equals(dNS.Status))
                        {
                            //unknown
                            si.Last_request_result = 2;
                        }
                        else if ("1002".Equals(dNS.Status))
                        {
                            //timeout
                            si.Last_request_result = -1;
                        }
                        si.Request_interval = dNS.TimeCost;
                        si.Request_count   += 1;
                        if (dnsFlag == false)
                        {
                            //消息提醒
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "ICMP":
                        ICMPRequest icmp     = new ICMPRequest(reIP);
                        bool        icmpFlag = icmp.DoRequest();
                        //请求完毕
                        RequestObj requestObj;    //用于存储icmp请求结果的对象
                        requestObj             = DataHelper.GetProperty(icmp);
                        si.Last_request_result = int.Parse(requestObj.Color);
                        si.Request_count      += 1;
                        si.Request_interval    = requestObj.TimeCost;
                        if (icmpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "FTP":
                        var     json = si.ProtocolIdentification;
                        JObject js   = (JObject)JsonConvert.DeserializeObject(json);
                        //在此处加入type类型
                        string     username = js["username"].ToString();
                        string     password = js["password"].ToString();
                        FTPRequest fTP      = new FTPRequest(LoginType.Identify);
                        fTP.FtpServer = reIP;
                        fTP.Identification.Username = username;
                        fTP.Identification.Password = password;
                        bool ftpFlag = await fTP.MakeRequest();

                        //请求完毕
                        if ("1001".Equals(fTP.Status))
                        {
                            //置为错误
                            si.Last_request_result = 0;
                        }
                        else if ("1000".Equals(fTP.Status))
                        {
                            //置为成功
                            si.Last_request_result = 1;
                        }
                        else if ("1002".Equals(fTP.Status))
                        {
                            //超时异常
                            si.Last_request_result = -1;
                        }
                        si.Request_count   += 1;
                        si.Request_interval = fTP.TimeCost;
                        if (ftpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    case "SMTP":
                        SMTPRequest sMTP     = new SMTPRequest(_address, si.Server_port);
                        bool        smtpFlag = await sMTP.MakeRequest();

                        //请求完毕

                        if ("1000".Equals(sMTP.Status))
                        {
                            si.Last_request_result = 1;
                        }
                        else if ("1001".Equals(sMTP.Status))
                        {
                            si.Last_request_result = 0;
                        }
                        else if ("1002".Equals(sMTP.Status))
                        {
                            si.Last_request_result = -1;
                        }
                        si.Request_count   += 1;
                        si.Request_interval = sMTP.TimeCost;
                        if (smtpFlag == false)
                        {
                            si.Last_request_result = 0;
                            toast.ShowToast(si);
                        }
                        break;

                    default:
                        break;
                    }
                    DBHelper.UpdateSite(si);
                }
            }
        }
        /// <summary>
        /// 发起请求主体
        /// </summary>
        /// <returns>请求结果Log</returns>
        public async Task <LogModel> MakeRequest(SiteModel siteElement)
        {
            LogModel log = null;

            if (!siteElement.Is_server)
            {
                try
                {
                    log = await utilObject.RequestHTTPSite(siteElement, HTTPRequest.Instance);
                }
                catch (Exception ex)
                {
                    DBHelper.InsertErrorLog(ex);
                    log = null;
                }
            }
            else
            {
                try
                {
                    switch (siteElement.Protocol_type)
                    {
                    // DNS协议请求   --xb
                    case "DNS":
                        // 发起DNS请求,生成请求记录并更新站点信息  --xb
                        log = await utilObject.AccessDNSServer(siteElement, DNSRequest.Instance);

                        break;

                    // ICMP协议请求   --xb
                    case "ICMP":
                        IPAddress _siteAddress_redress = await utilObject.GetIPAddressAsync(siteElement.Site_address);

                        ICMPRequest icmp = new ICMPRequest(_siteAddress_redress);
                        // 发起ICMP请求,生成请求记录并更新站点信息  --xb
                        log = await utilObject.ConnectToServerWithICMP(siteElement, icmp);

                        break;

                    // FTP协议请求   --xb
                    case "FTP":
                        // 发起FTP请求,生成请求记录并更新站点信息  --xb
                        log = await utilObject.AccessFTPServer(siteElement, FTPRequest.Instance);

                        break;

                    // SMTP协议请求   --xb
                    case "SMTP":
                        // 发起SMTP请求,生成请求记录并更新站点信息  --xb
                        SMTPRequest _smtpRequest = new SMTPRequest(siteElement.Site_address, siteElement.Server_port);
                        log = await utilObject.AccessSMTPServer(siteElement, _smtpRequest);

                        break;

                    // 补充之前欠缺的Socket服务器请求   --xb
                    case "SOCKET":
                        // 初始化Socket请求对象  --xb
                        SocketRequest _socketRequest = new SocketRequest();
                        // 请求指定终端,并生成对应的请求记录,最后更新站点信息  --xb
                        log = await utilObject.ConnectToServerWithSocket(siteElement, _socketRequest);

                        break;

                    // 补充之前欠缺的SSH服务器请求   --xb
                    case "SSH":
                        log = await utilObject.AccessSSHServer(siteElement, new SSHRequest(siteElement.Site_address, SshLoginType.Anonymous));

                        break;

                    default:
                        log = null;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    DBHelper.InsertErrorLog(ex);
                    log = null;
                }
            }
            return(log);
        }