private string GetData(string url) { string result = "0.0.0.0"; try { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request != null) { request.UserAgent = "Chem4Word Add-In"; request.Timeout = url.Contains("chem4word") ? 2000 : 1000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); try { // Get Server Date header i.e. "Tue, 01 Jan 2019 19:52:46 GMT" ServerDateHeader = response.Headers["date"]; } catch { // Do Nothing ServerDateHeader = null; } if (HttpStatusCode.OK.Equals(response.StatusCode)) { var stream = response.GetResponseStream(); if (stream != null) { using (var reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } } } } catch (WebException webException) { StartUpTimings.Add(webException.Status == WebExceptionStatus.Timeout ? $"Timeout: '{url}'" : webException.Message); } catch (Exception exception) { StartUpTimings.Add(exception.Message); } return(result); }
private void GetExternalIpAddress(object o) { string module = $"{MethodBase.GetCurrentMethod().Name}()"; string message = $"{module} started at {SafeDate.ToLongDate(DateTime.Now)}"; StartUpTimings.Add(message); Debug.WriteLine(message); Stopwatch sw = new Stopwatch(); sw.Start(); // http://www.ipv6proxy.net/ --> "Your IP address : 2600:3c00::f03c:91ff:fe93:dcd4" try { foreach (var domain in Domains) { try { string url = $"{domain}/{DetectionFile}"; Debug.WriteLine("Fetching external IpAddress from " + url + " attempt " + _retryCount); IpAddress = "IpAddress 0.0.0.0"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request != null) { request.UserAgent = "Chem4Word Add-In"; request.Timeout = 2000; // 2 seconds HttpWebResponse response = (HttpWebResponse)request.GetResponse(); try { // Get Server Date header i.e. "Tue, 01 Jan 2019 19:52:46 GMT" ServerDateHeader = response.Headers["date"]; } catch { // Do Nothing } if (HttpStatusCode.OK.Equals(response.StatusCode)) { using (var reader = new StreamReader(response.GetResponseStream())) { string webPage = reader.ReadToEnd(); if (webPage.StartsWith("Your IP address : ")) { // Tidy Up the data webPage = webPage.Replace("Your IP address : ", ""); webPage = webPage.Replace("UTC Date : ", ""); webPage = webPage.Replace("<br/>", "|"); webPage = webPage.Replace("<br />", "|"); string[] lines = webPage.Split('|'); #region Detect IPv6 if (lines[0].Contains(":")) { string[] ipV6Parts = lines[0].Split(':'); // Must have between 4 and 8 parts if (ipV6Parts.Length >= 4 && ipV6Parts.Length <= 8) { IpAddress = "IpAddress " + lines[0]; IpObtainedFrom = $"IpAddress V6 obtained from {url} on attempt {_retryCount + 1}"; } } #endregion Detect IPv6 #region Detect IPv4 if (lines[0].Contains(".")) { // Must have 4 parts string[] ipV4Parts = lines[0].Split('.'); if (ipV4Parts.Length == 4) { IpAddress = "IpAddress " + lines[0]; IpObtainedFrom = $"IpAddress V4 obtained from {url} on attempt {_retryCount + 1}"; } } #endregion Detect IPv4 #region Detect Php UTC Date if (lines.Length > 1) { ServerUtcDateRaw = lines[1]; ServerUtcDateTime = FromPhpDate(lines[1]); SystemUtcDateTime = DateTime.UtcNow; UtcOffset = SystemUtcDateTime.Ticks - ServerUtcDateTime.Ticks; } #endregion Detect Php UTC Date if (!IpAddress.Contains("0.0.0.0")) { break; } } Debug.WriteLine(IpAddress); } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); // Do Nothing } Thread.Sleep(500); } } catch (Exception ex) { Debug.WriteLine(ex.Message); // Something went wrong IpAddress = "IpAddress 0.0.0.0 - " + ex.Message; } if (string.IsNullOrEmpty(IpAddress) || IpAddress.Contains("0.0.0.0")) { if (_retryCount < 5) { _retryCount++; Thread.Sleep(500); ParameterizedThreadStart pts = GetExternalIpAddress; Thread thread = new Thread(pts); thread.SetApartmentState(ApartmentState.STA); thread.Start(null); } } sw.Stop(); message = $"{module} took {sw.ElapsedMilliseconds.ToString("#,000")}ms"; StartUpTimings.Add(message); Debug.WriteLine(message); }
private string GetData(string url) { string result = "0.0.0.0"; var securityProtocol = ServicePointManager.SecurityProtocol; try { if (url.StartsWith("https")) { ServicePointManager.SecurityProtocol = securityProtocol | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request != null) { request.UserAgent = "Chem4Word Add-In"; request.Timeout = url.Contains("chem4word") ? 5000 : 2500; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); try { // Get Server Date header i.e. "Tue, 01 Jan 2019 19:52:46 GMT" ServerDateHeader = response.Headers["date"]; SystemUtcDateTime = DateTime.UtcNow; ServerUtcDateTime = DateTime.Parse(ServerDateHeader).ToUniversalTime(); UtcOffset = SystemUtcDateTime.Ticks - ServerUtcDateTime.Ticks; } catch { // Indicate failure ServerDateHeader = null; SystemUtcDateTime = DateTime.MinValue; } if (HttpStatusCode.OK.Equals(response.StatusCode)) { var stream = response.GetResponseStream(); if (stream != null) { using (var reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } } } } catch (WebException webException) { StartUpTimings.Add(webException.Status == WebExceptionStatus.Timeout ? $"Timeout: '{url}'" : webException.Message); } catch (Exception exception) { StartUpTimings.Add(exception.Message); } finally { ServicePointManager.SecurityProtocol = securityProtocol; } return(result); }
private void GetExternalIpAddress(object o) { string module = $"{MethodBase.GetCurrentMethod().Name}()"; try { string message; IpAddress = "0.0.0.0"; for (int i = 0; i < 2; i++) { foreach (string place in _placesToTry) { _attempts++; try { message = $"Attempt #{_attempts} using '{place}'"; StartUpTimings.Add(message); Debug.WriteLine(message); if (place.Contains("chem4word")) { GetInternalVersion(place); } else { GetExternalVersion(place); } // Exit out of inner loop if (!IpAddress.Contains("0.0.0.0")) { break; } } catch (Exception exception) { Debug.WriteLine(exception.Message); StartUpTimings.Add(exception.Message); } } // Exit out of outer loop if (!IpAddress.Contains("0.0.0.0")) { break; } } if (IpAddress.Contains("0.0.0.0")) { // Handle failure IpAddress = "8.8.8.8"; } _ipStopwatch.Stop(); message = $"{module} took {_ipStopwatch.ElapsedMilliseconds.ToString("#,000", CultureInfo.InvariantCulture)}ms"; StartUpTimings.Add(message); Debug.WriteLine(message); } catch (ThreadAbortException threadAbortException) { // Do Nothing Debug.WriteLine(threadAbortException.Message); } catch (Exception exception) { Debug.WriteLine(exception.Message); StartUpTimings.Add(exception.Message); } }