From c4362adc4af0c4dcdbb2346a9e077bdf580d8007 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 31 Jan 2024 23:15:22 +0100 Subject: fix native player --- server/src/routes/ui/player.rs | 66 +++++++++++++++++++++++++---------------- web/native-protocol/install | 14 +++++++-- web/native-protocol/jellynative | 22 +++++--------- web/native-protocol/uninstall | 12 ++++++++ 4 files changed, 72 insertions(+), 42 deletions(-) create mode 100755 web/native-protocol/uninstall diff --git a/server/src/routes/ui/player.rs b/server/src/routes/ui/player.rs index 712f7fb..1ef0149 100644 --- a/server/src/routes/ui/player.rs +++ b/server/src/routes/ui/player.rs @@ -3,7 +3,10 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2024 metamuffin */ -use super::{account::session::Session, layout::LayoutPage}; +use super::{ + account::session::{token, Session}, + layout::LayoutPage, +}; use crate::{ database::DataAcid, routes::{ @@ -19,11 +22,12 @@ use crate::{ use anyhow::anyhow; use jellybase::{ database::{TableExt, T_NODE}, + permission::PermissionSetExt, CONF, }; use jellycommon::{ stream::{StreamFormat, StreamSpec}, - user::PlayerKind, + user::{PermissionSet, PlayerKind, UserPermission}, Node, SourceTrackKind, TrackID, }; use markup::DynRender; @@ -46,20 +50,19 @@ impl PlayerConfig { } } -fn jellynative_url(action: &str, secret: &str, node: &str) -> String { - format!( - "jellynative://{action}/{secret}/{}://{}{}", - if CONF.tls { "https" } else { "http" }, - CONF.hostname, - uri!(r_stream( - node, - StreamSpec { - format: StreamFormat::HlsMaster, - ..Default::default() - } - )) - ) +fn jellynative_url(action: &str, secret: &str, node: &str, session: &str) -> String { + let protocol = if CONF.tls { "https" } else { "http" }; + let host = &CONF.hostname; + let stream_url = uri!(r_stream( + node, + StreamSpec { + format: StreamFormat::HlsMaster, + ..Default::default() + } + )); + format!("jellynative://{action}/{secret}/{session}/{protocol}://{host}{stream_url}",) } + #[get("/n//player?", rank = 4)] pub fn r_player<'a>( sess: Session, @@ -69,21 +72,34 @@ pub fn r_player<'a>( ) -> MyResult, Redirect>> { let item = T_NODE.get(db, id)?.ok_or(anyhow!("node does not exist"))?; + let native_session = |action: &str| { + let perm = [ + UserPermission::StreamFormat(StreamFormat::HlsMaster), + UserPermission::StreamFormat(StreamFormat::HlsVariant), + UserPermission::StreamFormat(StreamFormat::Fragment), + ]; + for perm in &perm { + sess.user.permissions.assert(perm)?; + } + Ok(Either::Right(Redirect::temporary(jellynative_url( + action, + &sess.user.native_secret, + id, + &token::create( + sess.user.name, + PermissionSet(perm.map(|e| (e, true)).into()), + chrono::Duration::hours(24), + ), + )))) + }; + match sess.user.player_preference { PlayerKind::Browser => (), PlayerKind::Native => { - return Ok(Either::Right(Redirect::temporary(jellynative_url( - "player", - &sess.user.native_secret, - id, - )))) + return native_session("player"); } PlayerKind::NativeFullscreen => { - return Ok(Either::Right(Redirect::temporary(jellynative_url( - "player-fullscreen", - &sess.user.native_secret, - id, - )))) + return native_session("player-fullscreen"); } } diff --git a/web/native-protocol/install b/web/native-protocol/install index f197b01..97d92f6 100755 --- a/web/native-protocol/install +++ b/web/native-protocol/install @@ -1,5 +1,13 @@ #!/bin/fish -install -Dm750 jellynative ~/.local/bin/jellynative -install -Dm640 jellynative.desktop ~/.local/share/applications/jellynative.desktop -update-desktop-database ~/.local/share/applications +if fish_is_root_user + echo 'Global installation' + install -Dm755 jellynative /usr/local/bin/jellynative + install -Dm644 jellynative.desktop /usr/local/share/applications/jellynative.desktop + update-desktop-database /usr/local/share/applications +else + echo 'User installation' + install -Dm750 jellynative ~/.local/bin/jellynative + install -Dm640 jellynative.desktop ~/.local/share/applications/jellynative.desktop + update-desktop-database ~/.local/share/applications +end echo 'In firefox about:config set network.protocol-handler.expose.jellynative to true' diff --git a/web/native-protocol/jellynative b/web/native-protocol/jellynative index e1b310c..8694272 100755 --- a/web/native-protocol/jellynative +++ b/web/native-protocol/jellynative @@ -4,9 +4,6 @@ set protocol $parts[1] set action $parts[3] set secret $parts[4] -if not test $protocol = "jellynative:" - die "Wrong protocol" -end function die notify-send -u critical -t 5000 'Jellynative Error' $argv[1] @@ -14,26 +11,23 @@ function die end function player - if which mpv &>/dev/null - set playercommand mpv - else if which vlc &>/dev/null - set playercommand vlc - else - die 'No Media Player detected' - end notify-send -u low -t 1000 Jellynative 'Player ist launching...' - if not $playercommand $argv[2..] $argv[1] + if not mpv --http-header-fields=(echo -s 'Cookie: session=' $argv[1]) $argv[3..] $argv[2] die 'Player exited with error code' end end +if not test $protocol = "jellynative:" + die "Wrong protocol" +end + set config_dir $XDG_CONFIG_HOME if test -z $config set config_dir ~/.config end set config $config_dir/jellynative_secret if not test -e $config - random 0 4000000000 >$config + head -c 16 /dev/urandom | xxd -p >$config end set secret (cat $config) @@ -47,9 +41,9 @@ switch $action case show-secret notify-send -u low Jellynative "Secret: $secret" case player - player (string join / $parts[5..]) + player $parts[5] (string join / $parts[6..]) case player-fullscreen - player (string join / $parts[5..]) --fullscreen + player $parts[5] (string join / $parts[6..]) --fullscreen case '*' die 'Unknown action' end diff --git a/web/native-protocol/uninstall b/web/native-protocol/uninstall new file mode 100755 index 0000000..42e074e --- /dev/null +++ b/web/native-protocol/uninstall @@ -0,0 +1,12 @@ +#!/bin/fish +if fish_is_root_user + echo 'Global uninstallation' + rm /usr/local/bin/jellynative + rm /usr/local/share/applications/jellynative.desktop + update-desktop-database /usr/local/share/applications +else + echo 'User uninstallation' + rm ~/.local/bin/jellynative + rm ~/.local/share/applications/jellynative.desktop + update-desktop-database ~/.local/share/applications +end -- cgit v1.2.3-70-g09d2