fix player routing based on host

This commit is contained in:
JRK 2024-06-13 18:12:16 +02:00 committed by GitHub
parent bf0eb42fca
commit bb432c9694
2 changed files with 23 additions and 14 deletions

View File

@ -6,7 +6,7 @@ from flask import Flask, render_template, redirect, request, url_for, send_from_
from src.service.ModelStore import ModelStore
from src.interface.ObController import ObController
from src.util.utils import get_safe_cron_descriptor
from src.util.UtilNetwork import get_ip_address, get_safe_remote_addr
from src.util.UtilNetwork import get_ip_address, host_to_safe_ipaddr
from src.model.enum.AnimationSpeed import animation_speed_duration
@ -45,15 +45,7 @@ class PlayerController(ObController):
self._app.add_url_rule('/player/playlist/use/<playlist_slug_or_id>', 'player_playlist_use', self.player_playlist, methods=['GET'])
def player(self, playlist_slug_or_id: str = ''):
if not playlist_slug_or_id and self._model_store.variable().get_one_by_name('fleet_player_enabled'):
node_player = self._model_store.node_player().get_one_by("host = '{}' and enabled = {}".format(
get_safe_remote_addr(request.remote_addr),
True
))
if node_player and node_player.group_id:
node_player_group = self._model_store.node_player_group().get(node_player.group_id)
playlist_slug_or_id = node_player_group.playlist_id
playlist_slug_or_id = self._get_dynamic_playlist_id(playlist_slug_or_id)
current_playlist = self._model_store.playlist().get_one_by("slug = ? OR id = ?", {
"slug": playlist_slug_or_id,
@ -85,6 +77,8 @@ class PlayerController(ObController):
)
def player_playlist(self, playlist_slug_or_id: str = ''):
playlist_slug_or_id = self._get_dynamic_playlist_id(playlist_slug_or_id)
current_playlist = self._model_store.playlist().get_one_by("slug = ? OR id = ?", {
"slug": playlist_slug_or_id,
"id": playlist_slug_or_id
@ -92,3 +86,16 @@ class PlayerController(ObController):
playlist_id = current_playlist.id if current_playlist else None
return jsonify(self._get_playlist(playlist_id=playlist_id))
def _get_dynamic_playlist_id(self, playlist_slug_or_id: Optional[str]) -> str:
if not playlist_slug_or_id and self._model_store.variable().get_one_by_name('fleet_player_enabled'):
node_player = self._model_store.node_player().get_one_by("host = '{}' and enabled = {}".format(
host_to_safe_ipaddr(request.headers.getlist("Host")[0]),
True
))
if node_player and node_player.group_id:
node_player_group = self._model_store.node_player_group().get(node_player.group_id)
playlist_slug_or_id = node_player_group.playlist_id
return playlist_slug_or_id

View File

@ -35,7 +35,9 @@ def get_ip_address() -> Optional[str]:
return get_network_ipaddr()
def get_safe_remote_addr(remote_addr: str) -> str:
if remote_addr == '127.0.0.1' or remote_addr == 'localhost':
return get_network_ipaddr()
return remote_addr
def get_safe_remote_addr(host: str) -> str:
host = host.split(':')[0]
if host == '127.0.0.1' or host == 'localhost':
return get_ip_address()
return host