first commit
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Download de DB-IP Country Lite database voor GeoIP lookups.
|
||||
* Dit is een gratis database met wekelijkse updates.
|
||||
*/
|
||||
|
||||
$outputDir = __DIR__ . '/../storage/geoip';
|
||||
|
||||
if (!is_dir($outputDir)) {
|
||||
mkdir($outputDir, 0755, true);
|
||||
}
|
||||
|
||||
$yearMonth = date('Y-m');
|
||||
$url = "https://download.db-ip.com/free/dbip-country-lite-{$yearMonth}.csv.gz";
|
||||
$gzFile = $outputDir . '/dbip-country-lite.csv.gz';
|
||||
$csvFile = $outputDir . '/dbip-country-lite.csv';
|
||||
|
||||
echo "Downloaden GeoIP database van {$url}...\n";
|
||||
|
||||
$fp = fopen($gzFile, 'w');
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_FILE => $fp,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_TIMEOUT => 120,
|
||||
CURLOPT_USERAGENT => 'SyslogDash/1.0',
|
||||
]);
|
||||
curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
fclose($fp);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
unlink($gzFile);
|
||||
echo "Fout: HTTP {$httpCode} bij downloaden GeoIP database\n";
|
||||
echo "Tip: bezoek https://www.db-ip.com/db/lite.php voor handmatige download\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
echo "Uitpakken...\n";
|
||||
$gz = gzopen($gzFile, 'r');
|
||||
$csv = fopen($csvFile, 'w');
|
||||
while ($line = gzgets($gz)) {
|
||||
fputs($csv, $line);
|
||||
}
|
||||
gzclose($gz);
|
||||
fclose($csv);
|
||||
unlink($gzFile);
|
||||
|
||||
// Convert to binary format for faster lookups
|
||||
$ipv4File = $outputDir . '/ipv4.bin';
|
||||
$ipv6File = $outputDir . '/ipv6.bin';
|
||||
|
||||
$fh4 = fopen($ipv4File, 'w');
|
||||
$fh6 = fopen($ipv6File, 'w');
|
||||
$count4 = 0;
|
||||
$count6 = 0;
|
||||
|
||||
$csv = fopen($csvFile, 'r');
|
||||
while (($row = fgetcsv($csv)) !== false) {
|
||||
if (count($row) < 3) continue;
|
||||
$start = trim($row[0]);
|
||||
$end = trim($row[1]);
|
||||
$code = trim($row[2]);
|
||||
if (strlen($code) !== 2) continue;
|
||||
|
||||
if (filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
||||
$startLong = ip2long($start);
|
||||
$endLong = ip2long($end);
|
||||
if ($startLong === false || $endLong === false) continue;
|
||||
fwrite($fh4, pack('NNa2', $startLong, $endLong, $code));
|
||||
$count4++;
|
||||
} elseif (filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
||||
$startBin = inet_pton($start);
|
||||
$endBin = inet_pton($end);
|
||||
if ($startBin === false || $endBin === false) continue;
|
||||
fwrite($fh6, $startBin . $endBin . $code);
|
||||
$count6++;
|
||||
}
|
||||
}
|
||||
fclose($csv);
|
||||
fclose($fh4);
|
||||
fclose($fh6);
|
||||
|
||||
unlink($csvFile);
|
||||
|
||||
echo "GeoIP database bijgewerkt:\n";
|
||||
echo " IPv4: {$count4} ranges\n";
|
||||
echo " IPv6: {$count6} ranges\n";
|
||||
echo " Bestanden: {$ipv4File}, {$ipv6File}\n";
|
||||
Reference in New Issue
Block a user