aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/EntryPoints/WanAddressEntryPoint.cs
blob: 7b2a1314e01e518142d0deba050848d8aef61de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Plugins;
using System;
using System.IO;
using System.Threading;

namespace MediaBrowser.ServerApplication.EntryPoints
{
    public class WanAddressEntryPoint : IServerEntryPoint
    {
        public static string WanAddress;
        private Timer _timer;
        private readonly IHttpClient _httpClient;

        public WanAddressEntryPoint(IHttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public void Run()
        {
            _timer = new Timer(TimerCallback, null, TimeSpan.FromMinutes(1), TimeSpan.FromHours(24));
        }

        private async void TimerCallback(object state)
        {
            try
            {
                using (var stream = await _httpClient.Get(new HttpRequestOptions
                {
                    Url = "http://bot.whatismyipaddress.com/"

                }).ConfigureAwait(false))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        WanAddress = await reader.ReadToEndAsync().ConfigureAwait(false);
                    }
                }
            }
            catch
            {
            }
        }

        public void Dispose()
        {
            if (_timer != null)
            {
                _timer.Dispose();
                _timer = null;
            }
        }
    }
}