public void UpdateList() { var newProxies = new List <Proxy>(); var ret = ""; using (var wc = new ImpatientWebClient()) ret = wc.DownloadString("http://cn-proxy.com/"); var tables = Regex.Matches(ret, "<table class=\"sortable\">.+?<tbody>(.*?)<\\/tbody>", RegexOptions.Singleline); var first = true; foreach (Match mat in tables) { if (first) { first = false; continue; } var trs = Regex.Matches(mat.Groups[1].Value, "<tr>(.*?)<\\/tr>", RegexOptions.Singleline); foreach (Match tr in trs) { var tds = Regex.Matches(tr.Groups[1].Value, "<td>(.*?)<\\/td>"); try { newProxies.Add(new Proxy() { host = tds[0].Groups[1].Value, port = int.Parse(tds[1].Groups[1].Value), latency = int.MaxValue }); } catch (Exception) { } } } lock (this) Proxies = newProxies; }
public void UpdateList() { var newProxies = new List <Proxy>(); var ret = ""; using (var wc = new ImpatientWebClient()) ret = wc.DownloadString("https://cn-proxy.com/"); var trs = Regex.Matches(ret, "<tr>(.*?)<\\/tr>", RegexOptions.Singleline); foreach (Match tr in trs) { var tds = Regex.Matches(tr.Groups[1].Value, "<td>(.*?)<\\/td>"); if (tds.Count > 2) { var host = tds[0].Groups[1].Value; if (Regex.IsMatch(host, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) { if (int.TryParse(tds[1].Groups[1].Value, out var port)) { newProxies.Add(new Proxy() { host = host, port = port, latency = int.MaxValue }); } } } } lock (this) Proxies = newProxies; }
public void UpdateLatency() { var newProxies = Proxies.Select(p => new Proxy { host = p.host, port = p.port }).ToList(); var actions = new List <Action>(); foreach (var proxy in newProxies) { actions.Add(() => { var latency = int.MaxValue; try { using (var wc = new ImpatientWebClient(ProxyTestTimeout)) { wc.Proxy = new WebProxy(proxy.host, proxy.port); var sw = new Stopwatch(); sw.Start(); var ret = wc.DownloadString("https://music.163.com/about"); sw.Stop(); if (ret.Contains("music.126")) { latency = (int)sw.ElapsedMilliseconds; } } lock (this) { proxy.latency = latency; proxy.valid = latency != int.MaxValue; } Console.WriteLine("{0}: OK", proxy); } catch (Exception e) { Console.WriteLine("{0}: {1}", proxy, e.Message); } }); } Parallel.Invoke(new ParallelOptions { MaxDegreeOfParallelism = parallelism }, actions.ToArray()); newProxies = newProxies.Where(p => p.valid).OrderBy(p => p.latency).ToList(); Console.WriteLine("Available proxies: "); newProxies.ForEach(p => Console.WriteLine(p)); lock (this) Proxies = newProxies.Where(p => p.valid).OrderBy(p => p.latency).ToList(); }
public void UpdateLatency() { var tasks = new List <Task>(); List <Proxy> newProxies; lock (this) { newProxies = Proxies.Select(item => new Proxy { host = item.host, port = item.port, latency = item.latency }).ToList(); } foreach (var proxy in newProxies) { tasks.Add(Task.Factory.StartNew(() => { var latency = int.MaxValue; try { using (var wc = new ImpatientWebClient()) { wc.Proxy = new WebProxy(proxy.host, proxy.port); var sw = new Stopwatch(); sw.Start(); var ret = wc.DownloadString("http://music.163.com/about"); sw.Stop(); if (ret.Contains("music.126")) { latency = (int)sw.ElapsedMilliseconds; } } proxy.latency = latency; } catch (Exception) { } })); } Task.WaitAll(tasks.ToArray(), 5000); newProxies = newProxies.OrderBy(o => o.latency).ToList(); lock (this) { Proxies = newProxies; } }