Skip to content

Commit

Permalink
Merge pull request #294 from maxmind/greg/is-anycast
Browse files Browse the repository at this point in the history
Add support for is_anycast
  • Loading branch information
horgh authored Dec 4, 2023
2 parents 09fb42d + cc17291 commit 2b38ff4
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
11 changes: 10 additions & 1 deletion MaxMind.GeoIP2.UnitTests/DatabaseReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void ConnectionType_ValidResponse()
using var reader = new DatabaseReader(_connectionTypeDatabaseFile);
var ipAddress = "1.0.1.0";
var response = reader.ConnectionType(ipAddress);
Assert.Equal("Cable/DSL", response.ConnectionType);
Assert.Equal("Cellular", response.ConnectionType);
Assert.Equal(ipAddress, response.IPAddress);
Assert.Equal("1.0.1.0/24", response.Network?.ToString());
}
Expand Down Expand Up @@ -174,6 +174,9 @@ public void Enterprise_ValidResponse()
response = reader.Enterprise("149.101.100.0");
Assert.Equal("310", response.Traits.MobileCountryCode);
Assert.Equal("004", response.Traits.MobileNetworkCode);

response = reader.Enterprise("214.1.1.0");
Assert.True(response.Traits.IsAnycast);
}

[Fact]
Expand Down Expand Up @@ -204,6 +207,9 @@ public void Country_ValidResponse()
Assert.False(response.RegisteredCountry.IsInEuropeanUnion);
Assert.False(response.RepresentedCountry.IsInEuropeanUnion);
Assert.Equal("81.2.69.160/27", response.Traits.Network?.ToString());

response = reader.Country("214.1.1.0");
Assert.True(response.Traits.IsAnycast);
}

[Fact]
Expand All @@ -230,6 +236,9 @@ public void City_ValidResponse()
Assert.False(response.RegisteredCountry.IsInEuropeanUnion);
Assert.False(response.RepresentedCountry.IsInEuropeanUnion);
Assert.Equal("81.2.69.160/27", response.Traits.Network?.ToString());

response = reader.City("214.1.1.0");
Assert.True(response.Traits.IsAnycast);
}

[Fact]
Expand Down
2 changes: 2 additions & 0 deletions MaxMind.GeoIP2.UnitTests/DeserializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private static void CanDeserializeCountryResponse(CountryResponse resp)
Assert.Equal("United Kingdom", resp.RepresentedCountry.Name);
Assert.Equal("military", resp.RepresentedCountry.Type);

Assert.True(resp.Traits.IsAnycast);
Assert.Equal("1.2.3.4", resp.Traits.IPAddress);
}

