Skip to content

Instantly share code, notes, and snippets.

@p182
Last active December 19, 2023 20:54
Show Gist options
  • Save p182/d4129dc2a49a65bfb8aa1d1938c0f5ac to your computer and use it in GitHub Desktop.
Save p182/d4129dc2a49a65bfb8aa1d1938c0f5ac to your computer and use it in GitHub Desktop.
Generate advanced apt mirror.txt file for sources.list, supports different mirrorlist sources (from different locations, etc.) and host/path filtering
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
$locations = ['mirrors', 'SE', 'FI']; //locations to download lists for, mirrors is for local mirrors.txt by GeoIP, not always fastest servers
$substrings = ["truenetwork.ru", "linux-ia64.org", "powernet.com.ru", "timeweb.ru", "yandex", "hyperdedic.ru"]; //substrings of mirror URLs to exclude
$filteredLines = ["http://mirrors.linode.com/ubuntu/".PHP_EOL,"http://mirrors.digitalocean.com/ubuntu/".PHP_EOL]; //array definition to add mirrors to output
foreach ($locations as $location){
$url = 'http://mirrors.ubuntu.com/'.$location.'.txt';
$cache_file = '/tmp/mirrors_'.$location;
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 60 * 24 ))) {
// Cache file is less than 24 hours old.
// Don't bother refreshing, just use the file as-is.
$file = file($cache_file);
} else {
// Our cache is out-of-date, so load the data from our remote server,
// and also save it over our cache for next time.
$file = file($url);
if($file){//Do not cache empty file (e.g. download error)
file_put_contents($cache_file, $file, LOCK_EX);}
}
foreach($file as $line){
// Check if the line contains any of the substrings
$containsSubstring = false;
foreach ($substrings as $substring) {
if (strpos($line, $substring) !== false) {
$containsSubstring = true;
break;
}
}
// If the line contains any substring, skip it
if ($containsSubstring) {
continue;
}
//Replace https with http for better performance or http with https for better security
$line = str_replace('https://', 'http://', $line);
// Add the line to the filtered array
$filteredLines[] = $line;
}
}
echo implode('', $filteredLines);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment