Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Created May 6, 2020 20:18
Show Gist options
  • Save ArrayIterator/91134c3f160e449fd8012f0eadd13e58 to your computer and use it in GitHub Desktop.
Save ArrayIterator/91134c3f160e449fd8012f0eadd13e58 to your computer and use it in GitHub Desktop.
Maxmind GeoIP Updater (Based PHP)
#!/usr/bin/env php
<?php
/**
* GeoIP Legacy Download & Updated from https://www.miyuru.lk/geoiplegacy
* Change $path or put on /usr/share/GeoIP
* This script will replace GeoIP.dat, GeoIPCity.dat & GeoIPASNum.dat
* NGINX CONFIG:
http {
.... // etc
geoip_country /usr/share/GeoIP/GeoIP.dat;
geoip_org /usr/share/GeoIP/GeoIPASNum.dat;
geoip_city /usr/share/GeoIP/GeoIPCity.dat;
.... // etc
}
*/
if (php_sapi_name() !== 'cli') {
echo "Application must be run as CLI mode\n";
exit(255);
}
$path = '/usr/share/GeoIP';
if (!is_dir($path)) {
printf("Path /usr/share/GeoIP is not exists\n", $path);
exit(2555);
}
if (!is_writable($path)) {
printf("Path /usr/share/GeoIP is not writable\n", $path);
exit(2555);
}
$path .= '/';
$files = [
'GeoIP.dat' => 'https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz',
'GeoIPASNum.dat' => 'https://dl.miyuru.lk/geoip/maxmind/asn/maxmind.dat.gz',
'GeoIPCity.dat' => 'https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz',
];
$isRoot = get_current_user() === 'root';
$nginx = @shell_exec('which nginx');
if ($isRoot) {
echo "----------------------------------------\nUser Role is Root\n----------------------------------------\n";
}
foreach ($files as $key => $item) {
$file = $path . $key;
if (file_exists($file) && !is_writable($file)) {
printf("[X] File %s is not writable\n", $file);
continue;
}
$socket = @gzopen($item, 'rb');
if (!$socket) {
}
$metadata = @stream_get_meta_data($socket);
$type = $metadata['stream_type']??null;
if ($type !== 'ZLIB' || !($metadata['seekable']??null)) {
printf("[X] Stream for %s is not ZLIB data\n", $key);
@gzclose($socket);
continue;
}
printf("[+] Getting & Writing %s GEOIP Data", $key);
$stream = @fopen($file, 'wb');
if (!$stream) {
printf("[X] Could not create socket stream for %s\n", $file);
@gzclose($socket);
continue;
}
$written = 0;
while (!gzeof($socket)) {
$written += fwrite($stream, gzread($socket, 4096));
}
printf(" [OK] %d bytes\n", $key, $written);
is_resource($stream) && fclose($stream);
is_resource($socket) && gzclose($socket);
}
// RELOAD NGINX
if ($isRoot && $nginx && file_exists(trim($nginx))) {
$nginx = trim($nginx);
$version = trim((string)@shell_exec(sprintf('%s -v 2>&1', $nginx)));
printf("[*] RELOADING NGINX: %s\n", $version);
@system(sprintf("%s -s reload", $nginx));
}
gc_mem_caches();
__halt_compiler();
// end here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment