Bläddra i källkod

Add configurable server list URLs to frontend and Docker

Stefan Stidl 4 månader sedan
förälder
incheckning
8449b94ad6
5 ändrade filer med 42 tillägg och 6 borttagningar
  1. 7 0
      doc_docker.md
  2. 7 0
      docker/entrypoint.sh
  3. 14 0
      frontend/README.md
  4. 9 5
      frontend/javascript/index.js
  5. 5 1
      index-modern.html

+ 7 - 0
doc_docker.md

@@ -58,6 +58,7 @@ Here's a list of additional environment variables available in this mode:
 
 * __`TITLE`__: Title of your speed test. Default value: `LibreSpeed`
 * __`USE_NEW_DESIGN`__: When set to `true`, enables the new modern frontend design. When set to `false` (default), uses the classic design. The design can also be switched using URL parameters (`?design=new` or `?design=old`). Default value: `false`
+* __`SERVER_LIST_URL`__: When set, both frontend designs load their server list from this URL instead of the generated or mounted `server-list.json`. This is useful if you want the containerized frontend to consume a remote shared server list.
 * __`TELEMETRY`__: Whether to enable telemetry or not. If enabled, you maybe want your data to be persisted. See below. Default value: `false`
 * __`ENABLE_ID_OBFUSCATION`__: When set to true with telemetry enabled, test IDs are obfuscated, to avoid exposing the database internal sequential IDs. Default value: `false`
 * __`OBFUSCATION_SALT`__: The salt string that is used to obfuscate the test IDs. The format shoud be a 2 byte hex string (e.g. `0x1234abcd`). If not specified, a random one will be generated.
@@ -151,6 +152,12 @@ The test can be accessed on port 80.
 
 The list of environment variables available in this mode is the same as [above in standalone mode](#standalone-mode).
 
+If you want the Docker frontend to load its server list from another URL instead of `/servers.json`, set `SERVER_LIST_URL`:
+
+```shell
+docker run -e MODE=frontend -e SERVER_LIST_URL="https://example.com/custom-server-list.json" -p 80:8080 -it ghcr.io/librespeed/speedtest
+```
+
 #### Example Frontend mode
 
 This command starts LibreSpeed in frontend mode, with a given `servers.json` file, and with telemetry, ID obfuscation, and a stats password and a persistant sqlite database for results:

+ 7 - 0
docker/entrypoint.sh

@@ -3,6 +3,7 @@
 echo "Setting up docker env..."
 echo "MODE: $MODE"
 echo "USE_NEW_DESIGN: $USE_NEW_DESIGN"
+echo "SERVER_LIST_URL: $SERVER_LIST_URL"
 echo "WEBPORT: $WEBPORT"
 echo "REDACT_IP_ADDRESSES: $REDACT_IP_ADDRESSES"
 echo "DB_TYPE: $DB_TYPE"
@@ -77,6 +78,12 @@ if [[ "$MODE" == "frontend" || "$MODE" == "dual" ||  "$MODE" == "standalone" ]];
     # generate config for just the local server
     echo '[{"name":"local","server":"/backend",  "dlURL": "garbage.php", "ulURL": "empty.php", "pingURL": "empty.php", "getIpURL": "getIP.php", "sponsorName": "", "sponsorURL": "", "id":1 }]' > /var/www/html/server-list.json
   fi
+  if [ ! -z "$SERVER_LIST_URL" ]; then
+    echo "using SERVER_LIST_URL for frontend server list"
+    SERVER_LIST_URL_ESCAPED=$(printf '%s\n' "$SERVER_LIST_URL" | sed 's/[&/\\]/\\&/g; s/\$/\\$/g')
+    sed -i "s/var SPEEDTEST_SERVERS = \"server-list.json\";/var SPEEDTEST_SERVERS = \"$SERVER_LIST_URL_ESCAPED\";/" /var/www/html/index-modern.html
+    sed -i "s/var SPEEDTEST_SERVERS = \\[/var SPEEDTEST_SERVERS = \"$SERVER_LIST_URL_ESCAPED\";\\n\\t\\t\\/\\*/" /var/www/html/index-classic.html
+  fi
   
   # Replace GDPR email placeholder if GDPR_EMAIL is set
   if [ ! -z "$GDPR_EMAIL" ]; then

+ 14 - 0
frontend/README.md

@@ -25,6 +25,20 @@ testing servers. If you only have a single testing server, just provide a list
 with one item in it, being your server. The frontend will then skip doing an
 automatic server selection and will not allow the user to change servers.
 
+If you want to load the server list from a different URL, change the
+`SPEEDTEST_SERVERS` variable in `index-modern.html` before
+`javascript/index.js` is loaded. For example:
+
+```html
+<script type="text/javascript">
+  var SPEEDTEST_SERVERS = "https://example.com/custom-server-list.json";
+</script>
+<script type="text/javascript" src="javascript/index.js"></script>
+```
+
+You can also provide a relative URL, for example
+`var SPEEDTEST_SERVERS = "/speedtest/servers.json";`.
+
 For more advanced applications, you can override any of the settings that are
 defined in `speedtest_worker.js` using the file
 [`settings.json`](./settings.json). See

+ 9 - 5
frontend/javascript/index.js

@@ -136,13 +136,17 @@ async function applySettingsJSON() {
 }
 
 /**
- * Load server list from server-list.json on the server and populate the
- * dropdown
+ * Load server list from the configured source and populate the dropdown
  */
 async function applyServerListJSON() {
   try {
-    const response = await fetch("server-list.json");
-    const servers = await response.json();
+    const serverSource =
+      typeof globalThis.SPEEDTEST_SERVERS !== "undefined"
+        ? globalThis.SPEEDTEST_SERVERS
+        : "server-list.json";
+    const servers = Array.isArray(serverSource)
+      ? serverSource
+      : await fetch(serverSource).then((response) => response.json());
     if (!servers || !Array.isArray(servers) || servers.length === 0) {
       return console.error("Server list is empty or malformed");
     }
@@ -186,7 +190,7 @@ async function applyServerListJSON() {
       }
     });
   } catch (error) {
-    console.error("Failed to fetch server list:", error);
+    console.error("Failed to load server list:", error);
   }
 }
 

+ 5 - 1
index-modern.html

@@ -8,6 +8,10 @@
     content="Free and Open Source Speedtest. Run it right now in your browser, or self-host on a PHP, Golang, Rust or Node server. License: LGPL." />
   <link rel="shortcut icon" href="images/favicon.svg" />
   <script type="text/javascript" src="speedtest.js"></script>
+  <script type="text/javascript">
+    // Set this to a different URL to load the server list from another location.
+    var SPEEDTEST_SERVERS = "server-list.json";
+  </script>
   <script type="text/javascript" src="javascript/index.js"></script>
   <link rel="stylesheet" type="text/css" href="styling/index.css" />
   <title>LibreSpeed - Free and Open Source Speedtest</title>
@@ -145,4 +149,4 @@
   </dialog>
 </body>
 
-</html>
+</html>