Skip to content
Permalink
Browse files

Merge pull request #138 from maxmind/greg/network

Add network support
  • Loading branch information...
rafl committed Oct 2, 2019
2 parents eac52bc + 9cf4f40 commit 907ddd2c83532a013fcbccd0de768a9e4bf83bc4
@@ -2,10 +2,9 @@
sudo: false
language: php
dist: trusty

matrix:
include:
- php: '5.4'
- php: '5.5'
- php: '5.6'
- php: '7.0'
- php: '7.1'
@@ -14,22 +13,32 @@ matrix:
env:
- RUN_LINTER=1
- RUN_SNYK=1
- php: nightly
- php: '7.4snapshot'
- php: 'master'
- php: 'nightly'
fast_finish: true
allow_failures:
- php: nightly
- php: '7.4snapshot'
- php: 'master'
- php: 'nightly'

before_install:
- composer install --dev -n --prefer-source
- phpenv rehash
- "if [[ $RUN_SNYK && $SNYK_TOKEN ]]; then sudo apt-get install -y nodejs; npm install -g snyk; fi"

install:
- "if [[ $RUN_SNYK && $SNYK_TOKEN ]]; then snyk test --org=maxmind; fi"

script:
- mkdir -p build/logs
- "if [[ $RUN_LINTER ]]; then vendor/bin/php-cs-fixer fix --verbose --diff --dry-run --config=.php_cs; fi"
- "if [[ $RUN_LINTER ]]; then vendor/bin/phpcs --standard=PSR2 src/; fi"
- vendor/bin/phpunit -c phpunit.xml.dist

after_script:
- php vendor/bin/coveralls

after_success:
- "if [[ $RUN_SNYK && $SNYK_TOKEN && $TRAVIS_BRANCH == 'master' && $TRAVIS_PULL_REQUEST == 'false' ]]; then snyk monitor --org=maxmind --project-name=maxmind/GeoIP2-php; fi"
notifications:
@@ -1,9 +1,16 @@
CHANGELOG
=========

2.9.1
2.10.0
------------------

* PHP 5.6 or greater is now required.
* The `network` property was added to `GeoIp2\Record\Traits`,
`GeoIp2\Model\AnonymousIp`, `GeoIp2\Model\Asn`,
`GeoIp2\Model\ConnectionType`, `Geoip2\Model\Domain`,
and `GeoIp2\Model\Isp`. This is a string in CIDR format representing the
largest network where all of the properties besides `ipAddress` have the
same value.
* Updated documentation of anonymizer properties - `isAnonymousVpn`
and `isHostingProvider` - to be more descriptive.

@@ -140,6 +140,8 @@ print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
print($record->traits->network . "\n"); // '128.101.101.101/32'
```

### Anonymous IP Example ###
@@ -157,6 +159,7 @@ $record = $reader->anonymousIp('128.101.101.101');
if ($record->isAnonymous) { print "anon\n"; }
print($record->ipAddress . "\n"); // '128.101.101.101'
print($record->network . "\n"); // '128.101.101.101/32'
```

@@ -175,6 +178,7 @@ $record = $reader->connectionType('128.101.101.101');
print($record->connectionType . "\n"); // 'Corporate'
print($record->ipAddress . "\n"); // '128.101.101.101'
print($record->network . "\n"); // '128.101.101.101/32'
```

@@ -193,6 +197,7 @@ $record = $reader->domain('128.101.101.101');
print($record->domain . "\n"); // 'umn.edu'
print($record->ipAddress . "\n"); // '128.101.101.101'
print($record->network . "\n"); // '128.101.101.101/32'
```

@@ -228,6 +233,8 @@ print($record->location->accuracyRadius . "\n"); // 50
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
print($record->traits->network . "\n"); // '128.101.101.101/32'
```

### ISP Example ###
@@ -249,6 +256,7 @@ print($record->isp . "\n"); // 'University of Minnesota'
print($record->organization . "\n"); // 'University of Minnesota'
print($record->ipAddress . "\n"); // '128.101.101.101'
print($record->network . "\n"); // '128.101.101.101/32'
```

