コード例 #1
0
        public async Task <GeoResult> ResolveIpAsync(string ip, CancellationToken cancellationToken = new CancellationToken())
        {
            if (String.IsNullOrWhiteSpace(ip) || (!ip.Contains(".") && !ip.Contains(":")))
            {
                return(null);
            }

            ip = ip.Trim();

            var cacheValue = await _localCache.GetAsync <GeoResult>(ip).AnyContext();

            if (cacheValue.HasValue)
            {
                return(cacheValue.Value);
            }

            GeoResult result = null;

            if (ip.IsPrivateNetwork())
            {
                return(null);
            }

            var database = await GetDatabaseAsync(cancellationToken).AnyContext();

            if (database == null)
            {
                return(null);
            }

            try {
                var city = database.City(ip);
                if (city?.Location != null)
                {
                    result = new GeoResult {
                        Latitude  = city.Location.Latitude,
                        Longitude = city.Location.Longitude,
                        Country   = city.Country.IsoCode,
                        Level1    = city.MostSpecificSubdivision.IsoCode,
                        Locality  = city.City.Name
                    };
                }

                await _localCache.SetAsync(ip, result).AnyContext();

                return(result);
            } catch (Exception ex) {
                if (ex is AddressNotFoundException || ex is GeoIP2Exception)
                {
                    Logger.Trace().Message(ex.Message).Write();
                    await _localCache.SetAsync <GeoResult>(ip, null).AnyContext();
                }
                else
                {
                    Logger.Error().Exception(ex).Message("Unable to resolve geo location for ip: " + ip).Write();
                }

                return(null);
            }
        }
コード例 #2
0
        public async Task <GeoResult> ResolveIpAsync(string ip, CancellationToken cancellationToken = new CancellationToken())
        {
            if (String.IsNullOrEmpty(ip) || (!ip.Contains(".") && !ip.Contains(":")))
            {
                return(null);
            }

            ip = ip.Trim();
            if (ip.IsPrivateNetwork())
            {
                return(null);
            }

            var cacheValue = await _localCache.GetAsync <GeoResult>(ip).AnyContext();

            if (cacheValue.HasValue)
            {
                return(cacheValue.Value);
            }

            GeoResult result   = null;
            var       database = await GetDatabaseAsync(cancellationToken).AnyContext();

            if (database == null)
            {
                return(null);
            }

            try {
                if (database.TryCity(ip, out var city) && city?.Location != null)
                {
                    result = new GeoResult {
                        Latitude  = city.Location.Latitude,
                        Longitude = city.Location.Longitude,
                        Country   = city.Country.IsoCode,
                        Level1    = city.MostSpecificSubdivision.IsoCode,
                        Locality  = city.City.Name
                    };
                }

                await _localCache.SetAsync(ip, result).AnyContext();

                return(result);
            } catch (Exception ex) {
                if (ex is GeoIP2Exception)
                {
                    if (_logger.IsEnabled(LogLevel.Trace))
                    {
                        _logger.LogTrace(ex, ex.Message);
                    }
                    await _localCache.SetAsync <GeoResult>(ip, null).AnyContext();
                }
                else if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(ex, "Unable to resolve geo location for ip: {IP}", ip);
                }

                return(null);
            }
        }
コード例 #3
0
        public static bool TryParse(string input, out GeoResult result)
        {
            result = null;
            if (String.IsNullOrEmpty(input))
            {
                return(false);
            }

            string[] parts = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
            {
                return(false);
            }

            double latitude;

            if (!Double.TryParse(parts[0]?.Trim(), out latitude))
            {
                return(false);
            }

            double longitude;

            if (!Double.TryParse(parts[1]?.Trim(), out longitude))
            {
                return(false);
            }

            result = new GeoResult {
                Latitude = latitude, Longitude = longitude
            };
            return(true);
        }
コード例 #4
0
 private void UpdateGeoAndlocation(PersistentEvent ev, GeoResult result, bool isValidLocation = true) {
     ev.Geo = result?.ToString();
     
     if (result != null && isValidLocation)
         ev.SetLocation(result.ToLocation());
     else
         ev.Data.Remove(Event.KnownDataKeys.Location);
 }
コード例 #5
0
        public async Task<GeoResult> ResolveIpAsync(string ip, CancellationToken cancellationToken = new CancellationToken()) {
            if (String.IsNullOrWhiteSpace(ip) || (!ip.Contains(".") && !ip.Contains(":")))
                return null;

            ip = ip.Trim();

            var cacheValue = await _localCache.GetAsync<GeoResult>(ip).AnyContext();
            if (cacheValue.HasValue)
                return cacheValue.Value;

            GeoResult result = null;

            if (ip.IsPrivateNetwork())
                return null;

            var database = await GetDatabaseAsync(cancellationToken).AnyContext();
            if (database == null)
                return null;

            try {
                var city = database.City(ip);
                if (city?.Location != null) {
                    result = new GeoResult {
                        Latitude = city.Location.Latitude,
                        Longitude = city.Location.Longitude,
                        Country = city.Country.IsoCode,
                        Level1 = city.MostSpecificSubdivision.IsoCode,
                        Locality = city.City.Name
                    };
                }

                await _localCache.SetAsync(ip, result).AnyContext();
                return result;
            } catch (Exception ex) {
                if (ex is AddressNotFoundException || ex is GeoIP2Exception) {
                    Logger.Trace().Message(ex.Message).Write();
                    await _localCache.SetAsync<GeoResult>(ip, null).AnyContext();
                } else {
                    Logger.Error().Exception(ex).Message("Unable to resolve geo location for ip: " + ip).Write();
                }

                return null;
            }
        }
コード例 #6
0
        public static Location ToLocation(this GeoResult result)
        {
            if (result == null)
            {
                return(null);
            }

            if (String.IsNullOrEmpty(result.Country) && String.IsNullOrEmpty(result.Level1) && String.IsNullOrEmpty(result.Level2) && String.IsNullOrEmpty(result.Locality))
            {
                return(null);
            }

            return(new Location {
                Country = result.Country?.Trim(),
                Level1 = result.Level1?.Trim(),
                Level2 = result.Level2?.Trim(),
                Locality = result.Locality?.Trim()
            });
        }
コード例 #7
0
ファイル: GeoResult.cs プロジェクト: jackswei/Exceptionless
        public static bool TryParse(string input, out GeoResult result) {
            result = null;
            if (String.IsNullOrEmpty(input) || !input.Contains(","))
                return false;

            string[] parts = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 2)
                return false;

            double latitude;
            if (!Double.TryParse(parts[0]?.Trim(), out latitude))
                return false;

            double longitude;
            if (!Double.TryParse(parts[1]?.Trim(), out longitude))
                return false;

            result = new GeoResult { Latitude = latitude, Longitude = longitude };
            return true;
        }