コード例 #1
0
        private void CallbackServer(List <Object_Server> servers, Action <object[]> callback)
        {
            var tmpList = CloneObjectServers(servers);

            Parallel.ForEach(tmpList, server =>
            {
                try
                {
                    if (IsOver || ctsSwitch.Token.IsCancellationRequested)
                    {
                        return;
                    }
                    RefreshServerList(4, server);
                    var obj = new Object_IP()
                    {
                        Ip = server.Ip
                    };
                    obj.HttpSetSpeed();
                    if (!obj.IsNotValid && !obj.IsForbidden)
                    {
                        obj.HttpSetSpeed();
                    }
                    var o = new object[2];
                    o[0]  = server;
                    o[1]  = obj;
                    callback(o);
                }
                catch
                {
                }
            });
        }
コード例 #2
0
 public void SwitchCDN(string ip,string domain,Action<string> callback )
 {
     var _ip = new Object_IP() { Ip = ip};
     ServerSwitch( _ip,domain, (objip) =>
     {
         callback(string.Format("自动切换完成 [{0}] ,响应速度={1}秒",  objip.Ip, objip.Speed.Value.TotalSeconds.ToString("#0.00") ));
     },true);
 }
コード例 #3
0
ファイル: CDNSwitcher.cs プロジェクト: vstion/12306_Helper
        public void SwitchCDN(string ip, string domain, Action <string> callback)
        {
            var _ip = new Object_IP()
            {
                Ip = ip
            };

            ServerSwitch(_ip, domain, (objip) =>
            {
                callback(string.Format("自动切换完成 [{0}] ,响应速度={1}秒", objip.Ip, objip.Speed.Value.TotalSeconds.ToString("#0.00")));
            }, true);
        }
コード例 #4
0
 /// <summary>
 /// 通过URL地址获取IP
 /// </summary>
 /// <param name="url"></param>
 /// <returns></returns>
 public static List<Object_IP> GetSourceList(string url)
 {
     List<Object_IP> list = new List<Object_IP>();
     WebClient wc = new WebClient();
     string strip = wc.DownloadString(url);
     strip = strip.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace(")", "").Replace(" ", "").Replace("\r\n", "");
     string[] tmpstr = strip.Split(new string[]{"|",","},StringSplitOptions.RemoveEmptyEntries);
     for (int i = 0; i < tmpstr.Length; i++)
     {
         var ip = new Object_IP() { Ip = tmpstr[i] };
         list.Add(ip);
     }
     return list;
 }
コード例 #5
0
        private void lvServerList_DoubleClick(object sender, EventArgs e)
        {
            if (lvServerList.SelectedItems.Count > 0 && listenFlag)
            {
                var ip = new Object_IP()
                {
                    Ip = lvServerList.SelectedItems[0].SubItems[0].Text
                };
                AddSwitchListViewItem("正在执行手动切换 服务器 [{0}] ", Color.FromArgb(192, 0, 192), ip.Ip);

                ServerSwitch(ctsSwitch.Token, ip, (objip) => {
                    AddSwitchListViewItem("切换完成 [{0}] ,响应速度={1}秒", Color.Green, new object[] { objip.Ip, objip.Speed.Value.TotalSeconds.ToString("#0.00") });
                }, true);
            }
        }
コード例 #6
0
        public void GetListItem(List<Object_Server> list,Action<Object_IP> callbackIP)
        {
            var listIP = new List<Object_IP>();
            list.ForEach(item =>
            {
                var o = new Object_IP() { Ip = item.Ip };
                listIP.Add(o);
            });

            Parallel.ForEach(listIP, (item) =>
            {
                item.HttpSetSpeed();
                callbackIP(item);
            });
        }
