blob: 0031d990c987a3e7b3d75942a853cd635719e8df (
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
|
<?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_url = 'https://www.parabola.nu/mirrors/status/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)) {
// Generate an index page
require $assetdir.'/index.php';
} else {
$mirror = $tier0_url;
if (!should_force_tier0($filename)) {
$mtime = filemtime($root.'/'.$filename);
$json = json_decode(file_get_contents($mirrors_url), true);
// TODO: weight by geoip or something?
$mirrors = array();
foreach ($json['urls'] as $urldata) {
if (!in_array($urldata['protocol'], ['http', 'https', 'ftp'])) { continue; }
if (strtotime($urldata['last_sync']) < $mtime) { continue; }
array_push($mirrors, $urldata['url']);
}
if (count($mirrors) > 0) {
$mirror = $mirrors[array_rand($mirrors)];
}
}
header('Location: '.$mirror.$filename);
}
|