ConnectEntryPoint.cs 4.5 KB

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