Skip to content

Commit

Permalink
feat: use ipdata to get infos from ip (#5680)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Nov 6, 2021
1 parent a8f50b8 commit 339b0fe
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 5 deletions.
38 changes: 34 additions & 4 deletions app/Helpers/RequestHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Vectorface\Whip\Whip;
use Illuminate\Support\Arr;
use OK\Ipstack\Client as Ipstack;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Request;
use Stevebauman\Location\Facades\Location;

Expand Down Expand Up @@ -56,19 +57,48 @@ public static function infos($ip)
$ipstack = new Ipstack(config('location.ipstack_apikey'));
$position = $ipstack->get($ip, true);

if (! is_null($position) && Arr::get($position, 'country_code', null)) {
if (! is_null($position) && Arr::get($position, 'country_code')) {
return [
'country' => Arr::get($position, 'country_code', null),
'currency' => Arr::get($position, 'currency.code', null),
'timezone' => Arr::get($position, 'time_zone.id', null),
'country' => Arr::get($position, 'country_code'),
'currency' => Arr::get($position, 'currency.code'),
'timezone' => Arr::get($position, 'time_zone.id'),
];
}
}

if (config('location.ipdata.token') != null) {
try {
$position = static::getIpData($ip);

return [
'country' => Arr::get($position, 'country_code'),
'currency' => Arr::get($position, 'currency.code'),
'timezone' => Arr::get($position, 'time_zone.name'),
];
} catch (\Exception $e) {
// skip
}
}

return [
'country' => static::country($ip),
'currency' => null,
'timezone' => null,
];
}

/**
* Get data from ipdata.
*
* @param string $ip
* @return array
*/
private static function getIpData(string $ip): array
{
$token = config('location.ipdata.token', '');

$url = "https://api.ipdata.co/{$ip}?api-key=".$token;

return Http::get($url)->throw()->json();
}
}
50 changes: 49 additions & 1 deletion config/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@
Stevebauman\Location\Drivers\MaxMind::class,
],

/*
|--------------------------------------------------------------------------
| Position
|--------------------------------------------------------------------------
|
| Here you may configure the position instance that is created
| and returned from the above drivers. The instance you
| create must extend the built-in Position class.
|
*/
'position' => Stevebauman\Location\Position::class,

/*
|--------------------------------------------------------------------------
| MaxMind Configuration
Expand Down Expand Up @@ -66,6 +78,42 @@
],
],

/*
|--------------------------------------------------------------------------
| IP API Pro Configuration
|--------------------------------------------------------------------------
|
| The configuration for the IP API Pro driver.
|
*/
'ip_api' => [
'token' => env('IP_API_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| IPInfo Configuration
|--------------------------------------------------------------------------
|
| The configuration for the IPInfo driver.
|
*/
'ipinfo' => [
'token' => env('IPINFO_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| IPData Configuration
|--------------------------------------------------------------------------
|
| The configuration for the IPData driver.
|
*/
'ipdata' => [
'token' => env('IPDATA_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| Localhost Testing
Expand All @@ -78,7 +126,7 @@
|
*/
'testing' => [
'enabled' => false,
'enabled' => env('LOCATION_TESTING', false),
'ip' => '66.102.0.0',
],

Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/Helpers/ipdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"ip": "12.34.56.78",
"is_eu": true,
"city": "Paris",
"region": "\u00cele-de-France",
"region_code": "IDF",
"country_name": "France",
"country_code": "FR",
"continent_name": "Europe",
"continent_code": "EU",
"latitude": 0,
"longitude": 0,
"postal": "75000",
"calling_code": "33",
"flag": "https://ipdata.co/flags/fr.png",
"emoji_flag": "\ud83c\uddeb\ud83c\uddf7",
"emoji_unicode": "U+1F1EB U+1F1F7",
"languages": [
{
"name": "French",
"native": "Fran\u00e7ais"
}
],
"currency": {
"name": "Euro",
"code": "EUR",
"symbol": "\u20ac",
"native": "\u20ac",
"plural": "euros"
},
"time_zone": {
"name": "Europe/Paris",
"abbr": "CET",
"offset": "+0100",
"is_dst": false,
"current_time": "2021-11-01T00:00:00+01:00"
},
"threat": {
"is_tor": false,
"is_proxy": false,
"is_anonymous": false,
"is_known_attacker": false,
"is_known_abuser": false,
"is_threat": false,
"is_bogon": false
},
"count": "0"
}
41 changes: 41 additions & 0 deletions tests/Unit/Helpers/RequestHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Tests\TestCase;
use Mockery\MockInterface;
use App\Helpers\RequestHelper;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Request;
use Stevebauman\Location\Facades\Location;

Expand Down Expand Up @@ -62,4 +63,44 @@ public function get_country_from_ip()
RequestHelper::country(null)
);
}

/** @test */
public function get_infos_from_ip()
{
config(['location.ipdata.token' => 'test']);

$body = file_get_contents(base_path('tests/Fixtures/Helpers/ipdata.json'));
Http::fake([
'https://api.ipdata.co/*' => Http::response($body, 200),
]);

$this->assertEquals(
[
'country' => 'FR',
'currency' => 'EUR',
'timezone' => 'Europe/Paris',
],
RequestHelper::infos('test')
);
}

/** @test */
public function get_infos_from_ip_fail()
{
config(['location.ipdata.token' => 'test']);

Http::fake([
'https://api.ipdata.co/*' => Http::response(null, 500),
]);
Request::instance()->headers->set('Cf-Ipcountry', 'XX');

$this->assertEquals(
[
'country' => 'XX',
'currency' => null,
'timezone' => null,
],
RequestHelper::infos('test')
);
}
}

0 comments on commit 339b0fe

Please sign in to comment.