From 5c0ea670a3580f78ba23e322ef22ae7ba2c26975 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Sat, 25 Sep 2021 23:23:14 +0530
Subject: [PATCH 1/8] Initial Fastah API implementation
---
README.md | 2 +
data-sources/fastah.php | 246 +++++++++++++++++++++++++++++++++++++
geoip-detect.php | 1 +
readme.txt | 1 +
tests/test-datasources.php | 2 +-
views/options.php | 3 +
6 files changed, 254 insertions(+), 1 deletion(-)
create mode 100644 data-sources/fastah.php
diff --git a/README.md b/README.md
index 44f66185..0def70a1 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,8 @@ Provides geographic information detected by an IP adress. This can be used in th
* Commercial Web-API: [Maxmind GeoIP2 Precision](https://www.maxmind.com/en/geoip2-precision-services) (City, Country or Insights)
* Hosting-Provider dependent: [Cloudflare](https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do-) or [Amazon AWS CloudFront](https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/) (Country)
* Free or Commercial Web-API: [Ipstack](https://ipstack.com)
+ * Commercial Web-API via AWS Marketplace: [Fastah](https://aws.amazon.com/marketplace/pp/prodview-k5gjowexrefl2)
+
* Provides these 5 functions (see [API Documentation](https://github.com/yellowtree/geoip-detect/wiki/API:-PHP)):
* `geoip_detect2_get_info_from_ip($ip, $locales = array('en'), $options = array())`: Lookup Geo-Information of the specified IP
* `geoip_detect2_get_info_from_current_ip($locales = array('en'), $options = array())`: Lookup Geo-Information of the current website user
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
new file mode 100644
index 00000000..dc004ab9
--- /dev/null
+++ b/data-sources/fastah.php
@@ -0,0 +1,246 @@
+params= $params;
+ $this->params['language'] = reset($locales);
+ // TODO: Fastah API responses only support 'en' at this time
+ //if (empty($this->params['language'])) {
+ $this->params['language'] = 'en';
+ //}
+
+ $default_options = array(
+ 'timeout' => 1,
+ );
+ $this->options = $options + $default_options;
+ }
+
+ protected function locales($locale, $value) {
+ $locales = array('en' => $value);
+ if ($locale != 'en') {
+ $locales[$locale] = $value;
+ }
+ return $locales;
+ }
+
+ public function city($ip) {
+ $data = $this->api_call($ip);
+
+ if (!$data)
+ return _geoip_detect2_get_new_empty_record();
+
+ $r = array();
+
+ $r['extra']['original'] = $data;
+
+ // TODO: Check HTTP response code here.
+ if (isset($data['success']) && $data['success'] === false) {
+ throw new \RuntimeException($data['error']);
+ // Example error:
+ /* @see https://docs.getfastah.com/reference/getlocationbyip
+ {
+ "error": {
+ "message": "You did not specify a valid IP address."
+ }
+ }
+ */
+ }
+
+ $locale = $this->params['language'];
+ if (!empty($data['continent_name']))
+ $r['continent']['names'] = $this->locales($locale, $data['continent_name']);
+ if (!empty($data['continent_code']))
+ $r['continent']['code'] = strtoupper($data['continent_code']);
+ if (!empty($data['country_name']))
+ $r['country']['names'] = $this->locales($locale, $data['country_name']);
+ if (!empty($data['country_code']))
+ $r['country']['iso_code'] = strtoupper($data['country_code']);
+
+ if (!empty($data['region_code'])) {
+ $r['subdivisions'][0] = array(
+ 'iso_code' => $data['region_code'],
+ 'names' => $this->locales($locale, $data['region_name']),
+ );
+ }
+
+ if (!empty($data['city']))
+ $r['city']['names'] = $this->locales($locale, $data['city']);
+ if (!empty($data['latitude']))
+ $r['location']['latitude'] = $data['latitude'];
+ if (!empty($data['longitude']))
+ $r['location']['longitude'] = $data['longitude'];
+
+ if (!empty($data['location']['is_eu'])) {
+ $r['country']['is_in_european_union'] = $data['location']['is_eu'];
+ }
+ if (isset($data['timezone']['id']))
+ $r['location']['time_zone'] = $data['timezone']['id'];
+
+ if (isset($data['connection']['asn']))
+ $r['traits']['autonomous_system_number'] = $data['connection']['asn'];
+ if (isset($data['connection']['isp']))
+ $r['traits']['isp'] = $data['connection']['isp'];
+ if (isset($data['security']['is_proxy']))
+ $r['traits']['is_anonymous_vpn'] = $data['security']['is_proxy'] && $data['security']['proxy_type'] == 'vpn';
+ if (isset($data['security']['is_tor']))
+ $r['traits']['is_tor_exit_node'] = $data['security']['is_tor'];
+
+ if (!empty($data['location']['country_flag_emoji']))
+ $r['extra']['flag'] = strtoupper($data['location']['country_flag_emoji']);
+
+ if (!empty($data['currency']['code'])) {
+ $r['extra']['currency_code'] = $data['currency']['code'];
+ }
+
+
+ $r['traits']['ip_address'] = $ip;
+
+ $record = new \GeoIp2\Model\City($r, array('en'));
+
+ return $record;
+ }
+
+ public function country($ip) {
+ return $this->city($ip); // too much info shouldn't hurt ...
+ }
+
+ public function close() {
+
+ }
+
+ private function build_url($ip) {
+ $url = 'https';
+ $url .= '://' . self::URL . $ip;
+
+ $params = [
+ 'fastah-key' => $this->params['key'],
+ ];
+ return $url . '?' . \http_build_query($params);
+ }
+
+ private function api_call($ip) {
+
+ try {
+ // Setting timeout limit to speed up sites
+ $context = stream_context_create(
+ array(
+ 'http' => array(
+ 'protocol_version' => $this->params['http2'] ? 2.0 : 1.2,
+ 'timeout' => $this->options['timeout'],
+ ),
+ )
+ );
+ // Using @file... to supress errors
+ // Example output: {"country_name":"UNITED STATES","country_code":"US","city":"Aurora, TX","ip":"12.215.42.19"}
+
+ $body = @file_get_contents($this->build_url($ip), false, $context);
+ $data = json_decode($body, true);
+
+ return $data;
+ } catch (\Exception $e) {
+ // If the API isn't available, we have to do this
+ throw $e;
+ return null;
+ }
+ }
+}
+
+
+class FastahSource extends AbstractDataSource {
+ protected $params = array();
+
+ public function __construct() {
+ $this->params['key'] = get_option('geoip-detect-fastah_key', '');
+ $this->params['http2'] = get_option('geoip-detect-fastah_http2', 1);
+
+ }
+
+ public function getId() { return 'fastah'; }
+ public function getLabel() { return __('Fastah Web API', 'geoip-detect'); }
+
+ public function getDescriptionHTML() { return __('Fastah API via AWS Marketplace', 'geoip-detect'); }
+ public function getStatusInformationHTML() {
+ $html = '';
+
+ $html .= \sprintf(__('HTTP2: %s', 'geoip-detect'), $this->params['http2'] ? __('Enabled', 'geoip-detect') : __('Disabled', 'geoip-detect')) . '
';
+
+ if (!$this->isWorking())
+ $html .= '' . __('Fastah only works with an API key.', 'geoip-detect') . '
';
+
+ return $html;
+ }
+
+ public function getParameterHTML() {
+ $label_key = __('API Access Key:', 'geoip-detect');
+ $label_http2 = __('Use HTTP/2:', 'geoip-detect');
+
+ $key = esc_attr($this->params['key']);
+
+ $html = <<
+$label_http2 ';
+
+ return $html;
+ }
+
+ public function saveParameters($post) {
+ $message = '';
+
+ if (isset($post['options_fastah']['key'])) {
+ $key = sanitize_key($post['options_fastah']['key']);
+ update_option('geoip-detect-fastah_key', $key);
+ $this->params['key']= $key;
+ }
+
+ if (isset($post['options_fastah']['http2'])) {
+ $ssl = (int) $post['options_fastah']['http2'];
+ update_option('geoip-detect-fastah_http2', $http2);
+ $this->params['http2'] = $http2;
+ }
+
+ if (geoip_detect2_is_source_active('fastah') && !$this->isWorking())
+ $message .= __('Fastah only works with an API key.', 'geoip-detect');
+
+ return $message;
+ }
+
+ public function getReader($locales = array('en'), $options = array()) {
+ return new Reader($this->params, $locales, $options);
+ }
+
+ public function isWorking() {
+ return !empty($this->params['key']);
+ }
+
+}
+geoip_detect2_register_source(new FastahSource());
diff --git a/geoip-detect.php b/geoip-detect.php
index 228c767e..c8520bc5 100644
--- a/geoip-detect.php
+++ b/geoip-detect.php
@@ -92,6 +92,7 @@
include_once(GEOIP_PLUGIN_DIR . '/data-sources/precision.php');
include_once(GEOIP_PLUGIN_DIR . '/data-sources/header.php');
include_once(GEOIP_PLUGIN_DIR . '/data-sources/ipstack.php');
+include_once(GEOIP_PLUGIN_DIR . '/data-sources/fastah.php');
// You can define these constants in your theme/plugin if you like.
diff --git a/readme.txt b/readme.txt
index 0df94577..2b360c4c 100644
--- a/readme.txt
+++ b/readme.txt
@@ -22,6 +22,7 @@ Provides geographic information detected by an IP adress. This can be used in th
* Commercial Web-API: [Maxmind GeoIP2 Precision](https://www.maxmind.com/en/geoip2-precision-services) (City, Country or Insights)
* Hosting-Provider dependent: [Cloudflare](https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do-) or [Amazon AWS CloudFront](https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/) (Country)
* Free or Commercial Web-API: [Ipstack](https://ipstack.com)
+ * Commercial Web-API via AWS Marketplace: [Fastah](https://aws.amazon.com/marketplace/pp/prodview-k5gjowexrefl2)
* Provides these 5 functions (see [API Documentation](https://github.com/yellowtree/geoip-detect/wiki/API:-PHP)):
* `geoip_detect2_get_info_from_ip($ip, $locales = array('en'), $options = array())`: Lookup Geo-Information of the specified IP
* `geoip_detect2_get_info_from_current_ip($locales = array('en'), $options = array())`: Lookup Geo-Information of the current website user
diff --git a/tests/test-datasources.php b/tests/test-datasources.php
index dcc1405c..51314354 100644
--- a/tests/test-datasources.php
+++ b/tests/test-datasources.php
@@ -34,7 +34,7 @@ public function testPresenceOfAllDataSources() {
$source_ids = array_keys($sources);
sort($source_ids);
- $this->assertSame(array('auto', 'header', 'hostinfo', 'ipstack', 'manual', 'precision'), $source_ids);
+ $this->assertSame(array('auto', 'header', 'hostinfo', 'ipstack', 'fastah', 'manual', 'precision'), $source_ids);
}
public function testInvalidDatasources() {
diff --git a/views/options.php b/views/options.php
index 1f8fd99b..0c0c6a19 100644
--- a/views/options.php
+++ b/views/options.php
@@ -85,6 +85,9 @@
documentation for more infos). You should use a different data source or disable AJAX.', 'geoip-detect'), 'https://github.com/yellowtree/geoip-detect/wiki/JS-API-Documentation'); ?>
+
+ documentation for more infos).', 'geoip-detect'), 'https://github.com/yellowtree/geoip-detect/wiki/JS-API-Documentation'); ?>
+
From ae9b8c35993f3e3861c4897e02788096fac1551a Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Sun, 26 Sep 2021 16:37:08 +0530
Subject: [PATCH 2/8] Fastah API : typo fixes
---
data-sources/fastah.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index dc004ab9..92822f85 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -206,7 +206,7 @@ public function getParameterHTML() {
$label_key
$label_http2 ';
@@ -223,7 +223,7 @@ public function saveParameters($post) {
}
if (isset($post['options_fastah']['http2'])) {
- $ssl = (int) $post['options_fastah']['http2'];
+ $http2 = (int) $post['options_fastah']['http2'];
update_option('geoip-detect-fastah_http2', $http2);
$this->params['http2'] = $http2;
}
From 1bbdce191d69f28c13936837e68c5ad64101aef8 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Sun, 26 Sep 2021 21:55:09 +0530
Subject: [PATCH 3/8] Fastah : Better copyediting
---
data-sources/fastah.php | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index 92822f85..94d5fc64 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -182,9 +182,9 @@ public function __construct() {
}
public function getId() { return 'fastah'; }
- public function getLabel() { return __('Fastah Web API', 'geoip-detect'); }
+ public function getLabel() { return __('Fastah Web API beta', 'geoip-detect'); }
- public function getDescriptionHTML() { return __('Fastah API via AWS Marketplace', 'geoip-detect'); }
+ public function getDescriptionHTML() { return __('Commercial, with enterprise-friendly support and billing. Sign-up on AWS Marketplace. API Documentation', 'geoip-detect'); }
public function getStatusInformationHTML() {
$html = '';
@@ -197,16 +197,17 @@ public function getStatusInformationHTML() {
}
public function getParameterHTML() {
- $label_key = __('API Access Key:', 'geoip-detect');
- $label_http2 = __('Use HTTP/2:', 'geoip-detect');
+ $label_key = __('API Access Key :', 'geoip-detect');
+ $label_http2 = __('Use HTTP/2 :', 'geoip-detect');
$key = esc_attr($this->params['key']);
$html = <<
+$label_key
+Sign-up for a 30-day trial key, API usage dashboard
$label_http2 ';
From bc940543d902dff81b6172215e937de69bee21a8 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Wed, 29 Sep 2021 00:17:37 +0530
Subject: [PATCH 4/8] Fastah : Better HTTP/2 capability detection and messages
---
data-sources/fastah.php | 41 ++++++++++++++++++++++++++++++++---------
1 file changed, 32 insertions(+), 9 deletions(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index 94d5fc64..c8120ce6 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -26,7 +26,7 @@ class Reader implements \YellowTree\GeoipDetect\DataSources\ReaderInterface {
const URL = 'ep.api.getfastah.com/whereis/v1/json/';
protected $options = array();
protected $params = array();
-
+
function __construct($params, $locales, $options) {
$this->params= $params;
$this->params['language'] = reset($locales);
@@ -51,7 +51,6 @@ protected function locales($locale, $value) {
public function city($ip) {
$data = $this->api_call($ip);
-
if (!$data)
return _geoip_detect2_get_new_empty_record();
@@ -145,7 +144,7 @@ private function build_url($ip) {
}
private function api_call($ip) {
-
+
try {
// Setting timeout limit to speed up sites
$context = stream_context_create(
@@ -174,11 +173,25 @@ private function api_call($ip) {
class FastahSource extends AbstractDataSource {
protected $params = array();
+ // PHP-Curl with functional TLS 1.2 (secure ciphers) and HTTP 1.1 are minimum requirements
+ protected $bestAvailHTTP = CURL_HTTP_VERSION_1_1;
public function __construct() {
$this->params['key'] = get_option('geoip-detect-fastah_key', '');
- $this->params['http2'] = get_option('geoip-detect-fastah_http2', 1);
+ // Set default by probing PHP/Curl capabilities - minimum is HTTP 1.1 over TLSv1.2
+ // HTTP/2 ought to be available in PHP-Curl > v7.47.0 @see https://curl.se/docs/http2.html
+ if (curl_version()["features"] & CURL_HTTP_VERSION_2_0 !== 0) {
+ $this->bestAvailHTTP = CURL_HTTP_VERSION_2_0;
+ if (curl_version()["features"] & CURL_HTTP_VERSION_2TLS !== 0) {
+ $this->bestAvailHTTP = CURL_HTTP_VERSION_2TLS;
+ }
+ }
+ if ($this->bestAvailHTTP < CURL_HTTP_VERSION_2_0) {
+ $this->params['http2'] = get_option('geoip-detect-fastah_http2', 0);
+ } else {
+ $this->params['http2'] = get_option('geoip-detect-fastah_http2', 1);
+ }
}
public function getId() { return 'fastah'; }
@@ -190,8 +203,13 @@ public function getStatusInformationHTML() {
$html .= \sprintf(__('HTTP2: %s', 'geoip-detect'), $this->params['http2'] ? __('Enabled', 'geoip-detect') : __('Disabled', 'geoip-detect')) . '
';
- if (!$this->isWorking())
- $html .= '' . __('Fastah only works with an API key.', 'geoip-detect') . '
';
+ if (!$this->isWorking()) {
+ if (!is_callable('curl_init')) {
+ $html .= '' . __('Fastah requires PHP-Curl for secure HTTPS requests.', 'geoip-detect') . '
';
+ } else {
+ $html .= '' . __('Fastah only works with an API key.', 'geoip-detect') . '
';
+ }
+ }
return $html;
}
@@ -226,12 +244,17 @@ public function saveParameters($post) {
if (isset($post['options_fastah']['http2'])) {
$http2 = (int) $post['options_fastah']['http2'];
update_option('geoip-detect-fastah_http2', $http2);
+ // Show warning if HTTP/2 requested but actually not available via PHP-Curl
+ if ($http2 == 1 and $this->bestAvailHTTP < CURL_HTTP_VERSION_2_0) {
+ $message .= __('Fastah requires PHP-Curl with HTTP/2 support.
', 'geoip-detect');
+ $http2 = 0; // Turn it OFF forcefully
+ }
$this->params['http2'] = $http2;
}
if (geoip_detect2_is_source_active('fastah') && !$this->isWorking())
$message .= __('Fastah only works with an API key.', 'geoip-detect');
-
+
return $message;
}
@@ -239,8 +262,8 @@ public function getReader($locales = array('en'), $options = array()) {
return new Reader($this->params, $locales, $options);
}
- public function isWorking() {
- return !empty($this->params['key']);
+ public function isWorking() {
+ return (!empty($this->params['key']) and is_callable('curl_init'));
}
}
From 92b980ff01a10992cb18076c5acea5e45047c1c5 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Wed, 29 Sep 2021 13:56:56 +0530
Subject: [PATCH 5/8] Fastah : Fix fallback HTTP proto version
---
data-sources/fastah.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index c8120ce6..1ce3de1b 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -150,7 +150,7 @@ private function api_call($ip) {
$context = stream_context_create(
array(
'http' => array(
- 'protocol_version' => $this->params['http2'] ? 2.0 : 1.2,
+ 'protocol_version' => ($this->params['http2'] === 1) ? 2.0 : 1.1,
'timeout' => $this->options['timeout'],
),
)
From e8a21702ef941104aa38f4d91b15225e122b48e1 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Sun, 3 Oct 2021 03:29:17 +0530
Subject: [PATCH 6/8] Fastah: API response parsing and data fill
---
data-sources/fastah.php | 140 ++++++++++++++--------------------------
1 file changed, 48 insertions(+), 92 deletions(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index 1ce3de1b..b975b12b 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -50,75 +50,60 @@ protected function locales($locale, $value) {
}
public function city($ip) {
- $data = $this->api_call($ip);
- if (!$data)
- return _geoip_detect2_get_new_empty_record();
+ try {
+ $requestArgs = array(
+ 'method' => 'GET',
+ 'httpversion' => ($this->params['http2'] === 1) ? 2.0 : 1.1,
+ 'timeout' => $this->options['timeout'],
+ 'headers' => array(
+ 'Fastah-Key' => $this->params['key']
+ )
+ );
+ echo("Hello");
+ $response = wp_remote_get($this->build_url($ip), $requestArgs);
+ $respCode = wp_remote_retrieve_response_code( $response );
+ if (is_wp_error($response)) {
+ return _geoip_detect2_get_new_empty_record($ip, $response->get_error_message());
+ }
+ $body = wp_remote_retrieve_body( $response );
+ $data = json_decode( $body, true );
+ if ($respCode !== 200) {
+ if ($data && isset($data['error']) && isset($data['error']['message'])) {
+ return _geoip_detect2_get_new_empty_record($ip, $data['error']['message']);
+ }
+ if ($data && isset($data['message'])) {
+ return _geoip_detect2_get_new_empty_record($ip, $data['message']);
+ }
+ }
+ } catch (\Exception $e) {
+ return _geoip_detect2_get_new_empty_record($ip, $e->getMessage());
+ }
$r = array();
-
$r['extra']['original'] = $data;
- // TODO: Check HTTP response code here.
- if (isset($data['success']) && $data['success'] === false) {
- throw new \RuntimeException($data['error']);
- // Example error:
- /* @see https://docs.getfastah.com/reference/getlocationbyip
- {
- "error": {
- "message": "You did not specify a valid IP address."
- }
- }
- */
+ $locale = 'en'; // The REST API only support English at this time
+
+ if (!empty($data['locationData'])) {
+ // Continent
+ $r['continent']['code'] = strtoupper($data['locationData']['continentCode']);
+ // Country
+ $r['country']['names'] = $this->locales($locale, $data['locationData']['countryName']);
+ $r['country']['iso_code'] = strtoupper($data['locationData']['countryCode']);
+ // City
+ $r['city']['names'] = $this->locales($locale, $data['locationData']['cityName']);
+ $r['city']['geoname_id'] = $data['locationData']['cityGeonamesId'];
+ // Lat, Lng
+ $r['location']['latitude'] = $data['locationData']['lat'];
+ $r['location']['longitude'] = $data['locationData']['lng'];
+ // TZ
+ $r['location']['time_zone'] = $data['locationData']['tz'];
}
- $locale = $this->params['language'];
- if (!empty($data['continent_name']))
- $r['continent']['names'] = $this->locales($locale, $data['continent_name']);
- if (!empty($data['continent_code']))
- $r['continent']['code'] = strtoupper($data['continent_code']);
- if (!empty($data['country_name']))
- $r['country']['names'] = $this->locales($locale, $data['country_name']);
- if (!empty($data['country_code']))
- $r['country']['iso_code'] = strtoupper($data['country_code']);
-
- if (!empty($data['region_code'])) {
- $r['subdivisions'][0] = array(
- 'iso_code' => $data['region_code'],
- 'names' => $this->locales($locale, $data['region_name']),
- );
- }
+ // EU flag
+ $r['country']['isInEuropeanUnion'] = $data['isEuropeanUnion'];
- if (!empty($data['city']))
- $r['city']['names'] = $this->locales($locale, $data['city']);
- if (!empty($data['latitude']))
- $r['location']['latitude'] = $data['latitude'];
- if (!empty($data['longitude']))
- $r['location']['longitude'] = $data['longitude'];
-
- if (!empty($data['location']['is_eu'])) {
- $r['country']['is_in_european_union'] = $data['location']['is_eu'];
- }
- if (isset($data['timezone']['id']))
- $r['location']['time_zone'] = $data['timezone']['id'];
-
- if (isset($data['connection']['asn']))
- $r['traits']['autonomous_system_number'] = $data['connection']['asn'];
- if (isset($data['connection']['isp']))
- $r['traits']['isp'] = $data['connection']['isp'];
- if (isset($data['security']['is_proxy']))
- $r['traits']['is_anonymous_vpn'] = $data['security']['is_proxy'] && $data['security']['proxy_type'] == 'vpn';
- if (isset($data['security']['is_tor']))
- $r['traits']['is_tor_exit_node'] = $data['security']['is_tor'];
-
- if (!empty($data['location']['country_flag_emoji']))
- $r['extra']['flag'] = strtoupper($data['location']['country_flag_emoji']);
-
- if (!empty($data['currency']['code'])) {
- $r['extra']['currency_code'] = $data['currency']['code'];
- }
-
-
- $r['traits']['ip_address'] = $ip;
+ $r['traits']['ip_address'] = $data['ip'];
$record = new \GeoIp2\Model\City($r, array('en'));
@@ -126,7 +111,7 @@ public function city($ip) {
}
public function country($ip) {
- return $this->city($ip); // too much info shouldn't hurt ...
+ return $this->city($ip);
}
public function close() {
@@ -136,38 +121,9 @@ public function close() {
private function build_url($ip) {
$url = 'https';
$url .= '://' . self::URL . $ip;
-
- $params = [
- 'fastah-key' => $this->params['key'],
- ];
return $url . '?' . \http_build_query($params);
}
- private function api_call($ip) {
-
- try {
- // Setting timeout limit to speed up sites
- $context = stream_context_create(
- array(
- 'http' => array(
- 'protocol_version' => ($this->params['http2'] === 1) ? 2.0 : 1.1,
- 'timeout' => $this->options['timeout'],
- ),
- )
- );
- // Using @file... to supress errors
- // Example output: {"country_name":"UNITED STATES","country_code":"US","city":"Aurora, TX","ip":"12.215.42.19"}
-
- $body = @file_get_contents($this->build_url($ip), false, $context);
- $data = json_decode($body, true);
-
- return $data;
- } catch (\Exception $e) {
- // If the API isn't available, we have to do this
- throw $e;
- return null;
- }
- }
}
From a384153df1f5f0827c439804a2a51eb90fc13f65 Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Sun, 3 Oct 2021 03:39:02 +0530
Subject: [PATCH 7/8] Fastah: remove wayward echo
---
data-sources/fastah.php | 1 -
1 file changed, 1 deletion(-)
diff --git a/data-sources/fastah.php b/data-sources/fastah.php
index b975b12b..35e20213 100644
--- a/data-sources/fastah.php
+++ b/data-sources/fastah.php
@@ -59,7 +59,6 @@ public function city($ip) {
'Fastah-Key' => $this->params['key']
)
);
- echo("Hello");
$response = wp_remote_get($this->build_url($ip), $requestArgs);
$respCode = wp_remote_retrieve_response_code( $response );
if (is_wp_error($response)) {
From b6b2a1189eb924a7438fd00978db9d51ce768c2f Mon Sep 17 00:00:00 2001
From: Siddharth Mathur
Date: Wed, 6 Oct 2021 23:19:25 +0530
Subject: [PATCH 8/8] Fastah: fix test result expectation ordering
---
tests/test-datasources.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/tests/test-datasources.php b/tests/test-datasources.php
index 51314354..97462907 100644
--- a/tests/test-datasources.php
+++ b/tests/test-datasources.php
@@ -34,7 +34,7 @@ public function testPresenceOfAllDataSources() {
$source_ids = array_keys($sources);
sort($source_ids);
- $this->assertSame(array('auto', 'header', 'hostinfo', 'ipstack', 'fastah', 'manual', 'precision'), $source_ids);
+ $this->assertSame(array('auto', 'fastah', 'header','hostinfo', 'ipstack', 'manual', 'precision'), $source_ids);
}
public function testInvalidDatasources() {