2
0

ConnectEntryPoint.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. foreach (var ipLookupUrl in _ipLookups)
  46. {
  47. try
  48. {
  49. using (var stream = await _httpClient.Get(new HttpRequestOptions
  50. {
  51. Url = ipLookupUrl,
  52. UserAgent = "Emby Server/" + _appHost.ApplicationVersion
  53. }).ConfigureAwait(false))
  54. {
  55. using (var reader = new StreamReader(stream))
  56. {
  57. var address = await reader.ReadToEndAsync().ConfigureAwait(false);
  58. if (IsValid(address))
  59. {
  60. ((ConnectManager)_connectManager).OnWanAddressResolved(address);
  61. CacheAddress(address);
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. catch (HttpException)
  68. {
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error getting connection info", ex);
  73. }
  74. }
  75. }
  76. private string CacheFilePath
  77. {
  78. get { return Path.Combine(_appPaths.DataPath, "wan.txt"); }
  79. }
  80. private void CacheAddress(string address)
  81. {
  82. var path = CacheFilePath;
  83. try
  84. {
  85. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  86. _fileSystem.WriteAllText(path, address, Encoding.UTF8);
  87. }
  88. catch (Exception ex)
  89. {
  90. _logger.ErrorException("Error saving data", ex);
  91. }
  92. }
  93. private void LoadCachedAddress()
  94. {
  95. var path = CacheFilePath;
  96. try
  97. {
  98. var endpoint = _fileSystem.ReadAllText(path, Encoding.UTF8);
  99. if (IsValid(endpoint))
  100. {
  101. ((ConnectManager)_connectManager).OnWanAddressResolved(endpoint);
  102. }
  103. }
  104. catch (IOException)
  105. {
  106. // File isn't there. no biggie
  107. }
  108. catch (Exception ex)
  109. {
  110. _logger.ErrorException("Error loading data", ex);
  111. }
  112. }
  113. private bool IsValid(string address)
  114. {
  115. IPAddress ipAddress;
  116. var valid = IPAddress.TryParse(address, out ipAddress);
  117. if (!valid)
  118. {
  119. _logger.Error("{0} is not a valid ip address", address);
  120. }
  121. return valid;
  122. }
  123. public void Dispose()
  124. {
  125. if (_timer != null)
  126. {
  127. _timer.Dispose();
  128. _timer = null;
  129. }
  130. }
  131. }
  132. }