Skip to content

Commit

Permalink
2.33.1 Updated GeoIP interface to allow for automated download of new…
Browse files Browse the repository at this point in the history
… database
  • Loading branch information
Martin Francis committed Feb 7, 2022
1 parent db08d95 commit 5da3073
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 128 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -13,7 +13,7 @@
"ext-posix": "*",
"beberlei/doctrineextensions": "^1.2.0",
"doctrine/doctrine-bundle": "^1.9",
"gpslab/geoip2": "^1.1.6",
"gpslab/geoip2": "^2.0",
"sensio/framework-extra-bundle": "^5.2.4",
"symfony/apache-pack": "^1.0",
"symfony/asset": "^4.1",
Expand Down
83 changes: 22 additions & 61 deletions composer.lock

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

14 changes: 2 additions & 12 deletions config/services.yaml
Expand Up @@ -28,15 +28,5 @@ services:
# please note that last definitions always *replace* previous ones

gpslab_geoip:
# Path to download GeoIP database.
# It's a default value. You can change it.
# cache: '%kernel.cache_dir%/GeoLite2-City.mmdb'
cache: '/usr/share/GeoIP/GeoLite2-City.mmdb'

# URL for download new GeoIP database.
# It's a default value. You can change it.
url: 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz'

# Get model data in this locale
# It's a default value. You can change it.
locales: [ '%locale%' ]
license: 'UFzmNoddqV9v4upp'
edition: 'GeoLite2-City'
17 changes: 14 additions & 3 deletions public/css/style.css
@@ -1,8 +1,8 @@
/*
* Project: RXX - NDB Logging Database
* Homepage: https://rxx.classaxe.com
* Version: 2.31.2
* Date: 2022-01-05
* Version: 2.33.0
* Date: 2022-02-07
* Licence: LGPL
* Copyright: 2022 Martin Francis
*/
Expand Down Expand Up @@ -198,7 +198,7 @@ body.rww .button.is-inactive {
}
.inactive,
tr.inactive td {
color: #a0a0a0;
color: #808080;
font-style: italic;
}
.inactive a,
Expand Down Expand Up @@ -827,6 +827,17 @@ table.responsive tbody tr td b {
padding: 0.125em 0.25em;
margin: 0 0 0 0.5em;
}
.admin_tools .ip-examples {
font-size: 80%;
margin: 0 0 0 3em;
}
.admin_tools .ip-examples span {
color: #0000ff;
}
.admin_tools .ip-examples span:hover {
text-decoration: underline;
cursor: pointer;
}
.changelog {
margin: 0 0 0.5em 5em;
}
Expand Down
2 changes: 1 addition & 1 deletion public/css/style.min.css

Large diffs are not rendered by default.

77 changes: 31 additions & 46 deletions src/Service/GeoService.php
Expand Up @@ -11,8 +11,9 @@
use App\Utils\Rxx;
use Exception;
use GeoIp2\Exception\AddressNotFoundException;
use GeoIp2\Database\Reader;
use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
use MaxMind\Db\Reader\InvalidDatabaseException;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Class GeoService
Expand All @@ -24,16 +25,28 @@ class GeoService
* @var Visitor
*/
private $visitor;

private $dbPath = '/usr/share/GeoIP/GeoLite2-City.mmdb';
private $reader;
private $dbPath;

/**
* GeoService constructor.
* @param \App\Service\Visitor $visitor
* @param ReaderFactory
*/
public function __construct(Visitor $visitor)
public function __construct(KernelInterface $kernel, Visitor $visitor, ReaderFactory $factory)
{
$this->visitor = $visitor;
$this->dbPath = $kernel->getCacheDir() . '/GeoLite2-City.mmdb';
try {
$this->reader = $factory->create('default');
} catch (Exception $e) {
$binPath = substr(dirname(__DIR__), 0, -4) . "/bin/";
die(
"<h1>GeoIP Error</h1>\n"
. "Please run this command to download the latest GeoIP Database:</p>"
. "<pre>{$binPath}console geoip2:update</pre>"
);
}
}

