1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
<?php
# Copyright © 2016 Luke Shumaker <lukeshu@sbcglobal.net>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the ./COPYING file for more details.
// XXX: uWSGI doesn't set SCRIPT_NAME if php-app is set.
$_SERVER["SCRIPT_NAME"] = '/.fancyindex/mirror.php';
// Configuration
ini_set('default_socket_timeout', 2);
$tier0_url = 'https://repo.parabola.nu';
$mirrors_urls = [ 'https://www.parabola.nu/mirrors/status/tier/1/json'
//, 'https://www.parabola.nu/mirrors/status/tier/2/json'
];
function should_force_tier0($filename) {
return file_exists('/srv/repo/http/'.$filename) || (preg_match("/\.(db|files)(\.tar(\..z)?)?$/" , $filename) == 1);
}
// Automatic configuration
$assetdir = dirname($_SERVER["SCRIPT_FILENAME"]);
$root = $_SERVER["DOCUMENT_ROOT"];
$filename = explode("?", $_SERVER["REQUEST_URI"], 2)[0];
////////////////////////////////////////////////////////////////////////////////
if (!file_exists($root.'/'.$filename)) {
require $assetdir.'/404.php';
} elseif (is_dir($root.'/'.$filename)) {
if (substr("$filename", -1, 1) === "/") {
// Generate an index page
require $assetdir.'/index.php';
} else {
header('Location: '.$filename.'/');
}
} else {
$mirror = $tier0_url;
if (!should_force_tier0($filename)) {
$ctime = lstat($root.'/'.$filename)['ctime'];
header('X-Repomirror-C-Time: '.$ctime);
// TODO: weight by GeoIP or quality or something?
$mirrors = array();
foreach ($mirrors_urls as $mirrors_url) {
$str = file_get_contents($mirrors_url);
if ($str === false) {
error_log("REPOMIRROR Warning: read: ".$mirrors_url);
continue;
}
$json = json_decode($str, true);
if ($json === NULL) {
error_log("REPOMIRROR Warning: decode: ".$mirrors_url);
continue;
}
foreach ($json['urls'] as $urldata) {
if (!in_array($urldata['protocol'], ['http', 'https', 'ftp'])) { continue; }
if (strtotime($urldata['last_sync']) < $ctime) { continue; }
header('X-Repomirror-Mirror'.count($mirrors).': '.strtotime($urldata['last_sync']).' '.$urldata['url']);
$mirrors[] = $urldata['url'];
}
}
if (count($mirrors) > 0) {
$mirror = $mirrors[array_rand($mirrors)];
}
}
header('Location: '.$mirror.$filename);
}
|