ConnectEntryPoint.cs 4.4 KB

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