This commit is contained in:
2022-01-31 01:27:43 +08:00
parent 2ef9f0ecef
commit 9f7eaeb771
3 changed files with 44 additions and 16 deletions
+3 -5
View File
@@ -15,14 +15,11 @@
region: 'us-east',
url: 'https://us.stream.holopirates.moe',
},
{
region: 'us-east-2',
url: 'https://www.kitsu.red',
},
{
region: 'sg',
url: 'https://restream.kitsune.box.ca',
},
/*
{
region: 'eu',
url: 'https://eu.stream.holopirates.moe',
@@ -31,8 +28,9 @@
region: 'jp',
url: 'https://jp.stream.holopirates.moe',
},
*/
],
playlist: '/misc/stream/dance/index.m3u8',
playlist: '/index.m3u8',
};
</script>
</head>
+35 -11
View File
@@ -42,6 +42,13 @@
return results.reduce((a, b) => a + b, 0) / count;
};
const measureUpstreamLatency = async (upstream, n = 3) => {
const playlistURL = upstream.url + window.restream_config.playlist;
const latency = await measureLatency(playlistURL, n);
upstream.latency = latency;
updateMirrorList();
};
const updateMirrorList = () => {
const mirrorSelect = document.getElementById('mirror-select');
const current = mirrorSelect.value;
@@ -56,8 +63,7 @@
const option = document.createElement('option');
option.value = upstream.url;
option.text =
`[${upstream.region}] ${upstream.url}` +
'(' +
`[${upstream.region}] ${upstream.url} (` +
[
typeof upstream.latency === 'number'
? Math.floor(upstream.latency) + 'ms'
@@ -136,14 +142,25 @@
}
};
const latencyMeasurementLoop = async () => {
while (true) {
try {
for (const upstream of window.restream_config.upstreams) {
await measureUpstreamLatency(upstream, 1);
}
} catch (e) {
console.error(e);
}
await sleep(10000);
}
};
const init = async () => {
const video = document.getElementById('video');
const mirrorSelect = document.getElementById('mirror-select');
const mirrorPopup = document.getElementById('mirror-popup');
const mirrorLoadProgress = document.getElementById('mirror-load-progress');
// Init viewer counter
viewCountLoop();
const { upstreams } = window.restream_config;
// Set up hls.js
if (Hls.isSupported()) {
@@ -154,15 +171,11 @@
// Set up event listeners
mirrorSelect.addEventListener('change', () => reloadVideo());
const { upstreams } = window.restream_config;
// Measure latency
let n = 0;
for (const upstream of upstreams) {
n += 1;
const playlistURL = upstream.url + window.restream_config.playlist;
const latency = await measureLatency(playlistURL, 3);
upstream.latency = latency;
updateMirrorList();
await measureUpstreamLatency(upstream);
mirrorLoadProgress.innerText = '(' + n + '/' + upstreams.length + ')';
}
@@ -170,6 +183,13 @@
const upstream = upstreams
.filter((upstream) => typeof upstream.latency === 'number')
.sort((a, b) => a.latency - b.latency)[0];
if (!upstream) {
document.querySelector('#mirror-popup .popup-box').innerText =
'No upstream servers found';
return;
}
mirrorSelect.value = upstream.url;
reloadVideo();
@@ -179,6 +199,10 @@
setTimeout(() => {
mirrorPopup.remove();
}, 1000);
// Init loops
viewCountLoop();
latencyMeasurementLoop();
};
if (document.readyState === 'loading')
+6
View File
@@ -128,6 +128,11 @@ mkdir -p /var/cache/nginx/proxy_cache
# create the nginx config file
echo "Setting up nginx"
cat <<EOF > /etc/nginx/cors.conf
proxy_hide_header 'Access-Control-Allow-Origin';
proxy_hide_header 'Access-Control-Allow-Methods';
proxy_hide_header 'Access-Control-Allow-Headers';
proxy_hide_header 'Access-Control-Expose-Headers';
if (\$request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'HEAD, GET, POST, OPTIONS';
@@ -243,6 +248,7 @@ http {
proxy_pass $upstream_server;
location ~ \.(m3u8|mpd)$ {
include /etc/nginx/cors.conf;
rewrite ^(.*)$ $upstream_base\$1 break;
proxy_cache_valid any 1s;
proxy_pass $upstream_server;