/// <summary> /// Converts the IP address to a string as required by the PASV command /// </summary> /// <returns>The IP address as string</returns> public override string ToString() { var result = new StringBuilder(); if (_isEnhanced) { result.Append("|"); if (AddressFamily != null) { switch (AddressFamily) { case FubarDev.FtpServer.AddressFamily.IPv4: result.Append("1"); break; case FubarDev.FtpServer.AddressFamily.IPv6: result.Append("2"); break; default: return(string.Empty); } } result.Append($"|{IpAddress}|{IpPort}|"); } else { result .Append(IpAddress?.Replace('.', ',')) .Append(',') .Append(IpPort / 256) .Append(',') .Append(IpPort & 0xFF); } return(result.ToString()); }
private void GetExternalIpAddress(object o) { string module = $"{MethodBase.GetCurrentMethod().Name}()"; // http://www.ipv6proxy.net/ --> "Your IP address : 2600:3c00::f03c:91ff:fe93:dcd4" try { int idx = 0; foreach (var domain in Domains) { try { string url = $"{domain}/{DetectionFile}"; string message = $"Fetching external IpAddress from {url} attempt {_retryCount}.{idx++}"; Debug.WriteLine(message); StartUpTimings.Add(message); 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 // Failure, try next one if (!IpAddress.Contains("0.0.0.0")) { break; } } } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); StartUpTimings.Add($"GetExternalIpAddress {ex.Message}"); } Thread.Sleep(500); } } catch (Exception ex) { Debug.WriteLine(ex.Message); // Something went wrong IpAddress = "IpAddress 0.0.0.0 - " + ex.Message; StartUpTimings.Add($"GetExternalIpAddress {ex.Message}"); } try { if (string.IsNullOrEmpty(IpAddress) || IpAddress.Contains("0.0.0.0")) { // Try 0..4 times from 0..2 domains if (_retryCount < 4) { // Retry IncrementRetryCount(); Thread.Sleep(500); ParameterizedThreadStart pts = GetExternalIpAddress; Thread thread = new Thread(pts); thread.SetApartmentState(ApartmentState.STA); thread.Start(null); } else { // Failure IpAddress = IpAddress.Replace("0.0.0.0", "8.8.8.8"); _stopwatch.Stop(); var message = $"{module} took {_stopwatch.ElapsedMilliseconds.ToString("#,000", CultureInfo.InvariantCulture)}ms"; StartUpTimings.Add(message); Debug.WriteLine(message); } } else { // Success _stopwatch.Stop(); var message = $"{module} took {_stopwatch.ElapsedMilliseconds.ToString("#,000", CultureInfo.InvariantCulture)}ms"; StartUpTimings.Add(message); Debug.WriteLine(message); } } catch (ThreadAbortException threadAbortException) { // Do Nothing Debug.WriteLine(threadAbortException.Message); } }