コード例 #7
0
        /// <summary>
        /// 切换服务器
        /// </summary>
        /// <param name="objp">切换前的服务器</param>
        /// <param name="callback">切换后的服务器</param>
        /// <param name="flag">是否为手动切换</param>
        private void ServerSwitch(CancellationToken token, Object_IP objp, Action <Object_IP> callback, bool flag = false)
        {
            DeterMineCall(() =>
            {
                if (flag)
                {
                    objp.HttpSetSpeed();
                    switchSeed++;

                    if (objp.Speed != null)
                    {
                        hostAction.AddDnsResolve(objp.Ip, domain);
                        callback(objp);
                    }
                }
                else
                {
                    if (dnsListTmp.Count <= 0)
                    {
                        AddSwitchListViewItem("没有可用的服务器,请重启关闭本窗口重新启动并测试。", Color.Red, "");
                        ctsListen.Cancel();
                        ctsSwitch.Cancel();
                        return;
                    }
                    for (int i = switchSeed; i < dnsListTmp.Count; i++)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                        objp.Ip = dnsListTmp[i].Ip;
                        objp.HttpSetSpeed();
                        switchSeed++;

                        if (objp.Speed != null)
                        {
                            hostAction.AddDnsResolve(objp.Ip, domain);
                            callback(objp);
                            break;
                        }
                    }
                    if (objp.Speed != null)
                    {
                        AddSwitchListViewItem("切换完成 [{0}] ,响应速度={1}秒", Color.Green, new object[] { objp.Ip, objp.Speed.Value.TotalSeconds.ToString("#0.00") });
                    }
                }
            });
        }
コード例 #8
0
ファイル: GetIPInfoList.cs プロジェクト: vstion/12306_Helper
        /// <summary>
        /// 按升序排列,空值默认为最大
        /// </summary>
        /// <param name="o1"></param>
        /// <param name="o2"></param>
        /// <returns></returns>
        private int CompareSpeedAsc(Object_IP o1, Object_IP o2)
        {
            if (o1.Speed == null)
            {
                if (o2.Speed == null)
                {
                    return(0);
                }
                return(1);
            }
            if (o2.Speed == null)
            {
                return(-1);
            }
            int retval = o1.Speed.Value.CompareTo(o2.Speed.Value);

            return(retval);
        }
