From 9f7eaeb771057cfe8f9092b08565555983ccba86 Mon Sep 17 00:00:00 2001 From: Hizkia Felix Winata Date: Mon, 31 Jan 2022 01:27:43 +0800 Subject: [PATCH] misc --- player/static/index.html | 8 +++--- player/static/player.js | 46 ++++++++++++++++++++++++++--------- scripts/nginx/remote_setup.sh | 6 +++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/player/static/index.html b/player/static/index.html index fc3244a..d05085a 100644 --- a/player/static/index.html +++ b/player/static/index.html @@ -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', }; diff --git a/player/static/player.js b/player/static/player.js index 79cc006..df0f87b 100644 --- a/player/static/player.js +++ b/player/static/player.js @@ -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') diff --git a/scripts/nginx/remote_setup.sh b/scripts/nginx/remote_setup.sh index 2538397..9be64a0 100755 --- a/scripts/nginx/remote_setup.sh +++ b/scripts/nginx/remote_setup.sh @@ -128,6 +128,11 @@ mkdir -p /var/cache/nginx/proxy_cache # create the nginx config file echo "Setting up nginx" cat < /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;