1
1
using System ;
2
2
using System . Globalization ;
3
- using System . IO ;
4
- using System . Net ;
5
3
using System . Threading . Tasks ;
4
+ using Coderr . Server . Abstractions . Config ;
6
5
using Coderr . Server . Domain . Modules . ErrorOrigins ;
6
+ using Coderr . Server . ReportAnalyzer . Abstractions . ErrorReports ;
7
7
using Coderr . Server . ReportAnalyzer . Abstractions . Incidents ;
8
8
using DotNetCqs ;
9
9
using log4net ;
10
- using Newtonsoft . Json . Linq ;
11
- using Coderr . Server . Abstractions . Config ;
12
- using Coderr . Server . ReportAnalyzer ;
13
- using Coderr . Server . ReportAnalyzer . Abstractions . ErrorReports ;
14
10
15
11
namespace Coderr . Server . ReportAnalyzer . ErrorOrigins . Handlers
16
12
{
@@ -21,14 +17,15 @@ public class StorePositionFromNewReport : IMessageHandler<ReportAddedToIncident>
21
17
{
22
18
private readonly ILog _logger = LogManager . GetLogger ( typeof ( StorePositionFromNewReport ) ) ;
23
19
private readonly IErrorOriginRepository _repository ;
24
- private IConfiguration < OriginsConfiguration > _originConfiguration ;
20
+ private readonly IConfiguration < OriginsConfiguration > _originConfiguration ;
25
21
26
22
/// <summary>
27
23
/// Creates a new instance of <see cref="StorePositionFromNewReport" />.
28
24
/// </summary>
29
25
/// <param name="repository">repos</param>
30
26
/// <exception cref="ArgumentNullException">repository</exception>
31
- public StorePositionFromNewReport ( IErrorOriginRepository repository , IConfiguration < OriginsConfiguration > originConfiguration )
27
+ public StorePositionFromNewReport ( IErrorOriginRepository repository ,
28
+ IConfiguration < OriginsConfiguration > originConfiguration )
32
29
{
33
30
_repository = repository ?? throw new ArgumentNullException ( nameof ( repository ) ) ;
34
31
_originConfiguration = originConfiguration ;
@@ -43,16 +40,23 @@ public StorePositionFromNewReport(IErrorOriginRepository repository, IConfigurat
43
40
/// </returns>
44
41
public async Task HandleAsync ( IMessageContext context , ReportAddedToIncident e )
45
42
{
43
+ // Random swedish IP for testing purposes
44
+ if ( e . Report . RemoteAddress == "::1" || e . Report . RemoteAddress == "127.0.0.1" )
45
+ e . Report . RemoteAddress = "94.254.57.227" ;
46
+
47
+ var numberStyles = NumberStyles . AllowDecimalPoint | NumberStyles . AllowLeadingSign ;
46
48
var collection = e . Report . GetCoderrCollection ( ) ;
47
49
if ( collection != null )
48
50
{
49
51
var latitude = 0d ;
50
52
var longitude = 0d ;
51
53
var gotLat = collection . Properties . TryGetValue ( "Longitude" , out var longitudeStr )
52
- && double . TryParse ( longitudeStr , NumberStyles . AllowDecimalPoint , CultureInfo . InvariantCulture , out longitude ) ;
54
+ && double . TryParse ( longitudeStr , numberStyles ,
55
+ CultureInfo . InvariantCulture , out longitude ) ;
53
56
54
57
var gotLong = collection . Properties . TryGetValue ( "Latitude" , out var latitudeStr )
55
- && double . TryParse ( latitudeStr , NumberStyles . AllowDecimalPoint , CultureInfo . InvariantCulture , out latitude ) ;
58
+ && double . TryParse ( latitudeStr , numberStyles ,
59
+ CultureInfo . InvariantCulture , out latitude ) ;
56
60
if ( gotLat && latitude > 0 && gotLong && longitude > 0 )
57
61
{
58
62
var errorOrigin2 = new ErrorOrigin ( e . Report . RemoteAddress , longitude , latitude ) ;
@@ -65,66 +69,24 @@ public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
65
69
var latitude1 = e . Report . FindCollectionProperty ( "ReportLatitude" ) ;
66
70
var longitude1 = e . Report . FindCollectionProperty ( "ReportLongitude" ) ;
67
71
if ( longitude1 != null
68
- && double . TryParse ( latitude1 , NumberStyles . AllowDecimalPoint , CultureInfo . InvariantCulture , out var latitude2 )
72
+ && double . TryParse ( latitude1 , numberStyles , CultureInfo . InvariantCulture ,
73
+ out var latitude2 )
69
74
&& latitude1 != null
70
- && double . TryParse ( longitude1 , NumberStyles . AllowDecimalPoint , CultureInfo . InvariantCulture , out var longitude2 ) )
75
+ && double . TryParse ( longitude1 , numberStyles , CultureInfo . InvariantCulture ,
76
+ out var longitude2 ) )
71
77
{
72
78
var errorOrigin2 = new ErrorOrigin ( e . Report . RemoteAddress , longitude2 , latitude2 ) ;
73
79
await _repository . CreateAsync ( errorOrigin2 , e . Incident . ApplicationId , e . Incident . Id , e . Report . Id ) ;
74
80
return ;
75
81
}
76
82
77
83
78
- if ( string . IsNullOrEmpty ( e . Report . RemoteAddress ) || string . IsNullOrEmpty ( _originConfiguration . Value ? . ApiKey ) )
79
- {
80
- return ;
81
- }
82
-
83
-
84
- // Random swedish IP for testing purposes
85
- if ( e . Report . RemoteAddress == "::1" || e . Report . RemoteAddress == "127.0.0.1" )
86
- e . Report . RemoteAddress = "94.254.57.227" ;
84
+ if ( string . IsNullOrEmpty ( e . Report . RemoteAddress ) ||
85
+ string . IsNullOrEmpty ( _originConfiguration . Value ? . ApiKey ) ) return ;
87
86
88
- var errorOrigin = await LookupIpAddress ( e ) ;
89
- await _repository . CreateAsync ( errorOrigin , e . Incident . ApplicationId , e . Incident . Id , e . Report . Id ) ;
90
- }
91
-
92
- private async Task < ErrorOrigin > LookupIpAddress ( ReportAddedToIncident e )
93
- {
94
- var url = $ "http://api.ipstack.com/{ e . Report . RemoteAddress } ?access_key={ _originConfiguration . Value . ApiKey } ";
95
- var request = WebRequest . CreateHttp ( url ) ;
96
- var json = "" ;
97
- ErrorOrigin errorOrigin ;
98
- try
99
- {
100
- var response = await request . GetResponseAsync ( ) ;
101
- var stream = response . GetResponseStream ( ) ;
102
- var reader = new StreamReader ( stream ) ;
103
- json = await reader . ReadToEndAsync ( ) ;
104
- var jsonObj = JObject . Parse ( json ) ;
105
-
106
- /* /*{"ip":"94.254.21.175","country_code":"SE","country_name":"Sweden","region_code":"10","region_name":"Dalarnas Lan","city":"Falun","zipcode":"",
107
- * "latitude":60.6,"longitude":15.6333,
108
- * "metro_code":"","areacode":""}*/
109
-
110
- var lat = double . Parse ( jsonObj [ "latitude" ] . Value < string > ( ) , CultureInfo . InvariantCulture ) ;
111
- var lon = double . Parse ( jsonObj [ "longitude" ] . Value < string > ( ) , CultureInfo . InvariantCulture ) ;
112
- errorOrigin = new ErrorOrigin ( e . Report . RemoteAddress , lon , lat )
113
- {
114
- City = jsonObj [ "city" ] . ToString ( ) ,
115
- CountryCode = jsonObj [ "country_code" ] . ToString ( ) ,
116
- CountryName = jsonObj [ "country_name" ] . ToString ( ) ,
117
- RegionCode = jsonObj [ "region_code" ] . ToString ( ) ,
118
- RegionName = jsonObj [ "region_name" ] . ToString ( ) ,
119
- ZipCode = jsonObj [ "zip" ] . ToString ( )
120
- } ;
121
- }
122
- catch ( Exception exception )
123
- {
124
- throw new InvalidOperationException ( $ "Failed to call lookupService or parse the JSON: { json } .", exception ) ;
125
- }
126
87
127
- return errorOrigin ;
88
+ var origin = new ErrorOrigin ( e . Report . RemoteAddress ) ;
89
+ await _repository . CreateAsync ( origin , e . Incident . ApplicationId , e . Incident . Id , e . Report . Id ) ;
128
90
}
129
91
}
130
92
}
0 commit comments