Skip to content

Commit

Permalink
refactor: replaced tronovav/geoip2-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed May 10, 2024
1 parent 1171105 commit d20d80c
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 135 deletions.
68 changes: 0 additions & 68 deletions app/Console/Commands/GeoIPDownloadCommand.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/Logic/GeoIpLocatorImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function locate(string $ip): Country
private function retryLocate(string $ip, int $retries = 0): Country
{
try {
return $this->resolver->country($ip)->country;
return $this->resolver->city($ip)->country;
} catch (InvalidDatabaseException $exception) {
if ($retries > 2) {
Log::emergency('Cannot recover from invalid MaxMind DB.');
throw $exception;
}
Artisan::call('app:geoip:download');
Artisan::call('geoip:update');

return $this->retryLocate($ip, $retries + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Providers/GeoIpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GeoIpProvider extends ServiceProvider
public function register()
{
$this->app->singleton(Reader::class, function () {
return new Reader(storage_path('app/GeoLite2-Country/GeoLite2-Country.mmdb'));
return new Reader(storage_path('app/GeoLite2-City.mmdb'));
});

$this->app->singleton(GeoIpLocator::class, function (Container $app) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"laravel/tinker": "^v2.8",
"league/csv": "^9.10",
"opis/json-schema": "^2.3",
"tronovav/geoip2-update": "^v2.3"
"pulkitjalan/geoip": "^6.0"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^v2.13",
Expand Down
121 changes: 67 additions & 54 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 47 additions & 6 deletions config/geoip.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,59 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| GeoLite2 Token
| GeoIP Driver Type
|--------------------------------------------------------------------------
|
| Licence key needed to retrieve the lastest GeoLite2 Country database.
| To get yours, simply register to:
| https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
| Supported: "ip-api", "maxmind_database", "maxmind_api"
|
*/
'driver' => env('GEOIP_DRIVER', 'maxmind_database'),

/*
|--------------------------------------------------------------------------
| Return random ipaddresses (useful for dev envs)
|--------------------------------------------------------------------------
*/
'random' => env('GEOIP_RANDOM', false),

/*
|--------------------------------------------------------------------------
| IP-API Driver
|--------------------------------------------------------------------------
*/
'ip-api' => [
// Check out pro here: https://signup.ip-api.com/
'key' => env('GEOIP_IPAPI_KEY'),
'lang' => env('GEOIP_IPAPI_LANG'),
],

/*
|--------------------------------------------------------------------------
| Maxmind Database Driver
|--------------------------------------------------------------------------
*/
'maxmind_database' => [
// Example: app_path().'/database/maxmind/GeoLite2-City.mmdb'
// 'database' => base_path().'/'.env('GEOIP_MAXMIND_DATABASE', 'database/geoip/GeoLite2-City.mmdb'),
'database' => storage_path('app'). '/' . 'GeoLite2-City.mmdb',

'geoip_token' => env('GEOIP_TOKEN'),
// The license key is required for database updates
'license_key' => env('GEOIP_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| Maxmind Api Driver
|--------------------------------------------------------------------------
*/
'maxmind_api' => [
'user_id' => env('GEOIP_MAXMIND_USER_ID'),
'license_key' => env('GEOIP_MAXMIND_LICENSE_KEY'),
//maxmind api will default to paid service. Use 'geolite.info' for free GeoLite2 Web service. https://github.com/maxmind/GeoIP2-php#usage-1
'host' => env('GEOIP_MAXMIND_HOST'),
//local required on client call but defaults to 'en' for english.
'locales' => ['en'],
],
];
2 changes: 1 addition & 1 deletion containers/php/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if [ "$1" = 'php-fpm' ]; then
php artisan optimize
fi
php artisan migrate --force
php artisan app:geoip:download
php artisan geoip:update
chown -R www-data:www-data storage
elif [ "$1" = 'crond' ]; then
wait-for "${FPM_URL:?Missing FPM_URL}:${FPM_PORT:?Missing FPM_PORT}" -t 60
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/GeoIpLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

test('invalid database present', function () {
$reader = Mockery::mock(Reader::class, function (MockInterface $mock) {
$mock->shouldReceive('country')
$mock->shouldReceive('city')
->with('127.0.0.1')
->andThrow(new InvalidDatabaseException());
});

Artisan::partialMock()
->shouldReceive('call')
->times(3)
->with('app:geoip:download');
->with('geoip:update');

Log::partialMock()
->shouldReceive('emergency')
Expand Down

0 comments on commit d20d80c

Please sign in to comment.