Beispiel #1
0
        /// <summary>
        /// 获取可用的缓存服务器列表
        /// </summary>
        private void SwitchMethod(object token)
        {
            CancellationToken ct = (CancellationToken)token;

            if (ct.IsCancellationRequested)
            {
                return;
            }
            GetIPInfoList getlist = new GetIPInfoList();

            dnsList.Clear();
            if (dnsList.Count <= 0)
            {
                DeterMineCall(() =>
                {
                    AddSwitchListViewItem("正在从服务器更新列表...", Color.Navy, "");
                });
                List <Object_Server> list = getlist.GetSourceItem(dnsInterface);
                if (list == null)
                {
                    AddSwitchListViewItem("警告: 下载服务器列表失败, 10秒钟后重试.", Color.Red, "");
                    Thread.Sleep(10000);
                    SwitchMethod(ctsSwitch.Token);
                }
                else
                {
                    Parallel.ForEach(list, x => AddServerListViewItem(Color.Blue, x));
                    AddSwitchListViewItem("下载服务器列表成功,已经获得 {0} 个服务器IP.", Color.Navy, list.Count);
                    AddSwitchListViewItem("开始测试服务器速度...", Color.Navy, "");
                }
                bool flag = false;
                foreach (Object_Server s in list)
                {
                    if (s.Ip == address)
                    {
                        flag = true; break;
                    }
                }
                if (!flag)
                {
                    var oServer = new Object_Server()
                    {
                        Ip          = address,
                        LocalSpeed  = 0,
                        FailedCount = 0,
                        BrokenCount = 0,
                        AddTime     = DateTime.Now,
                        ValidCount  = 0
                    };
                    list.Add(oServer);
                }
                CallbackServer(list, dic =>
                {
                    if (ct.IsCancellationRequested)
                    {
                        DeterMineCall(() =>
                        {
                            foreach (ListViewItem lvi in lvServerList.Items)
                            {
                                if (lvi.Group.Name == "test")
                                {
                                    lvi.Group = lvServerList.Groups["testing"];
                                }
                            }
                        });
                        return;
                    }
                    lock (locker)
                    {
                        DeterMineCall(() =>
                        {
                            var obj    = new Object_IP();
                            obj        = (Object_IP)dic[1];
                            var server = new Object_Server();
                            server     = (Object_Server)dic[0];

                            var lvi = new ListViewItem();
                            lvi.SubItems.Add("时间");
                            lvi.SubItems.Add("信息");
                            lvi.SubItems[0].Text = DateTime.Now.ToLongTimeString();
                            if (!obj.IsForbidden)
                            {
                                if (!obj.IsNotValid)
                                {
                                    server.LocalSpeed = Convert.ToInt16(obj.Speed.Value.TotalMilliseconds);
                                    if (obj.Speed.Value.TotalSeconds > 1)
                                    {
                                        dnsList.Remove(obj);
                                        lvi.ForeColor = Color.FromArgb(192, 64, 0);
                                        RefreshServerList(0, server);
                                        lvi.SubItems[1].Text = string.Format("服务器 [{0}] 响应速度过低, 放弃使用. (响应速度={1}秒)", obj.Ip, obj.Speed.Value.TotalSeconds.ToString("#0.00"));
                                    }
                                    else
                                    {
                                        dnsList.Add(obj);
                                        lvi.ForeColor = Color.Green;
                                        RefreshServerList(1, server);
                                        lvi.SubItems[1].Text = string.Format("服务器 [{0}] 正常. (响应速度={1}秒)", obj.Ip, obj.Speed.Value.TotalSeconds.ToString("#0.00"));
                                    }
                                }
                                else
                                {
                                    dnsList.Remove(obj);
                                    lvi.ForeColor     = Color.Red;
                                    server.LocalSpeed = -1;
                                    RefreshServerList(2, server);
                                    lvi.SubItems[1].Text = string.Format("服务器 [{0}] 当前不可用. ({1})", obj.Ip, obj.Message);
                                }
                            }
                            else
                            {
                                dnsList.Remove(obj);
                                lvi.ForeColor     = Color.Red;
                                server.LocalSpeed = -2;
                                RefreshServerList(3, server);
                                lvi.SubItems[1].Text = string.Format("服务器 [{0}] 已禁止了您的IP. ({1})", obj.Ip, obj.Message);
                            }
                            uploadList.Add(server);
                            lvSwitch.Items.Insert(0, lvi);
                        });
                    }
                });

                //});
                //rbtnFishlee.Checked? Properties.Resources.GetDnsInterface:Properties.Resources.GetDnsInterfaceATM
                //排序
                if (ct.IsCancellationRequested)
                {
                    return;
                }
                dnsListTmp = dnsList;
                getlist.SortList((lists) =>
                {
                    AddSwitchListViewItem("服务器测试完毕...共得到可用服务器{0}个", Color.Navy, new object[] { dnsListTmp.Count });
                    //上报到云端
                    try
                    {
                        AddSwitchListViewItem("开始上报到云端...", Color.Navy, "");
                        var wc = new WebClient();
                        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        wc.UploadString(uploadCloud, "POST", string.Format("kyfw.12306.cn={0}", HttpUtility.UrlEncode(JsonConvert.SerializeObject(this.uploadList))));
                        AddSwitchListViewItem("上报成功...", Color.Navy, "");
                        dnsListTmp = lists;
                    }
                    catch {
                        AddSwitchListViewItem("上报失败...", Color.Navy, "");
                    }
                }, dnsListTmp, GetIPInfoList.SortOrder.Ascending);
                switchSeed = 0;
                CDNSwitcher.CDN_List.Clear();
                CDNSwitcher.CDN_List.AddRange(dnsListTmp);
                //开启监控线程
                if (!listenFlag)
                {
                    try
                    {
                        hostAction.AddDnsResolve(dnsListTmp[0].Ip, domain);
                        switchSeed++;
                    }
                    catch { }
                    ListenLocalHost();
                }
                Thread.Sleep(60 * 60 * 1000);
                lvServerList.Clear();
                StartGetDnsList();
            }
        }
        /// <summary>
        /// 获取可用的缓存服务器列表
        /// </summary>
        private void SwitchMethod(object token)
        {
            CancellationToken ct = (CancellationToken)token;
            if (ct.IsCancellationRequested)
            {
                return;
            }
            GetIPInfoList getlist = new GetIPInfoList();
            dnsList.Clear();
            if (dnsList.Count <= 0)
            {
                DeterMineCall(() =>
                {
                    AddSwitchListViewItem("正在从服务器更新列表...", Color.Navy, "");
                });
                List<Object_Server> list = getlist.GetSourceItem(dnsInterface);
                if (list == null)
                {
                    AddSwitchListViewItem("警告: 下载服务器列表失败, 10秒钟后重试.", Color.Red, "");
                    Thread.Sleep(10000);
                    SwitchMethod(ctsSwitch.Token);
                }
                else
                {
                    Parallel.ForEach(list,x=> AddServerListViewItem(Color.Blue, x));
                    AddSwitchListViewItem("下载服务器列表成功,已经获得 {0} 个服务器IP.", Color.Navy, list.Count);
                    AddSwitchListViewItem("开始测试服务器速度...", Color.Navy, "");
                }
                bool flag=false;
                foreach (Object_Server s in list)
                {
                    if (s.Ip == address)
                    {
                        flag = true; break;
                    }
                }
                if (!flag)
                {
                    var oServer = new Object_Server()
                    {
                        Ip = address,
                        LocalSpeed = 0,
                        FailedCount = 0,
                        BrokenCount = 0,
                        AddTime = DateTime.Now,
                        ValidCount = 0
                    };
                    list.Add(oServer);
                }
                CallbackServer(list, dic =>
                {
                    if (ct.IsCancellationRequested)
                    {
                        DeterMineCall(() =>
                        {
                            foreach (ListViewItem lvi in lvServerList.Items)
                            {
                                if (lvi.Group.Name == "test")
                                    lvi.Group = lvServerList.Groups["testing"];
                            }
                        });
                        return;
                    }
                    lock (locker)
                    {
                        DeterMineCall(() =>
                        {
                            var obj = new Object_IP();
                            obj = (Object_IP)dic[1];
                            var server = new Object_Server();
                            server = (Object_Server)dic[0];

                            var lvi = new ListViewItem();
                            lvi.SubItems.Add("时间");
                            lvi.SubItems.Add("信息");
                            lvi.SubItems[0].Text = DateTime.Now.ToLongTimeString();
                            if (!obj.IsForbidden)
                            {
                                if (!obj.IsNotValid)
                                {
                                    server.LocalSpeed = Convert.ToInt16(obj.Speed.Value.TotalMilliseconds);
                                    if (obj.Speed.Value.TotalSeconds > 1)
                                    {
                                        dnsList.Remove(obj);
                                        lvi.ForeColor = Color.FromArgb(192, 64, 0);
                                        RefreshServerList(0, server);
                                        lvi.SubItems[1].Text = string.Format("服务器 [{0}] 响应速度过低, 放弃使用. (响应速度={1}秒)", obj.Ip, obj.Speed.Value.TotalSeconds.ToString("#0.00"));
                                    }
                                    else
                                    {
                                        dnsList.Add(obj);
                                        lvi.ForeColor = Color.Green;
                                        RefreshServerList(1, server);
                                        lvi.SubItems[1].Text = string.Format("服务器 [{0}] 正常. (响应速度={1}秒)", obj.Ip, obj.Speed.Value.TotalSeconds.ToString("#0.00"));
                                    }
                                }
                                else
                                {
                                    dnsList.Remove(obj);
                                    lvi.ForeColor = Color.Red;
                                    server.LocalSpeed = -1;
                                    RefreshServerList(2, server);
                                    lvi.SubItems[1].Text = string.Format("服务器 [{0}] 当前不可用. ({1})", obj.Ip, obj.Message);
                                }
                            }
                            else
                            {
                                dnsList.Remove(obj);
                                lvi.ForeColor = Color.Red;
                                server.LocalSpeed = -2;
                                RefreshServerList(3, server);
                                lvi.SubItems[1].Text = string.Format("服务器 [{0}] 已禁止了您的IP. ({1})", obj.Ip, obj.Message);
                            }
                            uploadList.Add(server);
                            lvSwitch.Items.Insert(0, lvi);
                        });
                    }
                });

                //});
                //rbtnFishlee.Checked? Properties.Resources.GetDnsInterface:Properties.Resources.GetDnsInterfaceATM
                //排序
                if (ct.IsCancellationRequested)
                {
                    return;
                }
                dnsListTmp = dnsList;
                getlist.SortList((lists) =>
                {
                    AddSwitchListViewItem("服务器测试完毕...共得到可用服务器{0}个", Color.Navy, new object[] { dnsListTmp.Count });
                    //上报到云端
                    try
                    {
                        AddSwitchListViewItem("开始上报到云端...", Color.Navy, "");
                        var wc = new WebClient();
                        wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                        wc.UploadString(uploadCloud, "POST", string.Format("kyfw.12306.cn={0}", HttpUtility.UrlEncode(JsonConvert.SerializeObject(this.uploadList))));
                        AddSwitchListViewItem("上报成功...", Color.Navy, "");
                        dnsListTmp = lists;
                    }
                    catch {
                        AddSwitchListViewItem("上报失败...", Color.Navy, "");
                    }
                }, dnsListTmp, GetIPInfoList.SortOrder.Ascending);
                switchSeed = 0;
                CDNSwitcher.CDN_List.Clear();
                CDNSwitcher.CDN_List.AddRange(dnsListTmp);
                //开启监控线程
                if (!listenFlag)
                {
                    try
                    {
                        hostAction.AddDnsResolve(dnsListTmp[0].Ip, domain);
                        switchSeed++;
                    }
                    catch { }
                    ListenLocalHost();
                }
                Thread.Sleep(60 * 60 * 1000);
                lvServerList.Clear();
                StartGetDnsList();
            }
        }