public Task gwsAsync() { cts = new CancellationTokenSource(); task = Task.Factory.StartNew(() => { while (!cts.IsCancellationRequested) { try { using (IPv4DataContext db = new IPv4DataContext()) { var q = db.gws_Old.Select(f => f.Address).Except(db.gws.Select(f => f.Address)).ToList(); for (int i = 0; i < q.Count - 1; i++) { if (cts.IsCancellationRequested) { break; } var gws = q[i]; //CodeSite.Send("gws.IP", gws.IP); if (ProgressString != null) { ProgressString.Report(string.Format("{0}/{1}", i + 1, q.Count)); } var ip = db.IPv4SSL.SingleOrDefault(f => f.Address == gws); if (ip == null) { ip = new IPv4SSL((UInt32)gws); } else if (ip.Isgws) { //CodeSite.SendNote("跳过"); continue; } WebCheck(ip); if (ip.RoundtripTime != -1 && db.IPv4SSL.SingleOrDefault(f => f.Address == gws) == null) { db.IPv4SSL.InsertOnSubmit(ip); } if (ip.Isgws) { if (ProgressIP != null) { ProgressIP.Report(ip); } db.SubmitChanges(); } } } } catch (Exception ex) { ex.SendCodeSite("gwsAsync"); Thread.Sleep(1000); } } }, cts.Token); return(task); }
private void CheckList() { progressCount = 0; int newCount = 0, lastCount = progressCount, tempCount; Timer timer; if (ProgressString != null) { timer = new Timer(_ => { ProgressString.Report(string.Format(progressFormat, progressCount, newCount)); tempCount = progressCount; //CodeSite.Send("progressCount", tempCount - lastCount); lastCount = tempCount; }, null, 1000, 1000); } else { timer = null; } try { ParallelOptions po = new ParallelOptions() { CancellationToken = cts.Token, MaxDegreeOfParallelism = MaxDegreeOfParallelism }; //CodeSite.Send("po.MaxDegreeOfParallelism", po.MaxDegreeOfParallelism); Parallel.ForEach(listIP, po, (uip) => { Interlocked.Increment(ref progressCount); IPv4SSL ip = new IPv4SSL(uip); WebCheck(ip); if (ip.IsSSL) { Interlocked.Increment(ref newCount); bcIPv4SSL.Add(ip); } }); } catch (OperationCanceledException ex) { ex.SendCodeSite("OperationCanceledException"); } forceSave = true; while (forceSave) { //CodeSite.SendReminder("等待数据保存完毕"); Thread.Sleep(1000); } CodeSite.Send("新增数据", newCount); if (timer != null) { timer.Dispose(); } }
public static void TcpCheck(IPv4SSL ip) { IPAddress value = IPAddress.Parse(ip.IP); TcpClient tcpClient = new TcpClient { ReceiveTimeout = 1000, SendTimeout = 1000 }; try { using (Ping ping = new Ping()) { PingReply pingReply = ping.Send(value, 1000); if (pingReply.Status == IPStatus.Success) { tcpClient.Connect(value, 443); SslStream sslStream = new SslStream(tcpClient.GetStream(), false, (sender, certificate, chain, sslPolicyErrors) => Encoding.UTF8.GetString(certificate.GetRawCertData()).IndexOf("google") != -1); sslStream.AuthenticateAsClient(""); StreamReader streamReader = new StreamReader(sslStream); StreamWriter streamWriter = new StreamWriter(sslStream); streamWriter.Write("HEAD / HTTP/1.1\r\nHost:www.google.com\r\nConnection:Close\r\n\r\n"); streamWriter.Flush(); string text = streamReader.ReadToEnd(); CodeSite.Send("text", text); tcpClient.Close(); object[] array = new object[8]; if (text.IndexOf("Server: gvs 1.0") != -1) { array[2] = "GVS"; } else if (text.IndexOf("Server: gws") != -1) { array[2] = "gws"; } string text2 = sslStream.RemoteCertificate.Subject.Split(new char[] { ',' })[0].Substring(3); array[0] = value.ToString(); array[1] = "_OK " + pingReply.RoundtripTime.ToString().PadLeft(4, '0'); array[3] = text2; array[4] = "001"; array[5] = ""; } } } catch (Exception ex) { ex.SendCodeSite("TcpCheck"); } tcpClient.Close(); }
bool WebCheck(uint value) { IPv4SSL ip = new IPv4SSL(value); WebCheck(ip); if (ip.RoundtripTime != -1) { if (Properties.Settings.Default.IPv4Assigned) { CodeSite.Send("IP", ip.IP); } IPStack.Push(ip); } return(ip.RoundtripTime != -1); }
void SaveThreadStart() { CodeSite.EnterMethod(this, "SaveThreadStart"); try { ctsSave = new CancellationTokenSource(); taskSave = Task.Factory.StartNew(() => { IPv4SSL[] ipa = new IPv4SSL[100]; int c; while (!ctsSave.IsCancellationRequested || !IPStack.IsEmpty) { if ((c = IPStack.TryPopRange(ipa)) > 0) { var ip = ipa.Take(c); if (ProgressIP != null) { foreach (var item in ip) { if (item.Isgws) { ProgressIP.Report(item); } } } while (!SaveDB(ip) && !ctsSave.IsCancellationRequested) { Thread.Sleep(1000); } } if (IPStack.IsEmpty) { Thread.Sleep(1000); } } }, ctsSave.Token); } finally { CodeSite.ExitMethod(this, "SaveThreadStart"); } }
public static void WebCheck(IPv4SSL ip) { IPAddress value = IPAddress.Parse(ip.IP); try { using (Ping ping = new Ping()) { PingReply pr = ping.Send(value, 1000); if (pr.Status == IPStatus.Success) { ip.RoundtripTime = (int)pr.RoundtripTime; //ip = new IPv4SSL(value.ToUInt32(), pr.RoundtripTime); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format("https://{0}", value)); request.Timeout = 5000; request.AllowAutoRedirect = false; request.AllowWriteStreamBuffering = false; request.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { chain.Dispose(); if (certificate == null) { return(false); } ip.Issuer = ((X509Certificate2)certificate).GetNameInfo(X509NameType.SimpleName, true); ip.Subject = ((X509Certificate2)certificate).GetNameInfo(X509NameType.SimpleName, false); if (ip.IsGoogle) { CodeSite.Send("IP", value.ToString()); } certificate.Dispose(); return(ip.IsGoogle); }; request.Method = "HEAD"; request.KeepAlive = false; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { ip.Server = response.Server; CodeSite.Send("response", response); } } finally { request.Abort(); } } } } catch (WebException ex) { if (ip.IsGoogle) { HttpWebResponse response = ex.Response as HttpWebResponse; if (response != null) { ip.Server = response.Server; CodeSite.Send("response", response); } else { ex.SendCodeSite("IsGoogle"); } } } catch (Exception ex) { if (ip.IsGoogle) { ex.SendCodeSite("WebCheck"); } } }