Skip to content

Commit

Permalink
Added new field "fraudScore" to support PX12 database.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Feb 5, 2025
1 parent d0cb91c commit 894b32d
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 102 deletions.
2 changes: 1 addition & 1 deletion LICENSE.TXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 - 2024 IP2Location.com
Copyright (c) 2016 - 2025 IP2Location.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ To learn more about installation, usage, and code examples, please visit the dev
| PUB | Public Proxies. These are services which make connection requests on a user's behalf. Proxy server software can be configured by the administrator to listen on some specified port. These differ from VPNs in that the proxies usually have limited functions compare to VPNs. | High |
| WEB | Web Proxies. These are web services which make web requests on a user's behalf. These differ from VPNs or Public Proxies in that they are simple web-based proxies rather than operating at the IP address and other ports level. | High |
| SES | Search Engine Robots. These are services which perform crawling or scraping to a website, such as, the search engine spider or bots engine. | Low |
| RES | Residential proxies. These services offer users proxy connections through residential ISP with or without consents of peers to share their idle resources. Only available with PX10 & PX11 | Medium |
| CPN | Consumer Privacy Network: These services ensure encrypted traffic from the user's browser by routing internet requests through relays, concealing the IP address, location, and browsing activity. Only available with PX11 | Low |
| EPN | Enterprise Private Network: Services like SASE or SD-WAN combine network security functions with wide-area networking (WAN) capabilities to meet the secure remote access needs of organizations. Only available with PX11 | Low |
| RES | Residential proxies. These services offer users proxy connections through residential ISP with or without consents of peers to share their idle resources. Only available with PX10 - PX12 | Medium |
| CPN | Consumer Privacy Network: These services ensure encrypted traffic from the user's browser by routing internet requests through relays, concealing the IP address, location, and browsing activity. Only available with PX11 & PX12 | Low |
| EPN | Enterprise Private Network: Services like SASE or SD-WAN combine network security functions with wide-area networking (WAN) capabilities to meet the secure remote access needs of organizations. Only available with PX11 & PX12 | Low |



Expand Down
Binary file removed data/PX11.SAMPLE.BIN
Binary file not shown.
Binary file added data/PX12.SAMPLE.BIN
Binary file not shown.
3 changes: 2 additions & 1 deletion docs/source/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Load the IP2Location BIN database.
```

```{py:function} getPackageVersion()
Return the database's type, 1 to 10 respectively for PX1 to PX11. Please visit https://www.ip2location.com/databases/ip2proxy for details.
Return the database's type, 1 to 10 respectively for PX1 to PX12. Please visit https://www.ip2location.com/databases/ip2proxy for details.
:return: Returns the package version.
:rtype: string
Expand Down Expand Up @@ -53,4 +53,5 @@ Retrieve geolocation information for an IP address.
| threat | Security threat reported. |
| proxyType | Type of proxy. |
| provider | Name of VPN provider if available. |
| fraudScore | Potential risk score (0 - 99) associated with IP address. |
```
5 changes: 3 additions & 2 deletions docs/source/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You can query the geolocation information from the IP2Proxy BIN database as belo
```php
require 'vendor/autoload.php';

$db = new \IP2Proxy\Database('vendor/ip2location/ip2proxy-php/data/PX11.SAMPLE.BIN', \IP2PROXY\Database::FILE_IO);
$db = new \IP2Proxy\Database('vendor/ip2location/ip2proxy-php/data/PX12.SAMPLE.BIN', \IP2PROXY\Database::FILE_IO);

$records = $db->lookup('1.0.0.8', \IP2PROXY\Database::ALL);

Expand All @@ -42,7 +42,7 @@ echo '<p><strong>State: </strong>' . $records['regionName'] . '</p>';
echo '<p><strong>City: </strong>' . $records['cityName'] . '</p>';

/*
Type of proxy: VPN, TOR, DCH, PUB, WEB, RES (RES available in PX11 only)
Type of proxy: VPN, TOR, DCH, PUB, WEB, RES (RES available in PX11 & PX12 only)
*/
echo '<p><strong>Proxy Type: </strong>' . $records['proxyType'] . '</p>';