Expand Down Expand Up @@ -100,6 +101,7 @@ private static void CanDeserializeInsightsResponse(InsightsResponse insights)
Assert.True(insights.Traits.IsAnonymousProxy);
Assert.True(insights.Traits.IsSatelliteProvider);
#pragma warning restore 0618
Assert.True(insights.Traits.IsAnycast);
Assert.Equal("Comcast", insights.Traits.Isp);
Assert.Equal("310", insights.Traits.MobileCountryCode);
Assert.Equal("004", insights.Traits.MobileNetworkCode);
Expand Down
2 changes: 2 additions & 0 deletions MaxMind.GeoIP2.UnitTests/ResponseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ internal static class ResponseHelper
""is_anonymous"": true,
""is_anonymous_proxy"": true,
""is_anonymous_vpn"": true,
""is_anycast"": true,
""is_hosting_provider"": true,
""is_public_proxy"": true,
""is_residential_proxy"": true,
Expand Down Expand Up @@ -109,6 +110,7 @@ internal static class ResponseHelper
},
""traits"": {
""ip_address"": ""1.2.3.4"",
""is_anycast"": true,
""network"": ""1.2.3.0/24""
}
}";
Expand Down
2 changes: 1 addition & 1 deletion MaxMind.GeoIP2.UnitTests/TestData/MaxMind-DB
Submodule MaxMind-DB updated 63 files
+11 −0 .github/dependabot.yml
+52 −0 .github/workflows/codeql-analysis.yml
+38 −0 .github/workflows/go.yml
+21 −0 .github/workflows/golangci-lint.yml
+1 −0 .gitignore
+708 −0 .golangci.toml
+68 −0 cmd/write-test-data/main.go
+13 −0 go.mod
+16 −0 go.sum
+6 −0 perltidyrc
+178 −0 pkg/writer/decoder.go
+182 −0 pkg/writer/geoip2.go
+39 −0 pkg/writer/ip.go
+245 −0 pkg/writer/maxmind.go
+73 −0 pkg/writer/nestedstructures.go
+58 −0 pkg/writer/writer.go
+322 −1 source-data/GeoIP2-City-Test.json
+15 −10 source-data/GeoIP2-Connection-Type-Test.json
+99 −0 source-data/GeoIP2-Country-Test.json
+5 −0 source-data/GeoIP2-Domain-Test.json
+347 −1 source-data/GeoIP2-Enterprise-Test.json
+296 −0 source-data/GeoIP2-Precision-Enterprise-Sandbox-Test.json
+412 −2 source-data/GeoIP2-Precision-Enterprise-Test.json
+15 −0 source-data/GeoIP2-Static-IP-Score-Test.json
+18 −0 source-data/GeoIP2-User-Count-Test.json
+168 −0 source-data/GeoLite2-City-Test.json
+92 −0 source-data/GeoLite2-Country-Test.json
+0 −15 source-data/README
+ test-data/GeoIP2-Anonymous-IP-Test.mmdb
+ test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb
+ test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb
+ test-data/GeoIP2-City-Test.mmdb
+ test-data/GeoIP2-Connection-Type-Test.mmdb
+ test-data/GeoIP2-Country-Test.mmdb
+ test-data/GeoIP2-DensityIncome-Test.mmdb
+ test-data/GeoIP2-Domain-Test.mmdb
+ test-data/GeoIP2-Enterprise-Test.mmdb
+ test-data/GeoIP2-ISP-Test.mmdb
+ test-data/GeoIP2-Precision-Enterprise-Test.mmdb
+ test-data/GeoIP2-Static-IP-Score-Test.mmdb
+ test-data/GeoIP2-User-Count-Test.mmdb
+ test-data/GeoLite2-ASN-Test.mmdb
+ test-data/GeoLite2-City-Test.mmdb
+ test-data/GeoLite2-Country-Test.mmdb
+ test-data/MaxMind-DB-no-ipv4-search-tree.mmdb
+ test-data/MaxMind-DB-string-value-entries.mmdb
+ test-data/MaxMind-DB-test-broken-pointers-24.mmdb
+ test-data/MaxMind-DB-test-broken-search-tree-24.mmdb
+ test-data/MaxMind-DB-test-decoder.mmdb
+ test-data/MaxMind-DB-test-ipv4-24.mmdb
+ test-data/MaxMind-DB-test-ipv4-28.mmdb
+ test-data/MaxMind-DB-test-ipv4-32.mmdb
+ test-data/MaxMind-DB-test-ipv6-24.mmdb
+ test-data/MaxMind-DB-test-ipv6-28.mmdb
+ test-data/MaxMind-DB-test-ipv6-32.mmdb
+ test-data/MaxMind-DB-test-metadata-pointers.mmdb
+ test-data/MaxMind-DB-test-mixed-24.mmdb
+ test-data/MaxMind-DB-test-mixed-28.mmdb
+ test-data/MaxMind-DB-test-mixed-32.mmdb
+ test-data/MaxMind-DB-test-nested.mmdb
+ test-data/MaxMind-DB-test-pointer-decoder.mmdb
+28 −12 test-data/README.md
+0 −695 test-data/write-test-data.pl
1 change: 1 addition & 0 deletions MaxMind.GeoIP2.UnitTests/WebServiceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public void MissingKeys()
Assert.False(traits.IsAnonymousProxy);
Assert.False(traits.IsSatelliteProvider);
#pragma warning restore 0618
Assert.False(traits.IsAnycast);

