コード例 #1
0
        internal async Task Run(HttpContext context, Func <Task> next)
        {
            var identity = context.UserIdentity();

            //Pass the command to the next task in the pipeline
            await next.Invoke().ConfigureAwait(false);

            //This request should be filtered out ?
            if (_exclude?.Any(x => x(context)) ?? false)
            {
                return;
            }

            //Let's build our structure with collected data
            var req = new WebRequest
            {
                Timestamp       = DateTime.Now,
                Identity        = identity,
                RemoteIpAddress = context.Connection.RemoteIpAddress,
                Method          = context.Request.Method,
                UserAgent       = context.Request.Headers["User-Agent"],
                Path            = context.Request.Path.Value,
                IsWebSocket     = context.WebSockets.IsWebSocketRequest,

                //Ask the store to resolve the geo code of given ip address
                CountryCode = _geoIp != null
                               ? await _geoIp.ResolveCountryCodeAsync(context.Connection.RemoteIpAddress).ConfigureAwait(true)
                               : CountryCode.World
            };

            //Store the request into the store
            await _store.StoreWebRequestAsync(req).ConfigureAwait(false);
        }
コード例 #2
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var uri = new Uri($"https://ipinfo.io/{address}?token={_token}");

                    var obj = await LoadJson(uri).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #3
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _store.ResolveCountryCodeAsync(address);

                if (resolved == CountryCode.World)
                {
                    var ipstr    = address.ToString();
                    var response = await(new HttpClient()).GetStringAsync($"http://api.ipstack.com/{ipstr}?access_key={_accessKey}&format=1");

                    var obj = JsonConvert.DeserializeObject(response) as JObject;
                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _store.StoreGeoIpRangeAsync(address, address, resolved);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #4
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address);

                if (resolved == CountryCode.World)
                {
                    var ipstr    = address.ToString();
                    var response = await(new HttpClient()).GetStringAsync($"https://ipinfo.io/{ipstr}??token={_token}");

                    var obj = JsonConvert.DeserializeObject(response) as JObject;
                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
コード例 #5
0
        public static async Task TestGeoResolve(IGeoIpResolver store)
        {
            await store.StoreGeoIpRangeAsync(IPAddress.Parse("86.44.0.0"), IPAddress.Parse("86.49.47.255"),
                                             CountryCode.Cz);

            await store.StoreGeoIpRangeAsync(IPAddress.Parse("85.44.0.0"), IPAddress.Parse("86.43.255.255"),
                                             CountryCode.Sk);

            await store.StoreGeoIpRangeAsync(IPAddress.Parse("86.49.48.0"), IPAddress.Parse("86.86.255.255"),
                                             CountryCode.It);

            Assert.Equal(CountryCode.Cz, await store.ResolveCountryCodeAsync(IPAddress.Parse("86.49.47.89")));
        }
コード例 #6
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _resolver.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var obj = await LoadJson(new Uri($"http://ip-api.com/json/{address}")).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _resolver.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
                return(CountryCode.World);
            }
        }
コード例 #7
0
        public async Task <CountryCode> ResolveCountryCodeAsync(IPAddress address)
        {
            try
            {
                var resolved = await _store.ResolveCountryCodeAsync(address).ConfigureAwait(true);

                if (resolved == CountryCode.World)
                {
                    var obj = await LoadJson(new Uri($"http://api.ipstack.com/{address}?access_key={_accessKey}&format=1")).ConfigureAwait(true);

                    resolved = (CountryCode)Enum.Parse(typeof(CountryCode), obj["country_code"].ToString());

                    await _store.StoreGeoIpRangeAsync(address, address, resolved).ConfigureAwait(false);

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }