ConnectEntryPoint.cs 3.5 KB

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