|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:finamp/models/jellyfin_models.dart'; |
| 5 | +import 'package:finamp/services/finamp_user_helper.dart'; |
| 6 | +import 'package:get_it/get_it.dart'; |
| 7 | +import 'package:logging/logging.dart'; |
| 8 | + |
| 9 | +/// Used for discovering Jellyfin servers on the local network |
| 10 | +/// https://jellyfin.org/docs/general/networking/#port-bindings |
| 11 | +/// For some reason it's always being referred to as "client discovery" in the Jellyfin docs, even though we're actually discovering servers |
| 12 | +class JellyfinServerDiscoveryEmulationService { |
| 13 | + static final _serverDiscoveryEmulationLogger = Logger("JellyfinServerDiscoveryEmulation"); |
| 14 | + |
| 15 | + final _finampUserHelper = GetIt.instance<FinampUserHelper>(); |
| 16 | + |
| 17 | + late RawDatagramSocket socket; |
| 18 | + bool isDisposed = false; |
| 19 | + |
| 20 | + JellyfinServerDiscoveryEmulationService() { |
| 21 | + advertiseServer(); |
| 22 | + } |
| 23 | + |
| 24 | + void advertiseServer() async { |
| 25 | + |
| 26 | + const discoveryMessage = |
| 27 | + "who is JellyfinServer?"; // doesn't seem to be case sensitive, but the Kotlin SDK uses this capitalization |
| 28 | + final broadcastAddress = |
| 29 | + InternetAddress("255.255.255.255"); // UDP broadcast address |
| 30 | + const discoveryPort = 7359; // Jellyfin client discovery port |
| 31 | + |
| 32 | + socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, discoveryPort); |
| 33 | + socket.broadcastEnabled = |
| 34 | + true; // important to allow sending to broadcast address |
| 35 | + socket.multicastHops = 5; // to account for weird network setups |
| 36 | + |
| 37 | + _serverDiscoveryEmulationLogger.fine("Advertising server on port $discoveryPort"); |
| 38 | + |
| 39 | + socket.listen((event) { |
| 40 | + if (event == RawSocketEvent.read) { |
| 41 | + final datagram = socket.receive(); |
| 42 | + if (datagram != null) { |
| 43 | + _serverDiscoveryEmulationLogger |
| 44 | + .finest("Received datagram: ${utf8.decode(datagram.data)}"); |
| 45 | + final requestMessage = utf8.decode(datagram.data); |
| 46 | + if (requestMessage.toLowerCase().contains(discoveryMessage.toLowerCase())) { |
| 47 | + _serverDiscoveryEmulationLogger.fine("Received discovery message from ${datagram.address}:${datagram.port}"); |
| 48 | + // Respond with the server's information |
| 49 | + final response = ClientDiscoveryResponse( |
| 50 | + address: _finampUserHelper.currentUser?.baseUrl, |
| 51 | + endpointAddress: _finampUserHelper.currentUser?.baseUrl, |
| 52 | + id: _finampUserHelper.currentUser?.serverId, |
| 53 | + name: "Jellyfin Server (provided by Finamp)", |
| 54 | + ); |
| 55 | + final responseMessage = jsonEncode(response); |
| 56 | + _serverDiscoveryEmulationLogger.finest("Sending discovery response: $responseMessage"); |
| 57 | + socket.send(utf8.encode(responseMessage), datagram.address, datagram.port); |
| 58 | + _serverDiscoveryEmulationLogger.fine("Sent discovery response to ${datagram.address}:${datagram.port}"); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + void dispose() { |
| 66 | + isDisposed = true; |
| 67 | + socket.close(); |
| 68 | + } |
| 69 | +} |
0 commit comments