From 3888c34b03444bb4977ed44fb6f3fbdae0838387 Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Tue, 5 Jul 2022 18:05:33 +0500 Subject: [PATCH 01/13] added interface for handling IP retrieval --- src/iphandler/IPHandlerInterface.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/iphandler/IPHandlerInterface.php diff --git a/src/iphandler/IPHandlerInterface.php b/src/iphandler/IPHandlerInterface.php new file mode 100644 index 0000000..d6d3d5a --- /dev/null +++ b/src/iphandler/IPHandlerInterface.php @@ -0,0 +1,16 @@ + Date: Tue, 5 Jul 2022 18:54:37 +0500 Subject: [PATCH 02/13] set custom iphandler from config --- src/ipinfolaravel.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ipinfolaravel.php b/src/ipinfolaravel.php index 64d38b6..a06b523 100644 --- a/src/ipinfolaravel.php +++ b/src/ipinfolaravel.php @@ -83,6 +83,12 @@ public function configure() $this->settings['cache'] = new DefaultCache($maxsize, $ttl); } + if ($custom_iphandler = config('services.ipinfo.iphandler', null)) { + $this->settings['iphandler'] = $custom_iphandler; + } else { + // TODO: Handle this case, with default options + } + $this->ipinfo = new IPinfoClient($this->access_token, $this->settings); } From 6338fbe11031c690f3103945206e3fe101173c9c Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Thu, 7 Jul 2022 14:43:58 +0500 Subject: [PATCH 03/13] updated ip handler interface, added default implementation --- src/iphandler/DefaultIPHandler.php | 20 ++++++++++++++++++++ src/iphandler/IPHandlerInterface.php | 13 +++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/iphandler/DefaultIPHandler.php diff --git a/src/iphandler/DefaultIPHandler.php b/src/iphandler/DefaultIPHandler.php new file mode 100644 index 0000000..8df6f3a --- /dev/null +++ b/src/iphandler/DefaultIPHandler.php @@ -0,0 +1,20 @@ +ip(); + } +} diff --git a/src/iphandler/IPHandlerInterface.php b/src/iphandler/IPHandlerInterface.php index d6d3d5a..fe7fbf0 100644 --- a/src/iphandler/IPHandlerInterface.php +++ b/src/iphandler/IPHandlerInterface.php @@ -7,10 +7,11 @@ */ interface IPHandlerInterface { - - /** - * Get IP address. - * @return string IP address. - */ - public function getIP(); + + /** + * Get IP address. + * @param \Illuminate\Http\Request $request + * @return string IP address. + */ + public function getIP($request); } From a41851b9b86b90191f97ad6ebb0304a25b4096b2 Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Thu, 7 Jul 2022 14:45:41 +0500 Subject: [PATCH 04/13] receive ip handler implementation and use it for ip selection --- src/ipinfolaravel.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ipinfolaravel.php b/src/ipinfolaravel.php index a06b523..fdf3a6b 100644 --- a/src/ipinfolaravel.php +++ b/src/ipinfolaravel.php @@ -5,6 +5,7 @@ use Closure; use ipinfo\ipinfo\IPinfo as IPinfoClient; use ipinfo\ipinfolaravel\DefaultCache; +use ipinfo\ipinfolaravel\iphandler\DefaultIPHandler; class ipinfolaravel { @@ -26,6 +27,12 @@ class ipinfolaravel */ public $filter = null; + /** + * Provides ip. + * @var ipinfo\ipinfolaravel\iphandler\IPHandlerInterface + */ + public $ip_selector = null; + const CACHE_MAXSIZE = 4096; const CACHE_TTL = 60 * 24; @@ -43,7 +50,7 @@ public function handle($request, Closure $next) $details = null; } else { try { - $details = $this->ipinfo->getDetails($request->ip()); + $details = $this->ipinfo->getDetails($this->ip_selector->getIP($request)); } catch (\Exception $e) { $details = null; @@ -70,6 +77,7 @@ public function configure() $this->access_token = config('services.ipinfo.access_token', null); $this->filter = config('services.ipinfo.filter', [$this, 'defaultFilter']); $this->no_except = config('services.ipinfo.no_except', false); + $this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPHandler()); if ($custom_countries = config('services.ipinfo.countries_file', null)) { $this->settings['countries_file'] = $custom_countries; @@ -83,12 +91,6 @@ public function configure() $this->settings['cache'] = new DefaultCache($maxsize, $ttl); } - if ($custom_iphandler = config('services.ipinfo.iphandler', null)) { - $this->settings['iphandler'] = $custom_iphandler; - } else { - // TODO: Handle this case, with default options - } - $this->ipinfo = new IPinfoClient($this->access_token, $this->settings); } From 3c94f75e59e451e8073ea04db805341ee48152b6 Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Thu, 7 Jul 2022 21:49:18 +0500 Subject: [PATCH 05/13] added option for originating client ip, minor refactoring --- ...ultIPHandler.php => DefaultIPSelector.php} | 6 ++-- src/iphandler/IPHandlerInterface.php | 4 +-- src/iphandler/OriginatingIPSelector.php | 28 +++++++++++++++++++ src/ipinfolaravel.php | 4 +-- 4 files changed, 35 insertions(+), 7 deletions(-) rename src/iphandler/{DefaultIPHandler.php => DefaultIPSelector.php} (55%) create mode 100644 src/iphandler/OriginatingIPSelector.php diff --git a/src/iphandler/DefaultIPHandler.php b/src/iphandler/DefaultIPSelector.php similarity index 55% rename from src/iphandler/DefaultIPHandler.php rename to src/iphandler/DefaultIPSelector.php index 8df6f3a..b09772f 100644 --- a/src/iphandler/DefaultIPHandler.php +++ b/src/iphandler/DefaultIPSelector.php @@ -3,13 +3,13 @@ namespace ipinfo\ipinfolaravel\iphandler; /** - * Default implementation of the IPHandlerInterface. Selects default ip from request. + * Implementation of the IPHandlerInterface used as default option. Retrieve IP from request. */ -class DefaultIPHandler implements IPHandlerInterface +class DefaultIPSelector implements IPHandlerInterface { /** - * Selectes default IP address from request. + * Selects default IP address from request. * @param \Illuminate\Http\Request $request * @return string IP address. */ diff --git a/src/iphandler/IPHandlerInterface.php b/src/iphandler/IPHandlerInterface.php index fe7fbf0..f4a12e9 100644 --- a/src/iphandler/IPHandlerInterface.php +++ b/src/iphandler/IPHandlerInterface.php @@ -3,11 +3,11 @@ namespace ipinfo\ipinfolaravel\iphandler; /** - * Interface for handling the mechanism for IP retrieval. + * Interface for handling the mechanism of IP retrieval. */ interface IPHandlerInterface { - + /** * Get IP address. * @param \Illuminate\Http\Request $request diff --git a/src/iphandler/OriginatingIPSelector.php b/src/iphandler/OriginatingIPSelector.php new file mode 100644 index 0000000..95ce6bd --- /dev/null +++ b/src/iphandler/OriginatingIPSelector.php @@ -0,0 +1,28 @@ +headers->get('x-forwarded-for'); + if (empty($xForwardedFor)) { + $ip = $request->ip(); + } else { + $ips = explode(',', $xForwardedFor); + // trim as officially the space comes after each comma separator + $ip = trim($ips[0]); + } + return $ip; + } +} diff --git a/src/ipinfolaravel.php b/src/ipinfolaravel.php index fdf3a6b..47f3bf0 100644 --- a/src/ipinfolaravel.php +++ b/src/ipinfolaravel.php @@ -5,7 +5,7 @@ use Closure; use ipinfo\ipinfo\IPinfo as IPinfoClient; use ipinfo\ipinfolaravel\DefaultCache; -use ipinfo\ipinfolaravel\iphandler\DefaultIPHandler; +use ipinfo\ipinfolaravel\iphandler\DefaultIPSelector; class ipinfolaravel { @@ -77,7 +77,7 @@ public function configure() $this->access_token = config('services.ipinfo.access_token', null); $this->filter = config('services.ipinfo.filter', [$this, 'defaultFilter']); $this->no_except = config('services.ipinfo.no_except', false); - $this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPHandler()); + $this->ip_selector = config('services.ipinfo.ip_selector', new DefaultIPSelector()); if ($custom_countries = config('services.ipinfo.countries_file', null)) { $this->settings['countries_file'] = $custom_countries; From cb338aae75b7889cd225ade71d2cfbb66330cd57 Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Thu, 7 Jul 2022 22:29:48 +0500 Subject: [PATCH 06/13] updated readme for configuring ip selection handler feature --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index dcb6d4c..0a541da 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,43 @@ It is possible to use a custom cache by creating a child class of the [CacheInte ], ``` +### Choosing which IP to retrieve + +By default the ip from the request object is used. But the request may also be coming from a proxy server, so there is option provided to use different ip selectors. + +#### Using built-in ip selectors + +##### DefaultIPSelector + +Object of [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/DefaultIPSelector.php) class is used by default if no selector is provided. It returns the ip address from incoming request object. This address can be also of some proxy server forwarding the origninating client's request. This selector can be set explicitly by setting the `ip_selector` config value in `\config\services.php`. + +```php +'ipinfo' => [ + 'ip_selector' => new DefaultIPSelector(), +], +``` + +##### OriginatingIPSelector + +Object of [OriginatingIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/OriginatingIPSelector.php) class can be used as alternate to provide the originating client ip address. The originating client ip address is extracted from x-forwarded-for header. Keep in mind that this ip address is least trusted one. This selector can be set by setting the `ip_selector` config value in `\config\services.php`. + +```php +'ipinfo' => [ + 'ip_selector' => new OriginatingIPSelector(), +], +``` + +#### Using a different ip selector + +User may want to use custom techniques for getting an ip address out of the request according to their requirements. For this purpose custom ip selector can be created. This is done by implementing [IPHandlerInterface](https://github.com/ipinfo/php/blob/master/src/iphandler/IPHandlerInterface.php) and setting the the `ip_selector` config value in `\config\services.php`. + +```php +'ipinfo' => [ + ... + 'ip_selector' => new CustomIPSelector(), +], +``` + ### Internationalization When looking up an IP address, the response object includes a `$request->ipinfo->country_name` property which includes the country name based on American English. It is possible to return the country name in other languages by telling the library to read from a custom file. To define a custom file, add the following to your app's `\config\services.php` file and replace `{{countries}}` with your own file path. From 4c9d37b1241dc288cf36160db031fb6834e6065d Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Thu, 7 Jul 2022 22:33:24 +0500 Subject: [PATCH 07/13] corrected url link for php CacheInterface in readme for cache --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a541da..868f8ca 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ Default cache TTL and maximum size can be changed by setting values in the `$set #### Using a different cache -It is possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/php/blob/master/src/cache/Interface.php) class and setting the the `cache` config value in `\config\services.php`. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy). +It is possible to use a custom cache by creating a child class of the [CacheInterface](https://github.com/ipinfo/php/blob/master/src/cache/CacheInterface.php) class and setting the the `cache` config value in `\config\services.php`. FYI this is known as [the Strategy Pattern](https://sourcemaking.com/design_patterns/strategy). ```php 'ipinfo' => [ From 4b4d697bf211c584c89397a80117df01b3135814 Mon Sep 17 00:00:00 2001 From: Soban Mahmod Date: Wed, 13 Jul 2022 13:49:54 +0500 Subject: [PATCH 08/13] minor updates in readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 868f8ca..05ee5d3 100644 --- a/README.md +++ b/README.md @@ -141,15 +141,15 @@ It is possible to use a custom cache by creating a child class of the [CacheInte ], ``` -### Choosing which IP to retrieve +### IP Selection Mechanism -By default the ip from the request object is used. But the request may also be coming from a proxy server, so there is option provided to use different ip selectors. +By default, the ip from the incoming request is used. But the request may also be reaching through some proxy server, in which case the ip of that proxy server ended up being used. To handle this issue, option is provided to use different/custom ip selector. #### Using built-in ip selectors ##### DefaultIPSelector -Object of [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/DefaultIPSelector.php) class is used by default if no selector is provided. It returns the ip address from incoming request object. This address can be also of some proxy server forwarding the origninating client's request. This selector can be set explicitly by setting the `ip_selector` config value in `\config\services.php`. +Object of [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/DefaultIPSelector.php) class is used by default if no selector is provided. It returns the ip address from incoming request object. This address can also be of some proxy server forwarding the origninating client's request. This selector can be set explicitly by setting the `ip_selector` config value in `\config\services.php`. ```php 'ipinfo' => [ @@ -159,7 +159,7 @@ Object of [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/ipha ##### OriginatingIPSelector -Object of [OriginatingIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/OriginatingIPSelector.php) class can be used as alternate to provide the originating client ip address. The originating client ip address is extracted from x-forwarded-for header. Keep in mind that this ip address is least trusted one. This selector can be set by setting the `ip_selector` config value in `\config\services.php`. +Object of [OriginatingIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/OriginatingIPSelector.php) class can be used as alternate to provide the originating client ip address. The originating client ip address is extracted from x-forwarded-for header. Keep in mind that this ip address is least trusted one if proxies are involved. This selector can be set by setting the `ip_selector` config value in `\config\services.php`. ```php 'ipinfo' => [ From 7c67a5ad32e114c814edaaadbc2b46cf0ca095de Mon Sep 17 00:00:00 2001 From: Soban Mahmood Date: Mon, 18 Jul 2022 06:20:19 +0500 Subject: [PATCH 09/13] update ip selection description in readme Co-authored-by: Uman Shahzad --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 05ee5d3..fa12177 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,9 @@ It is possible to use a custom cache by creating a child class of the [CacheInte ### IP Selection Mechanism -By default, the ip from the incoming request is used. But the request may also be reaching through some proxy server, in which case the ip of that proxy server ended up being used. To handle this issue, option is provided to use different/custom ip selector. +By default, the IP from the incoming request is used. + +Since the desired IP by your system may be in other locations, the IP selection mechanism is configurable and some alternative built-in options are available. #### Using built-in ip selectors From c26409628ac764282f16bf1a50fac3177d652d14 Mon Sep 17 00:00:00 2001 From: Soban Mahmood Date: Mon, 18 Jul 2022 06:23:18 +0500 Subject: [PATCH 10/13] updated default ip selector description in README.md Co-authored-by: Uman Shahzad --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fa12177..acaeb53 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,9 @@ Since the desired IP by your system may be in other locations, the IP selection ##### DefaultIPSelector -Object of [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/DefaultIPSelector.php) class is used by default if no selector is provided. It returns the ip address from incoming request object. This address can also be of some proxy server forwarding the origninating client's request. This selector can be set explicitly by setting the `ip_selector` config value in `\config\services.php`. +A [DefaultIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/DefaultIPSelector.php) is used by default if no IP selector is provided. It returns the source IP from the incoming request. + +This selector can be set explicitly by setting the `ip_selector` config value in `\config\services.php`. ```php 'ipinfo' => [ From c7da29157c783602b6aa3e55d59c11e009fa34db Mon Sep 17 00:00:00 2001 From: Soban Mahmood Date: Mon, 18 Jul 2022 06:26:30 +0500 Subject: [PATCH 11/13] updated originating client ip selector description in README.md Co-authored-by: Uman Shahzad --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index acaeb53..5c95654 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,9 @@ This selector can be set explicitly by setting the `ip_selector` config value in ##### OriginatingIPSelector -Object of [OriginatingIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/OriginatingIPSelector.php) class can be used as alternate to provide the originating client ip address. The originating client ip address is extracted from x-forwarded-for header. Keep in mind that this ip address is least trusted one if proxies are involved. This selector can be set by setting the `ip_selector` config value in `\config\services.php`. +A [OriginatingIPSelector](https://github.com/ipinfo/php/blob/master/src/iphandler/OriginatingIPSelector.php) selects an IP address by trying to extract it from the `X-Forwarded-For` header. This is not always the most reliable unless your proxy setup allows you to trust it. It will default to the source IP on the request if the header doesn't exist. + +This selector can be set by setting the `ip_selector` config value in `\config\services.php`. ```php 'ipinfo' => [ From 58cde57251cacedef4d4688bf2a529c3f785c3c3 Mon Sep 17 00:00:00 2001 From: Soban Mahmood Date: Mon, 18 Jul 2022 06:27:01 +0500 Subject: [PATCH 12/13] updated custom ip heading in README.md Co-authored-by: Uman Shahzad --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c95654..0d309d6 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ This selector can be set by setting the `ip_selector` config value in `\config\s ], ``` -#### Using a different ip selector +#### Using a custom IP selector User may want to use custom techniques for getting an ip address out of the request according to their requirements. For this purpose custom ip selector can be created. This is done by implementing [IPHandlerInterface](https://github.com/ipinfo/php/blob/master/src/iphandler/IPHandlerInterface.php) and setting the the `ip_selector` config value in `\config\services.php`. From 29f5990181e49ef782e1f5c4850b2eae61755997 Mon Sep 17 00:00:00 2001 From: Soban Mahmood Date: Mon, 18 Jul 2022 06:28:21 +0500 Subject: [PATCH 13/13] updated custom ip selector description in README.md Co-authored-by: Uman Shahzad --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d309d6..2df74f0 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,9 @@ This selector can be set by setting the `ip_selector` config value in `\config\s #### Using a custom IP selector -User may want to use custom techniques for getting an ip address out of the request according to their requirements. For this purpose custom ip selector can be created. This is done by implementing [IPHandlerInterface](https://github.com/ipinfo/php/blob/master/src/iphandler/IPHandlerInterface.php) and setting the the `ip_selector` config value in `\config\services.php`. +In case a custom IP selector is required, you may implement the [IPHandlerInterface](https://github.com/ipinfo/php/blob/master/src/iphandler/IPHandlerInterface.php) interface and set the `ip_selector` config value in `\config\services.php`. + +For example: ```php 'ipinfo' => [