Example #1
0
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            // Random swedish IP for testing purposes
            if (e.Report.RemoteAddress == "::1" || e.Report.RemoteAddress == "127.0.0.1")
            {
                e.Report.RemoteAddress = "94.254.57.227";
            }

            var numberStyles = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign;
            var collection   = e.Report.GetCoderrCollection();

            if (collection != null)
            {
                var latitude  = 0d;
                var longitude = 0d;
                var gotLat    = collection.Properties.TryGetValue("Longitude", out var longitudeStr) &&
                                double.TryParse(longitudeStr, numberStyles,
                                                CultureInfo.InvariantCulture, out longitude);

                var gotLong = collection.Properties.TryGetValue("Latitude", out var latitudeStr) &&
                              double.TryParse(latitudeStr, numberStyles,
                                              CultureInfo.InvariantCulture, out latitude);
                if (gotLat && latitude > 0 && gotLong && longitude > 0)
                {
                    var errorOrigin2 = new ErrorOrigin(e.Report.RemoteAddress, longitude, latitude);
                    await _repository.CreateAsync(errorOrigin2, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);

                    return;
                }
            }


            var latitude1  = e.Report.FindCollectionProperty("ReportLatitude");
            var longitude1 = e.Report.FindCollectionProperty("ReportLongitude");

            if (longitude1 != null &&
                double.TryParse(latitude1, numberStyles, CultureInfo.InvariantCulture,
                                out var latitude2) &&
                latitude1 != null &&
                double.TryParse(longitude1, numberStyles, CultureInfo.InvariantCulture,
                                out var longitude2))
            {
                var errorOrigin2 = new ErrorOrigin(e.Report.RemoteAddress, longitude2, latitude2);
                await _repository.CreateAsync(errorOrigin2, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);

                return;
            }


            if (string.IsNullOrEmpty(e.Report.RemoteAddress) ||
                string.IsNullOrEmpty(_originConfiguration.Value?.ApiKey))
            {
                return;
            }


            var origin = new ErrorOrigin(e.Report.RemoteAddress);
            await _repository.CreateAsync(origin, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
        }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            if (string.IsNullOrEmpty(e.Report.RemoteAddress))
            {
                return;
            }

            if (string.IsNullOrEmpty(_originConfiguration.Value?.ApiKey))
            {
                return;
            }

            if (e.Report.RemoteAddress == "::1")
            {
                return;
            }
            if (e.Report.RemoteAddress == "127.0.0.1")
            {
                e.Report.RemoteAddress = "94.254.57.227";
            }

            var    url     = $"http://api.ipstack.com/{e.Report.RemoteAddress}?access_key={_originConfiguration.Value.ApiKey}";
            var    request = WebRequest.CreateHttp(url);
            string json    = "";

            try
            {
                var response = await request.GetResponseAsync();

                var stream = response.GetResponseStream();
                var reader = new StreamReader(stream);
                json = await reader.ReadToEndAsync();

                var jsonObj = JObject.Parse(json);

                /*    /*{"ip":"94.254.21.175","country_code":"SE","country_name":"Sweden","region_code":"10","region_name":"Dalarnas Lan","city":"Falun","zipcode":"",
                 * "latitude":60.6,"longitude":15.6333,
                 * "metro_code":"","areacode":""}*/

                var lat = double.Parse(jsonObj["latitude"].Value <string>(), CultureInfo.InvariantCulture);
                var lon = double.Parse(jsonObj["longitude"].Value <string>(), CultureInfo.InvariantCulture);
                var cmd = new ErrorOrigin(e.Report.RemoteAddress, lon, lat)
                {
                    City        = jsonObj["city"].ToString(),
                    CountryCode = jsonObj["country_code"].ToString(),
                    CountryName = jsonObj["country_name"].ToString(),
                    RegionCode  = jsonObj["region_code"].ToString(),
                    RegionName  = jsonObj["region_name"].ToString(),
                    ZipCode     = jsonObj["zip"].ToString()
                };

                await _repository.CreateAsync(cmd, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
            }
            catch (Exception exception)
            {
                _logger.Error($"Failed to store location for incident {e.Incident.Id}/report {e.Report.Id}: {json}", exception);
            }
        }
Example #3
0
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(ReportAddedToIncident e)
        {
            if (string.IsNullOrEmpty(e.Report.RemoteAddress))
            {
                return;
            }

            if (e.Report.RemoteAddress == "::1")
            {
                return;
            }
            if (e.Report.RemoteAddress == "127.0.0.1")
            {
                return;
            }

            var    request = WebRequest.CreateHttp("http://freegeoip.net/json/" + e.Report.RemoteAddress);
            string json    = "";

            try
            {
                var response = await request.GetResponseAsync();

                var stream = response.GetResponseStream();
                var reader = new StreamReader(stream);
                json = await reader.ReadToEndAsync();

                var jsonObj = JObject.Parse(json);

                /*    /*{"ip":"94.254.21.175","country_code":"SE","country_name":"Sweden","region_code":"10","region_name":"Dalarnas Lan","city":"Falun","zipcode":"",
                 * "latitude":60.6,"longitude":15.6333,
                 * "metro_code":"","areacode":""}*/

                var lat = double.Parse(jsonObj["latitude"].Value <string>(), CultureInfo.InvariantCulture);
                var lon = double.Parse(jsonObj["longitude"].Value <string>(), CultureInfo.InvariantCulture);
                var cmd = new ErrorOrigin(e.Report.RemoteAddress, lon, lat)
                {
                    City        = jsonObj["city"].ToString(),
                    CountryCode = jsonObj["country_code"].ToString(),
                    CountryName = jsonObj["country_name"].ToString(),
                    RegionCode  = jsonObj["region_code"].ToString(),
                    RegionName  = jsonObj["region_name"].ToString(),
                    ZipCode     = jsonObj["zip_code"].ToString()
                };

                await _repository.CreateAsync(cmd, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
            }
            catch (Exception exception)
            {
                _logger.Error("Failed to store location: " + json, exception);
            }
        }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            if (string.IsNullOrEmpty(e.Report.RemoteAddress))
            {
                return;
            }

            if (string.IsNullOrEmpty(_originConfiguration.Value?.ApiKey))
            {
                return;
            }

            // Random swedish IP for testing purposes
            if (e.Report.RemoteAddress == "::1" || e.Report.RemoteAddress == "127.0.0.1")
            {
                e.Report.RemoteAddress = "94.254.57.227";
            }

            var errorOrigin = await LookupIpAddress(e);

            await _repository.CreateAsync(errorOrigin, e.Incident.ApplicationId, e.Incident.Id, e.Report.Id);
        }