summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-21 22:48:04 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-21 22:48:04 -0400
commit3517c1aac1e844f91779e844b8030ad7b11e05d1 (patch)
treefc511745f9600dd50468f9e4320e83c8010673cf
parent6ced32a51269303343a6d3e803dc299eff39781d (diff)
add an automatic mirror selection script
-rw-r--r--404.php12
-rw-r--r--index.php22
-rw-r--r--mirror.php46
3 files changed, 62 insertions, 18 deletions
diff --git a/404.php b/404.php
new file mode 100644
index 0000000..aef4032
--- /dev/null
+++ b/404.php
@@ -0,0 +1,12 @@
+<?php
+// This is the template used by Nginx internally;
+// if you want something else, have Nginx intercept it.
+// Separation of concerns.
+header("HTTP/1.0 404 Not Found");
+?><html>
+<head><title>404 Not Found</title></head>
+<body bgcolor="white">
+<center><h1>404 Not Found</h1></center>
+<hr><center><?php echo $_SERVER["SERVER_SOFTWARE"]; ?></center>
+</body>
+</html>
diff --git a/index.php b/index.php
index 694931a..5a09cd6 100644
--- a/index.php
+++ b/index.php
@@ -13,26 +13,13 @@ $repos_para_project = [ 'libre', 'libre-testing',
$repos_para_community = [ 'cross', 'java', 'kernels', 'nonprism', 'pcr' ];
// Automatic configuration
-$assetdir = dirname($_SERVER["SCRIPT_NAME"]);
+$asseturl = dirname($_SERVER["SCRIPT_NAME"]);
+$assetdir = dirname($_SERVER["SCRIPT_FILENAME"]);
$root = $_SERVER["DOCUMENT_ROOT"];
$dirname = explode("?", $_SERVER["REQUEST_URI"], 2)[0];
////////////////////////////////////////////////////////////////////////////////
-function show_404() {
- // This is the template used by Nginx internally;
- // if you want something else, have Nginx intercept it.
- // Separation of concerns.
- ?><html>
-<head><title>404 Not Found</title></head>
-<body bgcolor="white">
-<center><h1>404 Not Found</h1></center>
-<hr><center><?php echo $_SERVER["SERVER_SOFTWARE"]; ?></center>
-</body>
-</html>
-<?php
-}
-
function normalizeN($filename) {
$parts = preg_split("|/+|", $filename, -1, PREG_SPLIT_NO_EMPTY);
$abs = substr($filename, 0, 1) === '/';
@@ -96,8 +83,7 @@ if (!is_dir($root.'/'.$dirname)) {
header('Content-Type: text/plain'); // gross, but if Nginx is "properly" configured, this only happens when serving itself, which is text
readfile($root.'/'.$dirname);
} else {
- header("HTTP/1.0 404 Not Found");
- show_404();
+ require $assetdir.'/404.php';
}
exit(0);
}
@@ -109,7 +95,7 @@ if (!is_dir($root.'/'.$dirname)) {
<head>
<meta charset="utf-8" />
<title>Parabola GNU/Linux-libre - Index of <?php echo htmlentities($dirname); ?></title>
- <link rel="stylesheet" type="text/css" href="<?php echo $assetdir."/style.css"; ?>" />
+ <link rel="stylesheet" type="text/css" href="<?php echo $asseturl."/style.css"; ?>" />
<link rel="icon" type="image/x-icon" href="https://www.parabola.nu/static/favicon.72ab042ac877.ico" />
<link rel="shortcut icon" type="image/x-icon" href="https://www.parabola.nu/static/favicon.72ab042ac877.ico" />
<link rel="apple-touch-icon" href="https://www.parabola.nu/static/logos/apple-touch-icon-57x57.6484c8e17e2f.png" />
diff --git a/mirror.php b/mirror.php
new file mode 100644
index 0000000..0031d99
--- /dev/null
+++ b/mirror.php
@@ -0,0 +1,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);
+}