public LocationInfo Geolocate(IPAddress address)
        {
            var startTime    = Stopwatch.GetTimestamp();
            var location     = _inner.Geolocate(address);
            var elapsedTicks = Stopwatch.GetTimestamp() - startTime;
            var isSuccessful = location != default;
            var elapsed      = TimeSpan.FromTicks(checked ((long)(elapsedTicks * StopwatchTickScale)));

            _metricsRecorder.RecordGeolocation(isSuccessful, elapsed);
            return(location);
        }
Exemple #2
0
        public async static Task GeolocateIpAddress(this AuditTrailEntry entry, IGeolocator geolocator)
        {
            using (var db = new DatabaseContext())
            {
                var info = await geolocator.Geolocate(entry.UserHostAddress);

                if (info != null)
                {
                    entry.UserCountry     = info.Country;
                    db.Entry(entry).State = EntityState.Modified;
                }

                await db.SaveChangesAsync();
            }
        }
Exemple #3
0
        private async Task Process(TextReader input, Stream output)
        {
            var requestLineTask  = input.ReadLineAsync();
            var responseLineTask = default(ValueTask);
            var buffer           = new byte[256];

            while (true)
            {
                string?requestLine;
                try
                {
                    requestLine = await requestLineTask;
                }
                catch (IOException)
                {
                    break;
                }

                if (requestLine is null)
                {
                    break;
                }

                requestLineTask = input.ReadLineAsync();
                if (!IPAddress.TryParse(requestLine, out var address))
                {
                    continue;
                }

                var locationInfo = _geolocator.Geolocate(address);
                if (locationInfo == default)
                {
                    continue;
                }

                await responseLineTask;
                var   length = CreateResponseLine(buffer, requestLine, locationInfo);
                responseLineTask = output.WriteAsync(buffer.AsMemory(0, length));
            }
        }