Expand All @@ -61,4 +61,5 @@ echo '<p><strong>AS: </strong>' . $records['as'] . '</p>';
echo '<p><strong>Last Seen: </strong>' . $records['lastSeen'] . '</p>';
echo '<p><strong>Threat: </strong>' . $records['threat'] . '</p>';
echo '<p><strong>Provider: </strong>' . $records['provider'] . '</p>';
echo '<p><strong>Fraud Score: </strong>' . $records['fraudScore'] . '</p>';
```
6 changes: 3 additions & 3 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
require 'vendor/autoload.php';

// Lookup by local BIN database
$db = new \IP2Proxy\Database('./data/PX11.SAMPLE.BIN', \IP2PROXY\Database::FILE_IO);
$db = new \IP2Proxy\Database('./data/PX12.SAMPLE.BIN', \IP2PROXY\Database::FILE_IO);

echo 'Module Version: ' . $db->getModuleVersion() . \PHP_EOL . \PHP_EOL;
echo 'Package: PX' . $db->getPackageVersion() . \PHP_EOL;
echo 'Database Date: ' . $db->getDatabaseVersion() . \PHP_EOL;
echo '$records = $db->lookup(\'1.0.0.8\', \IP2PROXY\Database::ALL);' . \PHP_EOL;
$records = $db->lookup('1.0.0.8', \IP2PROXY\Database::ALL);
echo '$records = $db->lookup(\'23.90.12.224\', \IP2PROXY\Database::ALL);' . \PHP_EOL;
$records = $db->lookup('23.83.130.186', \IP2PROXY\Database::ALL);
print_r($records);

echo \PHP_EOL . \PHP_EOL;
Expand Down
129 changes: 86 additions & 43 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class Database
*/
public const PROVIDER = 14;

/**
* Fraud score.
*
* @var int
*/
public const FRAUD_SCORE = 15;

/**
* Country name and code.
*
Expand Down Expand Up @@ -198,7 +205,7 @@ class Database
*
* @var string
*/
private const VERSION = '4.1.1';
private const VERSION = '4.2.0';

/**
* Include the IP address of the looked up IP address.
Expand Down Expand Up @@ -333,20 +340,21 @@ class Database
* @var array
*/
private $columns = [
self::COUNTRY_CODE => [8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
self::COUNTRY_NAME => [8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
self::REGION_NAME => [0, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16],
self::CITY_NAME => [0, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20],
self::ISP => [0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24],
self::PROXY_TYPE => [0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
self::DOMAIN => [0, 0, 0, 0, 28, 28, 28, 28, 28, 28, 28],
self::USAGE_TYPE => [0, 0, 0, 0, 0, 32, 32, 32, 32, 32, 32],
self::ASN => [0, 0, 0, 0, 0, 0, 36, 36, 36, 36, 36],
self::_AS => [0, 0, 0, 0, 0, 0, 40, 40, 40, 40, 40],
self::LAST_SEEN => [0, 0, 0, 0, 0, 0, 0, 44, 44, 44, 44],
self::THREAT => [0, 0, 0, 0, 0, 0, 0, 0, 48, 48, 48],
self::PROVIDER => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52],
];
self::COUNTRY_CODE => [8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
self::COUNTRY_NAME => [8, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12],
self::REGION_NAME => [0, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
self::CITY_NAME => [0, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
self::ISP => [0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24],
self::PROXY_TYPE => [0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
self::DOMAIN => [0, 0, 0, 0, 28, 28, 28, 28, 28, 28, 28, 28],
self::USAGE_TYPE => [0, 0, 0, 0, 0, 32, 32, 32, 32, 32, 32, 32],
self::ASN => [0, 0, 0, 0, 0, 0, 36, 36, 36, 36, 36, 36],
self::_AS => [0, 0, 0, 0, 0, 0, 40, 40, 40, 40, 40, 40],
self::LAST_SEEN => [0, 0, 0, 0, 0, 0, 0, 44, 44, 44, 44, 44],
self::THREAT => [0, 0, 0, 0, 0, 0, 0, 0, 48, 48, 48, 48],
self::PROVIDER => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 52],
self::FRAUD_SCORE => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56],
];

/**
* Column name mapping.
Expand All @@ -368,6 +376,7 @@ class Database
self::LAST_SEEN => 'lastSeen',
self::THREAT => 'threat',
self::PROVIDER => 'provider',
self::FRAUD_SCORE => 'fraudScore',
self::IP_ADDRESS => 'ipAddress',
self::IP_VERSION => 'ipVersion',
self::IP_NUMBER => 'ipNumber',
Expand All @@ -380,6 +389,7 @@ class Database
*/
private $databases = [
// IPv4 databases
'IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER-FRAUDSCORE',
'IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER',
'IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL',
'IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT',
Expand All @@ -393,6 +403,7 @@ class Database
'IP2PROXY-IP-COUNTRY',

// IPv6 databases
'IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER-FRAUDSCORE',
'IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER',
'IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL',
'IPV6-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT',
Expand Down Expand Up @@ -670,19 +681,19 @@ public function close()
{
switch ($this->mode) {
case self::FILE_IO:
// free the file pointer
if ($this->resource !== false) {
fclose($this->resource);
$this->resource = false;
}
break;
// free the file pointer
if ($this->resource !== false) {
fclose($this->resource);
$this->resource = false;
}
break;
case self::SHARED_MEMORY:
// detach from the memory segment
if ($this->resource !== false) {
shmop_close($this->resource);
$this->resource = false;
}
break;
// detach from the memory segment
if ($this->resource !== false) {
shmop_close($this->resource);
$this->resource = false;
}
break;
}
}

Expand Down Expand Up @@ -766,6 +777,7 @@ public function lookup($ip, $fields = null, $asNamed = true)
$ifields[] = self::LAST_SEEN;
$ifields[] = self::THREAT;
$ifields[] = self::PROVIDER;
$ifields[] = self::FRAUD_SCORE;
$ifields[] = self::COUNTRY;
$ifields[] = self::IP_ADDRESS;
$ifields[] = self::IP_VERSION;
Expand All @@ -790,6 +802,7 @@ public function lookup($ip, $fields = null, $asNamed = true)
self::LAST_SEEN => false,
self::THREAT => false,
self::PROVIDER => false,
self::FRAUD_SCORE => false,
self::COUNTRY => false,
self::IP_ADDRESS => false,
self::IP_VERSION => false,
Expand Down Expand Up @@ -928,6 +941,13 @@ public function lookup($ip, $fields = null, $asNamed = true)
}
break;

case self::FRAUD_SCORE:
if (!$done[self::FRAUD_SCORE]) {
$results[self::FRAUD_SCORE] = $this->readFraudScore($pointer);
$done[self::FRAUD_SCORE] = true;
}
break;

case self::IP_ADDRESS:
if (!$done[self::IP_ADDRESS]) {
$results[self::IP_ADDRESS] = $ip;
Expand Down Expand Up @@ -1063,12 +1083,12 @@ private function getMemoryLimit()
} else {
// Deal with shorthand bytes
switch (strtoupper(substr($limit, -1))) {
case 'G': $value *= 1024;
// no break
case 'M': $value *= 1024;
// no break
case 'K': $value *= 1024;
}
case 'G': $value *= 1024;
// no break
case 'M': $value *= 1024;
// no break
case 'K': $value *= 1024;
}
}
$this->memoryLimit = $value;
}
Expand Down Expand Up @@ -1264,7 +1284,7 @@ private function bcBin2Dec($data)
unpack('V', substr($data, 8, 4)),
unpack('V', substr($data, 4, 4)),
unpack('V', substr($data, 0, 4)),
];
];

foreach ($parts as &$part) {
if ($part[1] < 0) {
Expand Down Expand Up @@ -1308,17 +1328,17 @@ private function expand($ipv6)
private function read($pos, $len)
{
switch ($this->mode) {
case self::SHARED_MEMORY:
return shmop_read($this->resource, $pos, $len);
case self::SHARED_MEMORY:
return shmop_read($this->resource, $pos, $len);

case self::MEMORY_CACHE:
return $data = substr($this->buffer[$this->resource], $pos, $len);
case self::MEMORY_CACHE:
return $data = substr($this->buffer[$this->resource], $pos, $len);

default:
fseek($this->resource, $pos, \SEEK_SET);
default:
fseek($this->resource, $pos, \SEEK_SET);

return fread($this->resource, $len);
}
return fread($this->resource, $len);
}
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1679,6 +1699,29 @@ private function readProvider($pointer)
return $provider;
}

/**
* High level function to fetch the Fraud score.
*
* @param int $pointer Position to read from, if false, return self::INVALID_IP_ADDRESS
*
* @return string
*/
private function readFraudScore($pointer)
{
if ($pointer === false) {
// Deal with invalid IPs
$fraudScore = self::INVALID_IP_ADDRESS;
} elseif ($this->columns[self::FRAUD_SCORE][$this->type] === 0) {
// If the field is not supported, return accordingly
$fraudScore = self::FIELD_NOT_SUPPORTED;
} else {
// Read the domain
$fraudScore = $this->readString($this->columns[self::FRAUD_SCORE][$this->type]);
}

return $fraudScore;
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Binary search and support functions /////////////////////////////////////////////////////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1769,7 +1812,7 @@ private function binSearch($version, $ipNumber)
case 1:
$low = $mid + 1;
break;
}
}
}

return false;
Expand Down
Loading

0 comments on commit 894b32d

Please sign in to comment.