private string NetworkChecker() { try { var port = string.IsNullOrEmpty(Port) ? "21" : Port; IpAddress = IpAddress.Contains(":") ? IpAddress : IpAddress + ":" + port; FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"ftp://{IpAddress}/projectFolder/"); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("server", "abc"); var response = (FtpWebResponse)request.GetResponse(); response.Close(); return(port); } catch (Exception e) { Console.WriteLine(e + "Ip is invalid"); throw; } }
private void GetExternalIpAddress(object o) { // 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(); 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("<br/>", ""); webPage = webPage.Replace("<br />", ""); #region Detect IPv6 if (webPage.Contains(":")) { string[] ipV6Parts = webPage.Split(':'); // Must have between 4 and 8 parts if (ipV6Parts.Length >= 4 && ipV6Parts.Length <= 8) { IpAddress = "IpAddress " + webPage; IpObtainedFrom = $"IpAddress V6 obtained from {url} on attempt {_retryCount + 1}"; break; } } #endregion Detect IPv6 #region Detect IPv4 if (webPage.Contains(".")) { // Must have 4 parts string[] ipV4Parts = webPage.Split('.'); if (ipV4Parts.Length == 4) { IpAddress = "IpAddress " + webPage; IpObtainedFrom = $"IpAddress V4 obtained from {url} on attempt {_retryCount + 1}"; break; } } #endregion Detect IPv4 } 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 t = new Thread(pts); t.Start(null); } } }
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 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 {SafeDouble.AsString0(_ipStopwatch.ElapsedMilliseconds)}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); } }