From e75a3b34f967477083c2e469b7872230af918744 Mon Sep 17 00:00:00 2001 From: Jade Macho Date: Sun, 7 Jan 2024 17:37:31 +0100 Subject: [PATCH] Cleanup --- README.md | 22 +++++++---- index.php | 92 +++++++++++++++++++++++++++++---------------- radios/desktop.json | 2 +- 3 files changed, 75 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 21260c1..fa254dc 100644 --- a/README.md +++ b/README.md @@ -14,23 +14,29 @@ This repo contains a single php file that sits in between your radio and the Air ## How can I use this? -The way I've set it up on my Raspberry Pi 4b is absolutely cursed and there's certainly better ways to do it, but it's 04:34am as I'm clacking my keys right now and I want sleep, so here's my setup: + +### TL;DR + +- Set up a web server on ports 80 and 443, +- Use `index.php` for every request, +- Redirect `airable.wifiradiofrontier.com` to your proxying device (f.e. via a DNS adblocker such as Pi-hole), +- Play around with the `radios` folder next to `index.php`. + +### My setup + +The way I've set it up on my Raspberry Pi 4b is absolutely cursed and there's certainly better ways to do it. - Home Assistant OS - Yes, I know running custom software outside of addons (which are just neatly packaged docker containers with special rules) isn't supported, but if it works in such a restrictive environment, then it surely works everywhere. - [linuxserver/nginx](https://docs.linuxserver.io/images/docker-nginx/) - Easy to set up, supports a wide range of platforms, and I don't need any of the fancyness that comes with linuxserver/swag (certbot) as it sits in my local network. - The default config uses `index.php` for all requests, which is exactly what we want! Yay! -- Plop `index.php` and the `radios` dir into `www` (or set up a mount for it). -- In your favourite DNS adblocker (Pi-hole or AdGuard Home), redirect `airable.wifiradiofrontier.com` to your device's IP. -- Put a web radio into the radios dir (using my existing config as an example), or create your own: +- Mounted such that `index.php` and the `radios` dir end up as `www` +- AdGuard Home to redirect `airable.wifiradiofrontier.com` to my raspi. +- desktop.json contains a stream hosted using the following: - [Icecast](https://icecast.org/) or another web radio server - I'm personally using [AzuraCast](https://www.azuracast.com/), which uses Icecast under the hood, but with arm64 support! - [butt](https://danielnoethen.de/butt/) - This is really just if you want to f.e. stream your PC audio to your internet radio. -- ??? -- Profit! - -**TL;DR: Set up a web server on ports 80 and 443, make it use `index.php` for every request, play around with the `radios` folder.** ## Any hints for following your footsteps? diff --git a/index.php b/index.php index ba80704..6c941df 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,7 @@ &$stream) { - $stream["url"] = "$GLOBALS[frairable_host]/frontiersmart/radio/custom=$id/play=$index"; + $stream["url"] = "$GLOBALS[frairable_host]$GLOBALS[frairable_uri_meta]$id/$GLOBALS[frairable_part_play]$index"; } return $data; @@ -145,11 +153,13 @@ function get_radio_meta($id) { function get_radio_play($id, $index) { $data = get_radio_data($id); - // clone loves to complain about the stream not being an object... - // deep clone using json is ugly, but eh. + // Clone loves to complain about the stream not being an object... + // Deep clone using json is ugly, but eh. $stream = json_decode(json_encode($data["streams"][$index]), true); - $stream["id"] = ["frontiersmart", "redirect", "custom=$id/play=$index"]; + // The redirect ID can be anything, as long as it is unique. + // It doesn't need to match with the URI. + $stream["id"] = ["frontiersmart", "redirect", "$GLOBALS[frairable_part_custom]$id/$GLOBALS[frairable_part_play]$index"]; $stream["content"] = $data; unset($stream["reliability"]); @@ -157,8 +167,14 @@ function get_radio_play($id, $index) { return $stream; } + + +// Routing + if ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios") { - airable_proxy(function(&$data) { + // Stock starting point - add our own entry here. + + respond_with_proxy(function(&$data) { array_push($data["content"]["entries"], [ "id" => [ "frontiersmart", @@ -166,11 +182,14 @@ if ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios") { "custom" ], "title" => "Frairable (Custom)", - "url" => "$GLOBALS[frairable_host]/frontiersmart/radios/custom" + "url" => "$GLOBALS[frairable_host]$GLOBALS[frairable_uri_listing]" ]); }); -} elseif ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios/custom") { + +} elseif ($_SERVER["REQUEST_URI"] === $GLOBALS["frairable_uri_listing"]) { + // Our custom menu with our own entries. + $entries = []; foreach (new DirectoryIterator($GLOBALS["frairable_dir_radios"]) as $sub) { @@ -190,7 +209,7 @@ if ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios") { "custom" ], "title" => "Frairable (Custom)", - "url" => "$GLOBALS[frairable_host]$_SERVER[REQUEST_URI]", + "url" => "$GLOBALS[frairable_host]$GLOBALS[frairable_uri_listing]", "content" => [ "entries" => $entries ] @@ -198,12 +217,18 @@ if ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios") { respond_with_json($data); -} elseif (str_starts_with($_SERVER["REQUEST_URI"], "/frontiersmart/radio/custom=")) { - $id = substr($_SERVER["REQUEST_URI"], strlen("/frontiersmart/radio/custom=")); - $split = strpos($id, "/play="); +} elseif (str_starts_with($_SERVER["REQUEST_URI"], $GLOBALS["frairable_uri_meta"])) { + // The radio fetches different radio info multiple times: + // - Metadata when selecting the entry, containing a list of streams. + // - Playing info after the radio picked a stream to play. + // See the dumped meta.json and play.json for more info.. and to witness the complex redundancy. + + $id = substr($_SERVER["REQUEST_URI"], strlen($GLOBALS["frairable_uri_meta"])); + + $split = strpos($id, "/$GLOBALS[frairable_part_play]"); if ($split) { - $index = intval(substr($id, $split + strlen("/play="))); + $index = intval(substr($id, $split + strlen("/$GLOBALS[frairable_part_play]"))); $id = substr($id, 0, $split); respond_with_json(get_radio_play($id, $index)); @@ -211,8 +236,11 @@ if ($_SERVER["REQUEST_URI"] === "/frontiersmart/radios") { respond_with_json(get_radio_meta($id)); } + } else { - airable_proxy(); + respond_with_proxy(); } + + ?> \ No newline at end of file diff --git a/radios/desktop.json b/radios/desktop.json index 4589273..fd1b6d8 100644 --- a/radios/desktop.json +++ b/radios/desktop.json @@ -22,7 +22,7 @@ "samplerate": 48 }, "reliability": 0.99, - "url": "http://icecast.toriel.0x0a.de/desktop.mp3" + "url": "http://azuracast.toriel.0x0a.de/listen/toriel_radio/radio.mp3" } ] }