blob: 57b01cfc6f10f3ac01241ad2d351b10878d7a11b (
plain)
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
|
<?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
$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 = filectime($root.'/'.$filename);
// TODO: weight by GeoIP or quality or something?
$mirrors = array();
foreach ($mirrors_urls as $mirrors_url) {
$json = json_decode(file_get_contents($mirrors_url), true);
foreach ($json['urls'] as $urldata) {
if (!in_array($urldata['protocol'], ['http', 'https', 'ftp'])) { continue; }
if (strtotime($urldata['last_sync']) < $ctime) { continue; }
$mirrors[] = $urldata['url'];
}
}
if (count($mirrors) > 0) {
header('X-Repomirror-Mirrors: '.implode(' ', $mirrors));
$mirror = $mirrors[array_rand($mirrors)];
}
}
header('Location: '.$mirror.$filename);
}
|