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($"https://ipinfo.io/{ipstr}??token={token}");

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

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

                    return(resolved);
                }

                return(resolved);
            }
            catch (Exception)
            {
            }
            return(CountryCode.World);
        }
예제 #2
0
        internal async Task Run(HttpContext context, Func <Task> next)
        {
            //Pass the command to the next task in the pipeline
            await next.Invoke();

            //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        = context.UserIdentity(),
                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 gived ip address
                CountryCode = await _store.ResolveCountryCodeAsync(context.Connection.RemoteIpAddress)
            };

            //Store the request into the store
            await _store.StoreWebRequestAsync(req);
        }
        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);
        }
        internal async Task Run(HttpContext context, Func <Task> next)
        {
            if (_exclude?.Any(x => x(context)) ?? false)
            {
                await next.Invoke();

                return;
            }

            var req = new WebRequest
            {
                Timestamp       = DateTime.Now,
                Identity        = context.UserIdentity(),
                RemoteIpAddress = context.Connection.RemoteIpAddress,
                Method          = context.Request.Method,
                UserAgent       = context.Request.Headers["User-Agent"],
                Path            = context.Request.Path.Value,
                IsWebSocket     = context.WebSockets.IsWebSocketRequest,
                CountryCode     = await _store.ResolveCountryCodeAsync(context.Connection.RemoteIpAddress)
            };

            await _store.StoreWebRequestAsync(req);

            await next.Invoke();
        }
예제 #5
0
        public static async Task TestGeoResolve(IAnalyticStore 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")));
        }