Skip to content

Commit

Permalink
Merge pull request #218 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 1, 2023
2 parents 441034b + 0650a70 commit 7a981f9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ CHANGELOG
if an invalid IP address is passed to them. Previously, they would make
a request to the web service and throw a
`GeoIp2\Exception\InvalidRequestException`.
* The `isAnycast` property was added to `GeoIp2\Record\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.

2.13.0 (2022-08-05)
-------------------
Expand Down
2 changes: 1 addition & 1 deletion 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
+110 −11 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
11 changes: 11 additions & 0 deletions src/Record/Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class Traits implements \JsonSerializable
*/
public readonly bool $isAnonymousVpn;

/**
* @var bool This is true if the IP address belongs to an [anycast
* network](https://en.wikipedia.org/wiki/Anycast). This property is not
* available from GeoLite databases or web services.
*/
public readonly bool $isAnycast;

/**
* @var bool This is true if the IP address belongs
* to a hosting or VPN provider (see description of isAnonymousVpn property).
Expand Down Expand Up @@ -198,6 +205,7 @@ public function __construct(array $record)
$this->ipAddress = $record['ip_address'] ?? null;
$this->isAnonymous = $record['is_anonymous'] ?? false;
$this->isAnonymousVpn = $record['is_anonymous_vpn'] ?? false;
$this->isAnycast = $record['is_anycast'] ?? false;
$this->isHostingProvider = $record['is_hosting_provider'] ?? false;
$this->isLegitimateProxy = $record['is_legitimate_proxy'] ?? false;
$this->isp = $record['isp'] ?? null;
Expand Down Expand Up @@ -242,6 +250,9 @@ public function jsonSerialize(): array
if ($this->isAnonymousVpn !== false) {
$js['is_anonymous_vpn'] = $this->isAnonymousVpn;
}
if ($this->isAnycast !== false) {
$js['is_anycast'] = $this->isAnycast;
}
if ($this->isHostingProvider !== false) {
$js['is_hosting_provider'] = $this->isHostingProvider;
}
Expand Down
28 changes: 27 additions & 1 deletion tests/GeoIp2/Test/Database/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,34 @@ public function testConnectionType(): void
$ipAddress = '1.0.1.1';

$record = $reader->connectionType($ipAddress);
$this->assertSame('Cable/DSL', $record->connectionType);
$this->assertSame('Cellular', $record->connectionType);
$this->assertSame($ipAddress, $record->ipAddress);
$this->assertSame('1.0.1.0/24', $record->network);
$reader->close();
}

public function testCity(): void
{
$reader = new Reader('maxmind-db/test-data/GeoIP2-City-Test.mmdb');

// This IP has is_anycast
$record = $reader->city('214.1.1.0');
$this->assertTrue($record->traits->isAnycast);

$reader->close();
}

public function testCountry(): void
{
$reader = new Reader('maxmind-db/test-data/GeoIP2-Country-Test.mmdb');

// This IP has is_anycast
$record = $reader->country('214.1.1.0');
$this->assertTrue($record->traits->isAnycast);

$reader->close();
}

public function testDomain(): void
{
$reader = new Reader('maxmind-db/test-data/GeoIP2-Domain-Test.mmdb');
Expand Down Expand Up @@ -214,6 +236,10 @@ public function testEnterprise(): void
$this->assertSame('310', $record->traits->mobileCountryCode);
$this->assertSame('004', $record->traits->mobileNetworkCode);

// This IP has is_anycast
$record = $reader->enterprise('214.1.1.0');
$this->assertTrue($record->traits->isAnycast);

$reader->close();
}

Expand Down
2 changes: 2 additions & 0 deletions tests/GeoIp2/Test/Model/CountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CountryTest extends TestCase
],
'traits' => [
'ip_address' => '1.2.3.4',
'is_anycast' => true,
'prefix_len' => 24,
],
];
Expand Down Expand Up @@ -188,6 +189,7 @@ public function testJsonSerialize(): void
],
'traits' => [
'ip_address' => '1.2.3.4',
'is_anycast' => true,
'network' => '1.2.3.0/24',
],
];
Expand Down
7 changes: 7 additions & 0 deletions tests/GeoIp2/Test/Model/InsightsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function testFull(): void
'ip_address' => '1.2.3.4',
'is_anonymous' => true,
'is_anonymous_vpn' => true,
'is_anycast' => true,
'is_hosting_provider' => true,
'is_legitimate_proxy' => true,
'is_public_proxy' => true,
Expand Down Expand Up @@ -170,6 +171,11 @@ public function testFull(): void
'$model->traits->isAnonymous is true'
);

$this->assertTrue(
$model->traits->isAnycast,
'$model->traits->isAnycast is true'
);

$this->assertTrue(
$model->traits->isHostingProvider,
'$model->traits->isHostingProvider is true'
Expand Down Expand Up @@ -255,6 +261,7 @@ public function testFull(): void
'ip_address' => '1.2.3.4',
'is_anonymous' => true,
'is_anonymous_vpn' => true,
'is_anycast' => true,
'is_hosting_provider' => true,
'is_legitimate_proxy' => true,
'is_public_proxy' => true,
Expand Down
11 changes: 11 additions & 0 deletions tests/GeoIp2/Test/WebService/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ClientTest extends TestCase
'maxmind' => ['queries_remaining' => 11],
'traits' => [
'ip_address' => '1.2.3.4',
'is_anycast' => true,
'network' => '1.2.3.0/24',
],
];
Expand Down Expand Up @@ -236,6 +237,11 @@ public function testCountry(): void
'registered_country is_in_european_union is false'
);

$this->assertTrue(
$country->traits->isAnycast,
'is_anycast'
);

$this->assertSame(
'1.2.3.0/24',
$country->traits->network,
Expand All @@ -255,6 +261,11 @@ public function testInsights(): void
'continent geoname_id is 42'
);

$this->assertTrue(
$record->traits->isAnycast,
'is_anycast'
);

$this->assertSame(
'1.2.3.0/24',
$record->traits->network,
Expand Down

0 comments on commit 7a981f9

Please sign in to comment.