Skip to content

Commit

Permalink
dont crash on geoip and display flags where appropreate
Browse files Browse the repository at this point in the history
  • Loading branch information
antedebaas committed Oct 27, 2024
1 parent 65fa920 commit 921f308
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 128 deletions.
9 changes: 6 additions & 3 deletions config/packages/gpslab_geoip.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
gpslab_geoip:
license: '%env(MAXMIND_LICENCE)%'
edition: 'GeoLite2-Country'
path: '%kernel.project_dir%/var/data/GeoLite2-Country.mmdb'
databases:
default:
license: '%env(MAXMIND_LICENCE)%'
edition: 'GeoLite2-Country'
path: '%kernel.project_dir%/var/data/GeoLite2-Country.mmdb'
locales: ['en']
61 changes: 61 additions & 0 deletions src/Twig/Domain2Ip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

use Ante\DnsParser\Dns;
use GeoIp2\Database\Reader;

class Domain2Ip extends AbstractExtension
{
private $reader;

public function __construct(Reader $reader)
{
$this->reader = $reader;
}

public function getFilters(): array
{
return [
new TwigFilter('domain2ip', [$this, 'domain2ipFilter']),
];
}

public function domain2ipFilter($domain)
{
if (empty($domain)) {
return '';
}
$email = null;
if (is_array($domain)) {
$email = $domain[1];
$domain = $domain[0];
}

$dns = new Dns();
try {
$ipv4 = $dns->getRecords($domain, 'A')[0]->toArray()["ip"];
} catch (\Exception $e) {
$ipv4 = '';
}
try {
$ipv6 = $dns->getRecords($domain, 'AAAA')[0]->toArray()["ipv6"];
} catch (\Exception $e) {
$ipv6 = '';
}

if(!empty($ipv6)) {
$ip = $ipv6;
} else {
$ip = $ipv4;
}

if (!is_null($email)) {
return array($ip, $email);
} else {
return array($ip, $domain);
}
}
}
25 changes: 25 additions & 0 deletions src/Twig/Email2Domain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class Email2Domain extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('email2domain', [$this, 'email2domainFilter']),
];
}

public function email2domainFilter($email)
{
if (empty($email)) {
return '';
}
preg_match('/(\S*)?@(\S*)/', $email, $match);
$domain = $match[2];
return array($domain, $email);
}
}
65 changes: 65 additions & 0 deletions src/Twig/GeoIp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory;
use GeoIp2\Database\Reader;

class GeoIp extends AbstractExtension
{
private $reader;
private $cityDatabasePath = __DIR__ . '/../../var/GeoLite2/GeoLite2-City.mmdb';
private $countryDatabasePath = __DIR__ . '/../../var/GeoLite2/GeoLite2-Country.mmdb';


public function __construct(Reader $reader)
{
$this->reader = $reader;
}

public function getFunctions(): array
{
return [
new TwigFunction('geoip', [$this, 'geoipFunction'], ['is_safe' => ['html']]),
];
}

public function geoipFunction($type, $ip)
{

if (empty($ip)) {
return '';
}
$domain = null;
if (is_array($ip)) {
$domain = $ip[1];
$ip = $ip[0];
}

try {
$iso = strtolower($this->reader->country($ip)->country->isoCode);
} catch (\Exception $e) {
$iso = '';
}
try {
switch ($type) {
case 'country' || 'default':
$response = '<span class="flag flag-xxs flag-country-'.$iso.'"></span>';

if (!is_null($domain)) {
return $domain.' '.$response;
} else {
return $ip.' '.$response;
}

default:
return $ip;
}
}
catch (\Exception $e) {
return $ip;
}
}
}
47 changes: 0 additions & 47 deletions src/Twig/GeoIp_Country.php

This file was deleted.

18 changes: 9 additions & 9 deletions templates/dmarc_reports/report.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Reporting for: </b> </span>
<span class="float-end"> {{ report.domain.fqdn }}</span>
<span class="float-end">{{ geoip('country',report.domain.fqdn|domain2ip) }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Report ID: </b> </span>
<span class="float-end"> {{ report.externalid }}</span>
<span class="float-end">{{ report.externalid }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>E-Mail: </b> </span>
<span class="float-end"> {{ report.email }}</span>
<span class="float-end">{{ geoip('country',report.email|email2domain|domain2ip) }}</span>
</div>
</li>
<li class="list-group-item">
Expand All @@ -45,13 +45,13 @@
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Begin time: </b> </span>
<span class="float-end"> {{ report.begintime|date("d-M-Y H:i:s") }}</span>
<span class="float-end">{{ report.begintime|date("d-M-Y H:i:s") }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>End time: </b> </span>
<span class="float-end"> {{ report.endtime|date("d-M-Y H:i:s") }}</span>
<span class="float-end">{{ report.endtime|date("d-M-Y H:i:s") }}</span>
</div>
</li>
</ul>
Expand Down Expand Up @@ -170,25 +170,25 @@
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Envelope to: </b> </span>
<span class="float-end"> {{ record.envelopeto }}</span>
<span class="float-end">{{ geoip('country',record.envelopeto|domain2ip) }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Envelope from: </b> </span>
<span class="float-end"> {{ record.envelopefrom }}</span>
<span class="float-end">{{ geoip('country',record.envelopefrom|domain2ip) }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>Header from: </b> </span>
<span class="float-end"> {{ record.headerfrom }}</span>
<span class="float-end">{{ geoip('country',record.headerfrom|domain2ip) }}</span>
</div>
</li>
<li class="list-group-item">
<div class="clearfix">
<span class="float-start"><b>IP Address: </b> </span>
<span class="float-end">{{ record.sourceip|geoip_country }}</span>
<span class="float-end">{{ geoip('country',record.sourceip) }}</span>
</div>
</li>
</ul>
Expand Down
Loading

0 comments on commit 921f308

Please sign in to comment.