コード例 #9
0
ファイル: GetIPList.cs プロジェクト: vstion/12306_Helper
        /// <summary>
        /// 通过URL地址获取IP
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static List <Object_IP> GetSourceList(string url)
        {
            List <Object_IP> list  = new List <Object_IP>();
            WebClient        wc    = new WebClient();
            string           strip = wc.DownloadString(url);

            strip = strip.Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace("(", "").Replace(")", "").Replace(" ", "").Replace("\r\n", "");
            string[] tmpstr = strip.Split(new string[] { "|", "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < tmpstr.Length; i++)
            {
                var ip = new Object_IP()
                {
                    Ip = tmpstr[i]
                };
                list.Add(ip);
            }
            return(list);
        }
コード例 #10
0
ファイル: GetIPInfoList.cs プロジェクト: vstion/12306_Helper
        public void GetListItem(List <Object_Server> list, Action <Object_IP> callbackIP)
        {
            var listIP = new List <Object_IP>();

            list.ForEach(item =>
            {
                var o = new Object_IP()
                {
                    Ip = item.Ip
                };
                listIP.Add(o);
            });

            Parallel.ForEach(listIP, (item) =>
            {
                item.HttpSetSpeed();
                callbackIP(item);
            });
        }
コード例 #11
0
ファイル: CDNSwitcher.cs プロジェクト: vstion/12306_Helper
        /// <summary>
        ///
        /// </summary>
        /// <param name="objp">手动切换的IP对象,自动切换请置空</param>
        /// <param name="callback"></param>
        /// <param name="flag">手否是手动切换</param>
        public void AutoSwitchCDN(Object_IP objp, Action <string> callback, bool flag = false)
        {
            if (_index == -1)
            {
                _index = 0;
            }
            if (flag)
            {
                objp.HttpSetSpeed();
                //if (objp.Speed != null)
                //{
                hostAction.AddDnsResolve(objp.Ip, _domain);
                callback(string.Format("自动切换完成 [{0}]", objp.Ip));
                //}
            }
            else
            {
                if (CDN_List.Count <= 0)
                {
                    callback("没有可以切换的服务器...");
                    return;
                }
                for (int i = _index; i < CDN_List.Count; i++)
                {
                    var obj = new Object_IP();
                    obj.Ip = CDN_List[i].Ip;
                    //obj.HttpSetSpeed();
                    _index++;

                    //if (obj.Speed != null)
                    //{
                    hostAction.AddDnsResolve(obj.Ip, _domain);
                    callback(string.Format("自动切换完成 [{0}] ", obj.Ip));
                    break;
                    //}
                }
                if (_index >= CDN_List.Count / 2)
                {
                    _index = 0;
                }
            }
        }
コード例 #12
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objp">手动切换的IP对象,自动切换请置空</param>
        /// <param name="callback"></param>
        /// <param name="flag">手否是手动切换</param>
        public void AutoSwitchCDN(Object_IP objp, Action<string> callback, bool flag = false)
        {
            if (_index == -1)
                _index = 0;
            if (flag)
            {
                objp.HttpSetSpeed();
                //if (objp.Speed != null)
                //{
                    hostAction.AddDnsResolve(objp.Ip, _domain);
                    callback(string.Format("自动切换完成 [{0}]", objp.Ip));
                //}
            }
            else
            {
                if (CDN_List.Count <= 0)
                {
                    callback("没有可以切换的服务器...");
                    return;
                }
                for (int i = _index; i < CDN_List.Count; i++)
                {
                    var obj = new Object_IP();
                    obj.Ip = CDN_List[i].Ip;
                    //obj.HttpSetSpeed();
                    _index++;

                    //if (obj.Speed != null)
                    //{
                        hostAction.AddDnsResolve(obj.Ip, _domain);
                        callback(string.Format("自动切换完成 [{0}] ", obj.Ip));
                        break;
                    //}
                }
                if (_index >= CDN_List.Count/2)
                    _index = 0;
            }
        }
コード例 #13
0
 private void CallbackServer(List<Object_Server> servers, Action<object[]> callback)
 {
     var tmpList = CloneObjectServers(servers);
     Parallel.ForEach(tmpList, server =>
     {
         try
         {
             if (IsOver || ctsSwitch.Token.IsCancellationRequested)
                 return;
             RefreshServerList(4, server);
             var obj = new Object_IP() {Ip = server.Ip};
             obj.HttpSetSpeed();
             if (!obj.IsNotValid && !obj.IsForbidden)
                 obj.HttpSetSpeed();
             var o = new object[2];
             o[0] = server;
             o[1] = obj;
             callback(o);
         }
         catch
         {
         }
     });
 }
コード例 #14
0
        /// <summary>
        /// 监听方法
        /// </summary>
        private void ListenMethod(object token)
        {
            var ct = (CancellationToken)token;
            if (ct.IsCancellationRequested)
            {
                return;
            }
            lock (locker)
            {
                var localhost = new Object_IP();
                string hostLine = hostAction.GetCacheServerIP(domain);
                if (ipAddress != null)
                {
                    localhost.Ip = ipAddress.ToString();
                    ipAddress = null;
                }
                else
                {
                    localhost.Ip = hostLine == "" ? domain : hostLine.Replace(" " + domain, "");
                }
                localhost.HttpSetSpeed();

                hostAction.AddDnsResolve(localhost.Ip, domain);

                if (localhost.Speed == null)
                {
                    timeOutCount++;
                    if (timeOutCount + lowCount < 2)
                    {
                        AddSwitchListViewItem("服务器 [{0}] 操作超时,已记录 [{1}] 次,连续累计超时或过慢2次则进行切换...", Color.FromArgb(192, 0, 192), localhost.Ip, timeOutCount + lowCount);
                    }
                    else
                    {
                        AddSwitchListViewItem("服务器 [{0}] 操作超时,正在切换...", Color.FromArgb(192, 0, 192), localhost.Ip);
                        ServerSwitch(ct, localhost, (obj) => localhost = obj);
                        timeOutCount = 0;
                    }
                }
                else
                {
                    if (localhost.Speed.Value.TotalSeconds > 1)//小于1秒,为正常速度
                    {
                        lowCount++;
                        if (timeOutCount + lowCount < 3)
                        {
                            AddSwitchListViewItem("服务器 [{0}] 速度过慢,响应速度={1}秒,已记录 [{2}] 次,连续累计超时或过慢3次则进行切换...", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"), timeOutCount + lowCount);
                        }
                        else
                        {
                            AddSwitchListViewItem("服务器 [{0}] 速度过慢,响应速度={1}秒,正在切换...", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"));
                            ServerSwitch(ct, localhost, (obj) => localhost = obj);
                            lowCount = 0;
                        }
                    }
                    else
                    {
                        AddSwitchListViewItem("服务器 [{0}] 正常,响应速度={1}秒", Color.Green, localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"));
                        timeOutCount = 0;
                        lowCount = 0;
                    }
                }
            }
            Thread.Sleep(10000);
            ListenMethod(token);
        }
コード例 #15
0
        /// <summary>
        /// 监听方法
        /// </summary>
        private void ListenMethod(object token)
        {
            var ct = (CancellationToken)token;

            if (ct.IsCancellationRequested)
            {
                return;
            }
            lock (locker)
            {
                var    localhost = new Object_IP();
                string hostLine  = hostAction.GetCacheServerIP(domain);
                if (ipAddress != null)
                {
                    localhost.Ip = ipAddress.ToString();
                    ipAddress    = null;
                }
                else
                {
                    localhost.Ip = hostLine == "" ? domain : hostLine.Replace(" " + domain, "");
                }
                localhost.HttpSetSpeed();

                hostAction.AddDnsResolve(localhost.Ip, domain);

                if (localhost.Speed == null)
                {
                    timeOutCount++;
                    if (timeOutCount + lowCount < 2)
                    {
                        AddSwitchListViewItem("服务器 [{0}] 操作超时,已记录 [{1}] 次,连续累计超时或过慢2次则进行切换...", Color.FromArgb(192, 0, 192), localhost.Ip, timeOutCount + lowCount);
                    }
                    else
                    {
                        AddSwitchListViewItem("服务器 [{0}] 操作超时,正在切换...", Color.FromArgb(192, 0, 192), localhost.Ip);
                        ServerSwitch(ct, localhost, (obj) => localhost = obj);
                        timeOutCount = 0;
                    }
                }
                else
                {
                    if (localhost.Speed.Value.TotalSeconds > 1)//小于1秒,为正常速度
                    {
                        lowCount++;
                        if (timeOutCount + lowCount < 3)
                        {
                            AddSwitchListViewItem("服务器 [{0}] 速度过慢,响应速度={1}秒,已记录 [{2}] 次,连续累计超时或过慢3次则进行切换...", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"), timeOutCount + lowCount);
                        }
                        else
                        {
                            AddSwitchListViewItem("服务器 [{0}] 速度过慢,响应速度={1}秒,正在切换...", Color.FromArgb(192, 0, 192), localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"));
                            ServerSwitch(ct, localhost, (obj) => localhost = obj);
                            lowCount = 0;
                        }
                    }
                    else
                    {
                        AddSwitchListViewItem("服务器 [{0}] 正常,响应速度={1}秒", Color.Green, localhost.Ip, localhost.Speed.Value.TotalSeconds.ToString("#0.00"));
                        timeOutCount = 0;
                        lowCount     = 0;
                    }
                }
            }
            Thread.Sleep(10000);
            ListenMethod(token);
        }
コード例 #16
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();
            }
        }
コード例 #17
0
        private void lvServerList_DoubleClick(object sender, EventArgs e)
        {
            if (lvServerList.SelectedItems.Count > 0 && listenFlag)
            {
                var ip = new Object_IP() { Ip = lvServerList.SelectedItems[0].SubItems[0].Text };
                AddSwitchListViewItem("正在执行手动切换 服务器 [{0}] ", Color.FromArgb(192, 0, 192), ip.Ip);

                ServerSwitch(ctsSwitch.Token, ip, (objip) => {
                    AddSwitchListViewItem("切换完成 [{0}] ,响应速度={1}秒", Color.Green, new object[] { objip.Ip, objip.Speed.Value.TotalSeconds.ToString("#0.00") });
                },true);
            }
        }
コード例 #18
0
ファイル: CDNSwitcher.cs プロジェクト: vstion/12306_Helper
 /// <summary>
 /// 切换服务器
 /// </summary>
 /// <param name="objp">切换前的服务器</param>
 /// <param name="callback">切换后的服务器</param>
 private void ServerSwitch(Object_IP objp, string domain, Action <Object_IP> callback, bool flag = false)
 {
     hostAction.AddDnsResolve(objp.Ip, domain);
 }
コード例 #19
0
        /// <summary>
        /// 切换服务器
        /// </summary>
        /// <param name="objp">切换前的服务器</param>
        /// <param name="callback">切换后的服务器</param>
        /// <param name="flag">是否为手动切换</param>
        private void ServerSwitch(CancellationToken token, Object_IP objp, Action<Object_IP> callback,bool flag=false)
        {
            DeterMineCall(() =>
            {
                if (flag)
                {
                    objp.HttpSetSpeed();
                    switchSeed++;

                    if (objp.Speed != null)
                    {
                        hostAction.AddDnsResolve(objp.Ip, domain);
                        callback(objp);
                    }
                }
                else
                {
                    if (dnsListTmp.Count <= 0)
                    {
                        AddSwitchListViewItem("没有可用的服务器,请重启关闭本窗口重新启动并测试。", Color.Red, "");
                        ctsListen.Cancel();
                        ctsSwitch.Cancel();
                        return;
                    }
                    for (int i = switchSeed; i < dnsListTmp.Count; i++)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }
                        objp.Ip = dnsListTmp[i].Ip;
                        objp.HttpSetSpeed();
                        switchSeed++;

                        if (objp.Speed != null)
                        {
                            hostAction.AddDnsResolve(objp.Ip, domain);
                            callback(objp);
                            break;
                        }
                    }
                    if (objp.Speed != null)
                    {
                        AddSwitchListViewItem("切换完成 [{0}] ,响应速度={1}秒", Color.Green, new object[] { objp.Ip, objp.Speed.Value.TotalSeconds.ToString("#0.00") });
                    }
                }
            });
        }
コード例 #20
0
 /// <summary>
 /// 按降序排序,空值默认为最小
 /// </summary>
 /// <param name="o1"></param>
 /// <param name="o2"></param>
 /// <returns></returns>
 private int CompareSpeedDesc(Object_IP o1, Object_IP o2)
 {
     if (o1.Speed == null)
     {
         if (o2.Speed == null)
         {
             return 0;
         }
         return 1;
     }
     if (o2.Speed == null)
     {
         return -1;
     }
     int retval = o2.Speed.Value.CompareTo(o1.Speed.Value);
     return retval;
 }
コード例 #21
0
 /// <summary>
 /// 切换服务器
 /// </summary>
 /// <param name="objp">切换前的服务器</param>
 /// <param name="callback">切换后的服务器</param>
 private void ServerSwitch( Object_IP objp,string domain, Action<Object_IP> callback, bool flag = false)
 {
     hostAction.AddDnsResolve(objp.Ip, domain);
 }
コード例 #22
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();
            }
        }