@@ -299,6 +307,8 @@ print($record->postal->code . "\n"); // '55455'
print($record->location->latitude . "\n"); // 44.9733
print($record->location->longitude . "\n"); // -93.2323
print($record->traits->network . "\n"); // '128.101.101.101/32'
```

## Values to use for Database or Array Keys ##
@@ -376,7 +386,7 @@ to the client API, please see

## Requirements ##

This library requires PHP 5.4 or greater.
This library requires PHP 5.6 or greater.

This library also relies on the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php).

@@ -13,9 +13,10 @@
}
],
"require": {
"maxmind-db/reader": "~1.0",
"maxmind-db/reader": "~1.5",
"maxmind/web-service-common": "~0.5",
"php": ">=5.4"
"php": ">=5.6",
"ext-json": "*"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "2.*",
@@ -0,0 +1,25 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
use GeoIp2\Database\Reader;
srand(0);
$reader = new Reader('GeoIP2-City.mmdb');
$count = 500000;
$startTime = microtime(true);
for ($i = 0; $i < $count; ++$i) {
$ip = long2ip(rand(0, pow(2, 32) - 1));
try {
$t = $reader->city($ip);
} catch (\GeoIp2\Exception\AddressNotFoundException $e) {
}
if ($i % 10000 === 0) {
echo $i . ' ' . $ip . "\n";
}
}
$endTime = microtime(true);
$duration = $endTime - $startTime;
echo 'Requests per second: ' . $count / $duration . "\n";
@@ -34,6 +34,7 @@
class Reader implements ProviderInterface
{
private $dbReader;
private $dbType;
private $locales;
/**
@@ -51,6 +52,7 @@ public function __construct(
$locales = ['en']
) {
$this->dbReader = new DbReader($filename);
$this->dbType = $this->dbReader->metadata()->databaseType;
$this->locales = $locales;
}
@@ -212,34 +214,36 @@ public function isp($ipAddress)
private function modelFor($class, $type, $ipAddress)
{
$record = $this->getRecord($class, $type, $ipAddress);
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
$record['traits']['ip_address'] = $ipAddress;
$record['traits']['prefix_len'] = $prefixLen;
$class = 'GeoIp2\\Model\\' . $class;
return new $class($record, $this->locales);
}
private function flatModelFor($class, $type, $ipAddress)
{
$record = $this->getRecord($class, $type, $ipAddress);
list($record, $prefixLen) = $this->getRecord($class, $type, $ipAddress);
$record['ip_address'] = $ipAddress;
$record['prefix_len'] = $prefixLen;
$class = 'GeoIp2\\Model\\' . $class;
return new $class($record);
}
private function getRecord($class, $type, $ipAddress)
{
if (strpos($this->metadata()->databaseType, $type) === false) {
if (strpos($this->dbType, $type) === false) {
$method = lcfirst($class);
throw new \BadMethodCallException(
"The $method method cannot be used to open a "
. $this->metadata()->databaseType . ' database'
"The $method method cannot be used to open a {$this->dbType} database"
);
}
$record = $this->dbReader->get($ipAddress);
list($record, $prefixLen) = $this->dbReader->getWithPrefixLen($ipAddress);
if ($record === null) {
throw new AddressNotFoundException(
"The address $ipAddress is not in the database."
@@ -259,7 +263,7 @@ private function getRecord($class, $type, $ipAddress)
);
}
return $record;
return [$record, $prefixLen];
}
/**
@@ -2,6 +2,8 @@
namespace GeoIp2\Model;
use GeoIp2\Util;
/**
* This class provides the GeoIP2 Anonymous IP model.
*
@@ -19,6 +21,9 @@
* exit node.
* @property-read string $ipAddress The IP address that the data in the model is
* for.
* @property-read string $network The network in CIDR notation associated with
* the record. In particular, this is the largest network where all of the
* fields besides $ipAddress have the same value.
*/
class AnonymousIp extends AbstractModel
{
@@ -28,6 +33,7 @@ class AnonymousIp extends AbstractModel
protected $isPublicProxy;
protected $isTorExitNode;
protected $ipAddress;
protected $network;
/**
* @ignore
@@ -43,6 +49,8 @@ public function __construct($raw)
$this->isHostingProvider = $this->get('is_hosting_provider');
$this->isPublicProxy = $this->get('is_public_proxy');
$this->isTorExitNode = $this->get('is_tor_exit_node');
$this->ipAddress = $this->get('ip_address');
$ipAddress = $this->get('ip_address');
$this->ipAddress = $ipAddress;
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
}
}
@@ -2,6 +2,8 @@
namespace GeoIp2\Model;
use GeoIp2\Util;
/**
* This class provides the GeoLite2 ASN model.
*
@@ -12,12 +14,16 @@
* address.
* @property-read string $ipAddress The IP address that the data in the model is
* for.
* @property-read string $network The network in CIDR notation associated with
* the record. In particular, this is the largest network where all of the
* fields besides $ipAddress have the same value.
*/
class Asn extends AbstractModel
{
protected $autonomousSystemNumber;
protected $autonomousSystemOrganization;
protected $ipAddress;
protected $network;
/**
* @ignore
@@ -30,6 +36,8 @@ public function __construct($raw)
$this->autonomousSystemNumber = $this->get('autonomous_system_number');
$this->autonomousSystemOrganization =
$this->get('autonomous_system_organization');
$this->ipAddress = $this->get('ip_address');
$ipAddress = $this->get('ip_address');
$this->ipAddress = $ipAddress;
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
}
}
@@ -2,6 +2,8 @@
namespace GeoIp2\Model;
use GeoIp2\Util;
/**
* This class provides the GeoIP2 Connection-Type model.
*
@@ -10,11 +12,15 @@
* Additional values may be added in the future.
* @property-read string $ipAddress The IP address that the data in the model is
* for.
* @property-read string $network The network in CIDR notation associated with
* the record. In particular, this is the largest network where all of the
* fields besides $ipAddress have the same value.
*/
class ConnectionType extends AbstractModel
{
protected $connectionType;
protected $ipAddress;
protected $network;
/**
* @ignore
@@ -26,6 +32,8 @@ public function __construct($raw)
parent::__construct($raw);
$this->connectionType = $this->get('connection_type');
$this->ipAddress = $this->get('ip_address');
$ipAddress = $this->get('ip_address');
$this->ipAddress = $ipAddress;
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
}
}
@@ -2,6 +2,8 @@
namespace GeoIp2\Model;
use GeoIp2\Util;
/**
* This class provides the GeoIP2 Domain model.
*
@@ -10,11 +12,15 @@
* "example.co.uk", not "foo.example.com".
* @property-read string $ipAddress The IP address that the data in the model is
* for.
* @property-read string $network The network in CIDR notation associated with
* the record. In particular, this is the largest network where all of the
* fields besides $ipAddress have the same value.
*/
class Domain extends AbstractModel
{
protected $domain;
protected $ipAddress;
protected $network;
/**
* @ignore
@@ -26,6 +32,8 @@ public function __construct($raw)
parent::__construct($raw);
$this->domain = $this->get('domain');
$this->ipAddress = $this->get('ip_address');
$ipAddress = $this->get('ip_address');
$this->ipAddress = $ipAddress;
$this->network = Util::cidr($ipAddress, $this->get('prefix_len'));
}
}

0 comments on commit 907ddd2

Please sign in to comment.
You can’t perform that action at this time.