Repository navigation
Expand file tree
/
Copy pathIpEnrichmentService.cs
More file actions
115 lines (100 loc) · 3.59 KB
/
Copy pathIpEnrichmentService.cs
File metadata and controls
115 lines (100 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Net;
using MaxMind.GeoIP2;
using Microsoft.Extensions.Logging;
using OpenShock.Common.Options;
namespace OpenShock.Common.Services.Geo;
public sealed class IpEnrichmentService : IIpEnrichmentService, IDisposable
{
private static readonly string[] VpnKeywords =
[
"mullvad", "nordvpn", "expressvpn", "protonvpn", "ipvanish", "surfshark",
"privateinternetaccess", "pia", "hidemyass", "purevpn", "cyberghost",
"windscribe", "tunnelbear", "hotspot shield", "vyprvpn", "airvpn",
"perfect privacy", "ivpn", "ovpn",
// Datacenter / hosting ASNs that VPN exit nodes overwhelmingly use
"digitalocean", "linode", "akamai", "hetzner", "vultr", "ovh",
"amazon", "google", "microsoft", "choopa", "m247", "datacamp",
"frantech", "quadranet", "leaseweb", "serverius", "hostwinds",
"psychz", "tzulo", "nexeon", "misaka",
];
private readonly DatabaseReader? _asnReader;
private readonly DatabaseReader? _cityReader;
private readonly ILogger<IpEnrichmentService> _logger;
public IpEnrichmentService(GeoOptions options, ILogger<IpEnrichmentService> logger)
{
_logger = logger;
_asnReader = TryOpen(options.AsnDbPath, "ASN");
_cityReader = TryOpen(options.CityDbPath, "City");
}
private DatabaseReader? TryOpen(string? path, string dbName)
{
if (string.IsNullOrWhiteSpace(path))
{
_logger.LogInformation("GeoLite2 {DbName} database path not configured, skipping", dbName);
return null;
}
if (!File.Exists(path))
{
_logger.LogWarning("GeoLite2 {DbName} database not found at {Path}", dbName, path);
return null;
}
try
{
return new DatabaseReader(path);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to open GeoLite2 {DbName} database at {Path}", dbName, path);
return null;
}
}
public IpEnrichmentData? Enrich(IPAddress ip)
{
if (_asnReader is null && _cityReader is null) return null;
string? asnOrg = null;
// Null means "unknown" (no ASN DB, lookup miss, or failure); only a resolved ASN org yields a verdict.
bool? isVpn = null;
if (_asnReader is not null)
{
try
{
if (_asnReader.TryAsn(ip, out var asn) && asn is not null)
{
asnOrg = asn.AutonomousSystemOrganization;
if (asnOrg is not null)
{
var lower = asnOrg.ToLowerInvariant();
isVpn = Array.Exists(VpnKeywords, k => lower.Contains(k));
}
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "ASN lookup failed for {Ip}", ip);
}
}
string? countryCode = null;
string? city = null;
if (_cityReader is not null)
{
try
{
if (_cityReader.TryCity(ip, out var cityResponse) && cityResponse is not null)
{
countryCode = cityResponse.Country.IsoCode;
city = cityResponse.City.Name;
}
}
catch (Exception ex)
{
_logger.LogDebug(ex, "City lookup failed for {Ip}", ip);
}
}
return new IpEnrichmentData(asnOrg, isVpn, countryCode, city);
}
public void Dispose()
{
_asnReader?.Dispose();
_cityReader?.Dispose();
}
}