ConnectEntryPoint.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Connect;
  4. using MediaBrowser.Controller.Plugins;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Net;
  7. using System;
  8. using System.IO;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading;
  12. namespace MediaBrowser.Server.Implementations.Connect
  13. {
  14. public class ConnectEntryPoint : IServerEntryPoint
  15. {
  16. private Timer _timer;
  17. private readonly IHttpClient _httpClient;
  18. private readonly IApplicationPaths _appPaths;
  19. private readonly ILogger _logger;
  20. private readonly IConnectManager _connectManager;
  21. private readonly INetworkManager _networkManager;
  22. public ConnectEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, ILogger logger, INetworkManager networkManager, IConnectManager connectManager)
  23. {
  24. _httpClient = httpClient;
  25. _appPaths = appPaths;
  26. _logger = logger;
  27. _networkManager = networkManager;
  28. _connectManager = connectManager;
  29. }
  30. public void Run()
  31. {
  32. LoadCachedAddress();
  33. _timer = new Timer(TimerCallback, null, TimeSpan.FromSeconds(5), TimeSpan.FromHours(3));
  34. }
  35. private async void TimerCallback(object state)
  36. {
  37. try
  38. {
  39. using (var stream = await _httpClient.Get(new HttpRequestOptions
  40. {
  41. Url = "http://bot.whatismyipaddress.com/"
  42. }).ConfigureAwait(false))
  43. {
  44. using (var reader = new StreamReader(stream))
  45. {
  46. var address = await reader.ReadToEndAsync().ConfigureAwait(false);
  47. if (IsValid(address))
  48. {
  49. ((ConnectManager) _connectManager).OnWanAddressResolved(address);
  50. CacheAddress(address);
  51. }
  52. }
  53. }
  54. }
  55. catch (HttpException)
  56. {
  57. }
  58. catch (Exception ex)
  59. {
  60. _logger.ErrorException("Error getting connection info", ex);
  61. }
  62. }
  63. private string CacheFilePath
  64. {
  65. get { return Path.Combine(_appPaths.DataPath, "wan.txt"); }
  66. }
  67. private void CacheAddress(string address)
  68. {
  69. var path = CacheFilePath;
  70. try
  71. {
  72. Directory.CreateDirectory(Path.GetDirectoryName(path));
  73. File.WriteAllText(path, address, Encoding.UTF8);
  74. }
  75. catch (Exception ex)
  76. {
  77. _logger.ErrorException("Error saving data", ex);
  78. }
  79. }
  80. private void LoadCachedAddress()
  81. {
  82. var path = CacheFilePath;
  83. try
  84. {
  85. var endpoint = File.ReadAllText(path, Encoding.UTF8);
  86. if (IsValid(endpoint))
  87. {
  88. ((ConnectManager)_connectManager).OnWanAddressResolved(endpoint);
  89. }
  90. }
  91. catch (IOException)
  92. {
  93. // File isn't there. no biggie
  94. }
  95. catch (Exception ex)
  96. {
  97. _logger.ErrorException("Error loading data", ex);
  98. }
  99. }
  100. private bool IsValid(string address)
  101. {
  102. IPAddress ipAddress;
  103. return IPAddress.TryParse(address, out ipAddress);
  104. }
  105. public void Dispose()
  106. {
  107. if (_timer != null)
  108. {
  109. _timer.Dispose();
  110. _timer = null;
  111. }
  112. }
  113. }
  114. }