ConnectEntryPoint.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Connect;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Net;
  8. using System;
  9. using System.IO;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading;
  13. using CommonIO;
  14. using MediaBrowser.Common.IO;
  15. namespace MediaBrowser.Server.Implementations.Connect
  16. {
  17. public class ConnectEntryPoint : IServerEntryPoint
  18. {
  19. private Timer _timer;
  20. private readonly IHttpClient _httpClient;
  21. private readonly IApplicationPaths _appPaths;
  22. private readonly ILogger _logger;
  23. private readonly IConnectManager _connectManager;
  24. private readonly INetworkManager _networkManager;
  25. private readonly IApplicationHost _appHost;
  26. private readonly IFileSystem _fileSystem;
  27. public ConnectEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, INetworkManager networkManager, IConnectManager connectManager, IApplicationHost appHost, IFileSystem fileSystem)
  28. {
  29. _httpClient = httpClient;
  30. _appPaths = appPaths;
  31. _logger = logger;
  32. _networkManager = networkManager;
  33. _connectManager = connectManager;
  34. _appHost = appHost;
  35. _fileSystem = fileSystem;
  36. }
  37. public void Run()
  38. {
  39. LoadCachedAddress();
  40. _timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(5), TimeSpan.FromHours(3));
  41. }
  42. private readonly string[] _ipLookups = { "http://bot.whatismyipaddress.com", "https://connect.emby.media/service/ip" };
  43. private async void TimerCallback(object state)
  44. {
  45. var index = 0;
  46. foreach (var ipLookupUrl in _ipLookups)
  47. {
  48. try
  49. {
  50. // Sometimes whatismyipaddress might fail, but it won't do us any good having users raise alarms over it.
  51. var logErrors = index > 0;
  52. #if DEBUG
  53. logErrors = true;
  54. #endif
  55. using (var stream = await _httpClient.Get(new HttpRequestOptions
  56. {
  57. Url = ipLookupUrl,
  58. UserAgent = "Emby/" + _appHost.ApplicationVersion,
  59. LogErrors = logErrors
  60. }).ConfigureAwait(false))
  61. {
  62. using (var reader = new StreamReader(stream))
  63. {
  64. var address = await reader.ReadToEndAsync().ConfigureAwait(false);
  65. if (IsValid(address, ipLookupUrl))
  66. {
  67. ((ConnectManager)_connectManager).OnWanAddressResolved(address);
  68. CacheAddress(address);
  69. return;
  70. }
  71. }
  72. }
  73. }
  74. catch (HttpException)
  75. {
  76. }
  77. catch (Exception ex)
  78. {
  79. _logger.ErrorException("Error getting connection info", ex);
  80. }
  81. index++;
  82. }
  83. }
  84. private string CacheFilePath
  85. {
  86. get { return Path.Combine(_appPaths.DataPath, "wan.txt"); }
  87. }
  88. private void CacheAddress(string address)
  89. {
  90. var path = CacheFilePath;
  91. try
  92. {
  93. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  94. _fileSystem.WriteAllText(path, address, Encoding.UTF8);
  95. }
  96. catch (Exception ex)
  97. {
  98. _logger.ErrorException("Error saving data", ex);
  99. }
  100. }
  101. private void LoadCachedAddress()
  102. {
  103. var path = CacheFilePath;
  104. try
  105. {
  106. var endpoint = _fileSystem.ReadAllText(path, Encoding.UTF8);
  107. if (IsValid(endpoint, "cache"))
  108. {
  109. ((ConnectManager)_connectManager).OnWanAddressResolved(endpoint);
  110. }
  111. }
  112. catch (IOException)
  113. {
  114. // File isn't there. no biggie
  115. }
  116. catch (Exception ex)
  117. {
  118. _logger.ErrorException("Error loading data", ex);
  119. }
  120. }
  121. private bool IsValid(string address, string source)
  122. {
  123. IPAddress ipAddress;
  124. var valid = IPAddress.TryParse(address, out ipAddress);
  125. if (!valid)
  126. {
  127. _logger.Error("{0} is not a valid ip address. Source: {1}", address, source);
  128. }
  129. return valid;
  130. }
  131. public void Dispose()
  132. {
  133. if (_timer != null)
  134. {
  135. _timer.Dispose();
  136. _timer = null;
  137. }
  138. }
  139. }
  140. }