/// <summary> /// 添加ListView行 /// </summary> /// <param name="str">显示文本</param> /// <param name="color">文本颜色</param> /// <param name="param">参数,没有填""</param> private void AddServerListViewItem(Color color, Object_Server server) { DeterMineCall(() => { ListViewItem lvi = new ListViewItem(); lvi.SubItems[0].Name = "ip"; lvi.SubItems.Add("validcount").Name = "validcount"; lvi.SubItems.Add("failedcount").Name = "failedcount"; lvi.SubItems.Add("brokencount").Name = "brokencount"; lvi.SubItems.Add("serverspeed").Name = "serverspeed"; lvi.SubItems.Add("localspeed").Name = "localspeed"; lvi.SubItems.Add("status").Name = "status"; lvi.Name = server.Ip; lvi.Tag = server; lvi.SubItems[0].Text = server.Ip; lvi.SubItems[1].Text = server.ValidCount.ToString(); lvi.SubItems[2].Text = server.FailedCount.ToString(); lvi.SubItems[3].Text = server.BrokenCount.ToString(); lvi.SubItems[4].Text = server.ServerSpeed.ToString(); lvi.SubItems[5].Text = "--"; lvi.SubItems[6].Text = "--"; lvi.ForeColor = color; lvi.Group = lvServerList.Groups["testing"]; lvServerList.Items.Insert(0, lvi); }); }
private void RefreshServerList(int flag, Object_Server item) { try { DeterMineCall(() => { switch (flag) { case 0: { lvServerList.Items[item.Ip].SubItems["localspeed"].Text = item.LocalSpeed.ToString(); lvServerList.Items[item.Ip].SubItems["status"].Text = "速度过慢"; lvServerList.Items[item.Ip].ForeColor = Color.FromArgb(192, 64, 0); lvServerList.Items[item.Ip].Group = lvServerList.Groups["low"]; break; } case 1: { lvServerList.Items[item.Ip].SubItems["localspeed"].Text = item.LocalSpeed.ToString(); lvServerList.Items[item.Ip].SubItems["status"].Text = "正常"; lvServerList.Items[item.Ip].ForeColor = Color.Green; lvServerList.Items[item.Ip].Group = lvServerList.Groups["normal"]; break; } case 2: { lvServerList.Items[item.Ip].ForeColor = Color.Red; lvServerList.Items[item.Ip].SubItems["status"].Text = "不可用"; lvServerList.Items[item.Ip].Group = lvServerList.Groups["notvalid"]; break; } case 3: { lvServerList.Items[item.Ip].ForeColor = Color.Red; lvServerList.Items[item.Ip].SubItems["status"].Text = "已被封禁"; lvServerList.Items[item.Ip].Group = lvServerList.Groups["breakout"]; break; } case 4: { lvServerList.Items[item.Ip].ForeColor = Color.Crimson; lvServerList.Items[item.Ip].SubItems["status"].Text = "测试中"; lvServerList.Items[item.Ip].Group = lvServerList.Groups["test"]; break; } } }); } catch { } }
/// <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(); } }