ConnectEntryPoint.cs 4.2 KB

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