/**
Expand All @@ -46,24 +59,7 @@ public function getContinent()
return 'NA';
}
try {
$reader = new Reader($this->dbPath);
} catch (Exception $e) {
$user = posix_getpwuid(posix_geteuid());
$uName = $user['name'];
$group = posix_getgrgid($user['gid']);
$gName = $group['name'];
$binPath = substr(dirname(__DIR__), 0, -4)."/bin/";
die(
"<h1>GeoIP Error</h1>\n"
."<p>{$this->dbPath} is missing.<br />\n"
."Please run these commands:</p>"
."<pre>sudo mkdir -p ".dirname($this->dbPath).";\n"
."sudo chown $uName:$gName ".dirname($this->dbPath).";\n"
."{$binPath}console geoip2:update</pre>"
);
}
try {
$record = $reader->city($ip);
$record = $this->reader->city($ip);
return $record->continent->code;
} catch (AddressNotFoundException $e) {
return 'NA';
Expand All @@ -74,32 +70,15 @@ public function getContinent()

/**
* @param $ip
* @return \GeoIp2\Model\City|string|void
* @return array
*/
public function getDetailsForIp($ip)
{
if (!$ip) {
return 'NA';
}
try {
$reader = new Reader($this->dbPath);
} catch (Exception $e) {
$user = posix_getpwuid(posix_geteuid());
$uName = $user['name'];
$group = posix_getgrgid($user['gid']);
$gName = $group['name'];
$binPath = substr(dirname(__DIR__), 0, -4)."/bin/";
die(
"<h1>GeoIP Error</h1>\n"
."<p>{$this->dbPath} is missing.<br />\n"
."Please run these commands:</p>"
."<pre>sudo mkdir -p ".dirname($this->dbPath).";\n"
."sudo chown $uName:$gName ".dirname($this->dbPath).";\n"
."{$binPath}console geoip2:update</pre>"
);
return [ 'Result' => 'No IP address given' ];
}
try {
$record = $reader->city($ip);
$record = $this->reader->city($ip);
return [
'IP' => $ip,
'City' => $record->city->name,
Expand All @@ -112,22 +91,28 @@ public function getDetailsForIp($ip)
'GSQ' => Rxx::convertDegreesToGSQ($record->location->latitude, $record->location->longitude),
'System' => strtoupper($this->getDefaultSystem()),
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d h:i:s', filemtime($this->dbPath))
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (AddressNotFoundException $e) {
return [
'IP' => $ip,
'Result' => 'Address not found'
'Result' => 'Address not found',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (InvalidDatabaseException $e) {
return [
'IP' => $ip,
'Result' => 'Invalid Database'
'Result' => 'Invalid Database',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d H:i:s', filemtime($this->dbPath))
];
} catch (Exception $e) {
return [
'IP' => $ip,
'Result' => 'Invalid Request'
'Result' => 'Invalid Request',
'GeoIP2DB' => $this->dbPath,
'GeoIP2Age' => date('Y-m-d h:i:s', filemtime($this->dbPath))
];
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/css/include/admin/tools.less
Expand Up @@ -23,4 +23,15 @@
}
}
}
.ip-examples {
font-size: 80%;
margin: 0 0 0 3em;
span {
color: #0000ff;
&:hover {
text-decoration: underline;
cursor: pointer;
}
}
}
}
3 changes: 0 additions & 3 deletions symfony.lock
Expand Up @@ -83,9 +83,6 @@
"geoip2/geoip2": {
"version": "v2.9.0"
},
"gpslab/compressor": {
"version": "v1.0.0"
},
"gpslab/geoip2": {
"version": "v1.1.1"
},
Expand Down
13 changes: 12 additions & 1 deletion templates/admin/tools/index.html.twig
Expand Up @@ -66,7 +66,14 @@
</li>
<li><a class="button geoip" href="{{ url('admin/tools', {'system' : system, 'tool' : 'systemGeoIpTest'}) }}">{% trans %}Go{% endtrans %}</a>
<strong>{% trans %}Check location info for IP Address:{% endtrans %}</strong>
<label class="sr-only" for="ip">IP Address</label><input type="text" name="ip" id="ip" value="{{ ip }}"/>
<label class="sr-only" for="ip">IP Address</label><input type="text" name="ip" id="ip" value="{{ ip }}"/><br />
<span class="ip-examples">Examples: [
<span data-ip="206.248.171.206">Toronto, Canada</span> |
<span data-ip="72.130.194.78">Hawaii, USA</span> |
<span data-ip="213.219.36.56">London, UK</span> |
<span data-ip="95.31.18.119">Moscow, Russia</span> |
<span data-ip="202.86.32.122">Brisbane, Australia</span>
]</span>
</li>
</ul>
</li>
Expand Down Expand Up @@ -106,6 +113,10 @@
}
buttons.attr('disabled', 'disabled');
});
$('.ip-examples span').on('click', function(e){
$('#ip').val($(this).data('ip'));
$('.admin_tools a.geoip').trigger('click');
})
});
</script>
{% endblock %}

0 comments on commit 5da3073

Please sign in to comment.