foreach (var c in new[]
{
Expand Down
67 changes: 67 additions & 0 deletions MaxMind.GeoIP2/Model/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Traits(
[Parameter("is_anonymous")] bool isAnonymous = false,
[Parameter("is_anonymous_proxy")] bool isAnonymousProxy = false,
[Parameter("is_anonymous_vpn")] bool isAnonymousVpn = false,
[Parameter("is_anycast")] bool isAnycast = false,
[Parameter("is_hosting_provider")] bool isHostingProvider = false,
[Parameter("is_legitimate_proxy")] bool isLegitimateProxy = false,
[Parameter("is_public_proxy")] bool isPublicProxy = false,
Expand All @@ -59,6 +60,7 @@ public Traits(
IsAnonymousProxy = isAnonymousProxy;
#pragma warning restore 618
IsAnonymousVpn = isAnonymousVpn;
IsAnycast = isAnycast;
IsHostingProvider = isHostingProvider;
IsLegitimateProxy = isLegitimateProxy;
IsPublicProxy = isPublicProxy;
Expand All @@ -77,6 +79,61 @@ public Traits(
UserType = userType;
}

/// <summary>
/// Constructor for binary compatibility.
/// </summary>
[Obsolete]
public Traits(
long? autonomousSystemNumber,
string? autonomousSystemOrganization,
string? connectionType,
string? domain,
string? ipAddress,
bool isAnonymous,
bool isAnonymousProxy,
bool isAnonymousVpn,
bool isHostingProvider,
bool isLegitimateProxy,
bool isPublicProxy,
bool isResidentialProxy,
bool isSatelliteProvider,
bool isTorExitNode,
string? isp,
string? mobileCountryCode,
string? mobileNetworkCode,
string? organization,
string? userType,
Network? network,
double? staticIPScore,
int? userCount
) : this(
autonomousSystemNumber,
autonomousSystemOrganization,
connectionType,
domain,
ipAddress,
isAnonymous,
isAnonymousProxy,
isAnonymousVpn,
false, // isAnycast
isHostingProvider,
isLegitimateProxy,
isPublicProxy,
isResidentialProxy,
isSatelliteProvider,
isTorExitNode,
isp,
mobileCountryCode,
mobileNetworkCode,
organization,
userType,
network,
staticIPScore,
userCount
)
{
}

/// <summary>
/// The
/// <a
Expand Down Expand Up @@ -153,6 +210,15 @@ public Traits(
[Obsolete("Use our GeoIP2 Anonymous IP database instead.")]
public bool IsAnonymousProxy { get; internal set; }

/// <summary>
/// This is true if the IP address belongs to an <a
/// href="https://en.wikipedia.org/wiki/Anycast">anycast network</a>.
/// This is not available from GeoLite databases or web services.
/// </summary>
[JsonInclude]
[JsonPropertyName("is_anycast")]
public bool IsAnycast { get; internal set; }

/// <summary>
/// This is true if the IP address is registered to an anonymous
/// VPN provider.
Expand Down Expand Up @@ -372,6 +438,7 @@ public override string ToString()
$"{nameof(IsAnonymousProxy)}: {IsAnonymousProxy}, " +
#pragma warning restore 618
$"{nameof(IsAnonymousVpn)}: {IsAnonymousVpn}, " +
$"{nameof(IsAnycast)}: {IsAnycast}, " +
$"{nameof(IsHostingProvider)}: {IsHostingProvider}, " +
$"{nameof(IsLegitimateProxy)}: {IsLegitimateProxy}, " +
$"{nameof(IsPublicProxy)}: {IsPublicProxy}, " +
Expand Down
5 changes: 5 additions & 0 deletions releasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ GeoIP2 .NET API Release Notes
However, if you are using .NET 5.0, the .NET Standard 2.1 target should
continue working for you.
* .NET 7.0 and .NET 8.0 have been added as a target.
* The `IsAnycast` property was added to `MaxMind.GeoIP2.Model.Traits`. This
returns `true` if the IP address belongs to an [anycast
network](https://en.wikipedia.org/wiki/Anycast). This is available for the
GeoIP2 Country, City Plus, and Insights web services and the GeoIP2 Country,
City, and Enterprise databases.

5.1.0 (2022-02-04)
------------------
Expand Down

0 comments on commit 2b38ff4

Please sign in to comment.