WanAddressEntryPoint.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Plugins;
  3. using System;
  4. using System.IO;
  5. using System.Threading;
  6. namespace MediaBrowser.ServerApplication.EntryPoints
  7. {
  8. public class WanAddressEntryPoint : IServerEntryPoint
  9. {
  10. public static string WanAddress;
  11. private Timer _timer;
  12. private readonly IHttpClient _httpClient;
  13. public WanAddressEntryPoint(IHttpClient httpClient)
  14. {
  15. _httpClient = httpClient;
  16. }
  17. public void Run()
  18. {
  19. _timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));
  20. }
  21. private async void TimerCallback(object state)
  22. {
  23. try
  24. {
  25. using (var stream = await _httpClient.Get(new HttpRequestOptions
  26. {
  27. Url = "http://bot.whatismyipaddress.com/"
  28. }).ConfigureAwait(false))
  29. {
  30. using (var reader = new StreamReader(stream))
  31. {
  32. WanAddress = await reader.ReadToEndAsync().ConfigureAwait(false);
  33. }
  34. }
  35. }
  36. catch (Exception ex)
  37. {
  38. var b = true;
  39. }
  40. }
  41. public void Dispose()
  42. {
  43. if (_timer != null)
  44. {
  45. _timer.Dispose();
  46. _timer = null;
  47. }
  48. }
  49. }
  50. }