librespot/core/src/session.rs

484 lines
14 KiB
Rust
Raw Normal View History

use std::{
collections::HashMap,
future::Future,
io,
pin::Pin,
process::exit,
sync::{Arc, Weak},
task::{Context, Poll},
time::{SystemTime, UNIX_EPOCH},
};
2019-03-24 14:15:14 +00:00
use byteorder::{BigEndian, ByteOrder};
use bytes::Bytes;
use futures_core::TryStream;
2021-04-17 09:55:57 +00:00
use futures_util::{future, ready, StreamExt, TryStreamExt};
use num_traits::FromPrimitive;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;
2021-12-10 23:03:35 +00:00
use quick_xml::events::Event;
2021-02-13 10:53:23 +00:00
use thiserror::Error;
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
2015-06-23 14:38:29 +00:00
use crate::{
apresolve::ApResolver,
audio_key::AudioKeyManager,
authentication::Credentials,
cache::Cache,
channel::ChannelManager,
config::SessionConfig,
connection::{self, AuthenticationError},
http_client::HttpClient,
mercury::MercuryManager,
packet::PacketType,
spclient::SpClient,
token::TokenProvider,
Error,
};
2021-02-13 10:53:23 +00:00
#[derive(Debug, Error)]
pub enum SessionError {
#[error(transparent)]
AuthenticationError(#[from] AuthenticationError),
#[error("Cannot create session: {0}")]
IoError(#[from] io::Error),
#[error("Session is not connected")]
NotConnected,
#[error("packet {0} unknown")]
Packet(u8),
}
impl From<SessionError> for Error {
fn from(err: SessionError) -> Self {
match err {
SessionError::AuthenticationError(_) => Error::unauthenticated(err),
SessionError::IoError(_) => Error::unavailable(err),
SessionError::NotConnected => Error::unavailable(err),
SessionError::Packet(_) => Error::unimplemented(err),
}
}
2021-02-13 10:53:23 +00:00
}
2021-12-10 23:03:35 +00:00
pub type UserAttributes = HashMap<String, String>;
#[derive(Debug, Clone, Default)]
pub struct UserData {
pub country: String,
pub canonical_username: String,
pub attributes: UserAttributes,
}
#[derive(Debug, Clone, Default)]
2018-02-12 19:10:36 +00:00
struct SessionData {
client_id: String,
2021-12-11 22:06:58 +00:00
connection_id: String,
time_delta: i64,
2018-04-21 15:46:29 +00:00
invalid: bool,
user_data: UserData,
}
2018-02-12 19:13:37 +00:00
struct SessionInternal {
config: SessionConfig,
data: RwLock<SessionData>,
2015-06-23 14:38:29 +00:00
2021-06-20 21:09:27 +00:00
http_client: HttpClient,
tx_connection: OnceCell<mpsc::UnboundedSender<(u8, Vec<u8>)>>,
2017-01-18 20:39:46 +00:00
apresolver: OnceCell<ApResolver>,
2021-01-21 20:49:39 +00:00
audio_key: OnceCell<AudioKeyManager>,
channel: OnceCell<ChannelManager>,
mercury: OnceCell<MercuryManager>,
spclient: OnceCell<SpClient>,
2021-06-19 20:47:39 +00:00
token_provider: OnceCell<TokenProvider>,
cache: Option<Arc<Cache>>,
2017-01-19 22:45:24 +00:00
handle: tokio::runtime::Handle,
2015-04-25 20:32:07 +00:00
}
2017-01-18 20:39:46 +00:00
#[derive(Clone)]
2018-02-12 19:13:37 +00:00
pub struct Session(Arc<SessionInternal>);
2017-01-18 20:39:46 +00:00
2015-04-25 20:32:07 +00:00
impl Session {
pub fn new(config: SessionConfig, cache: Option<Cache>) -> Self {
2021-06-20 21:09:27 +00:00
let http_client = HttpClient::new(config.proxy.as_ref());
2017-01-20 02:37:02 +00:00
debug!("new Session");
2017-01-18 18:41:22 +00:00
Self(Arc::new(SessionInternal {
2021-03-01 02:37:22 +00:00
config,
data: RwLock::new(SessionData::default()),
2021-06-20 21:09:27 +00:00
http_client,
tx_connection: OnceCell::new(),
cache: cache.map(Arc::new),
apresolver: OnceCell::new(),
2021-01-21 20:49:39 +00:00
audio_key: OnceCell::new(),
channel: OnceCell::new(),
mercury: OnceCell::new(),
spclient: OnceCell::new(),
2021-06-19 20:47:39 +00:00
token_provider: OnceCell::new(),
handle: tokio::runtime::Handle::current(),
}))
}
pub async fn connect(&self, credentials: Credentials) -> Result<(), Error> {
let ap = self.apresolver().resolve("accesspoint").await?;
info!("Connecting to AP \"{}:{}\"", ap.0, ap.1);
let mut transport = connection::connect(&ap.0, ap.1, self.config().proxy.as_ref()).await?;
let reusable_credentials =
connection::authenticate(&mut transport, credentials, &self.config().device_id).await?;
info!("Authenticated as \"{}\" !", reusable_credentials.username);
self.set_username(&reusable_credentials.username);
if let Some(cache) = self.cache() {
cache.save_credentials(&reusable_credentials);
}
let (tx_connection, rx_connection) = mpsc::unbounded_channel();
self.0
.tx_connection
.set(tx_connection)
.map_err(|_| SessionError::NotConnected)?;
let (sink, stream) = transport.split();
let sender_task = UnboundedReceiverStream::new(rx_connection)
.map(Ok)
.forward(sink);
let receiver_task = DispatchTask(stream, self.weak());
2017-01-20 02:37:02 +00:00
2021-04-12 20:54:32 +00:00
tokio::spawn(async move {
let result = future::try_join(sender_task, receiver_task).await;
if let Err(e) = result {
error!("{}", e);
}
});
Ok(())
}
pub fn apresolver(&self) -> &ApResolver {
self.0
.apresolver
.get_or_init(|| ApResolver::new(self.weak()))
2015-06-23 14:38:29 +00:00
}
2017-01-18 20:39:46 +00:00
pub fn audio_key(&self) -> &AudioKeyManager {
2021-01-21 20:49:39 +00:00
self.0
.audio_key
.get_or_init(|| AudioKeyManager::new(self.weak()))
2017-01-18 20:39:46 +00:00
}
2017-01-18 18:41:22 +00:00
2017-01-19 22:45:24 +00:00
pub fn channel(&self) -> &ChannelManager {
2021-01-21 20:49:39 +00:00
self.0
.channel
.get_or_init(|| ChannelManager::new(self.weak()))
2017-01-19 22:45:24 +00:00
}
pub fn http_client(&self) -> &HttpClient {
&self.0.http_client
}
2017-01-18 21:33:52 +00:00
pub fn mercury(&self) -> &MercuryManager {
2021-01-21 20:49:39 +00:00
self.0
.mercury
.get_or_init(|| MercuryManager::new(self.weak()))
2017-01-18 21:33:52 +00:00
}
pub fn spclient(&self) -> &SpClient {
self.0.spclient.get_or_init(|| SpClient::new(self.weak()))
}
2021-06-19 20:47:39 +00:00
pub fn token_provider(&self) -> &TokenProvider {
self.0
.token_provider
.get_or_init(|| TokenProvider::new(self.weak()))
}
pub fn time_delta(&self) -> i64 {
self.0.data.read().time_delta
2019-03-24 14:15:14 +00:00
}
2021-01-21 20:49:39 +00:00
pub fn spawn<T>(&self, task: T)
2018-02-11 11:37:08 +00:00
where
2021-01-21 20:49:39 +00:00
T: Future + Send + 'static,
T::Output: Send + 'static,
2017-01-19 22:45:24 +00:00
{
self.0.handle.spawn(task);
2017-01-19 22:45:24 +00:00
}
fn debug_info(&self) {
2018-02-11 11:37:08 +00:00
debug!(
"Session strong={} weak={}",
2018-02-11 11:37:08 +00:00
Arc::strong_count(&self.0),
Arc::weak_count(&self.0)
);
}
fn check_catalogue(attributes: &UserAttributes) {
if let Some(account_type) = attributes.get("type") {
if account_type != "premium" {
error!("librespot does not support {:?} accounts.", account_type);
info!("Please support Spotify and your artists and sign up for a premium account.");
// TODO: logout instead of exiting
exit(1);
}
}
}
fn dispatch(&self, cmd: u8, data: Bytes) -> Result<(), Error> {
use PacketType::*;
let packet_type = FromPrimitive::from_u8(cmd);
let cmd = match packet_type {
Some(cmd) => cmd,
None => {
trace!("Ignoring unknown packet {:x}", cmd);
return Err(SessionError::Packet(cmd).into());
}
};
match packet_type {
Some(Ping) => {
let server_timestamp = BigEndian::read_u32(data.as_ref()) as i64;
2019-03-24 14:15:14 +00:00
let timestamp = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(dur) => dur,
Err(err) => err.duration(),
}
.as_secs() as i64;
2019-03-24 14:15:14 +00:00
self.0.data.write().time_delta = server_timestamp - timestamp;
2019-03-24 14:15:14 +00:00
self.debug_info();
self.send_packet(Pong, vec![0, 0, 0, 0])
2018-02-11 11:37:08 +00:00
}
Some(CountryCode) => {
let country = String::from_utf8(data.as_ref().to_owned())?;
2017-04-29 14:18:16 +00:00
info!("Country: {:?}", country);
self.0.data.write().user_data.country = country;
Ok(())
2016-01-02 15:19:39 +00:00
}
Some(StreamChunkRes) | Some(ChannelError) => self.channel().dispatch(cmd, data),
Some(AesKey) | Some(AesKeyError) => self.audio_key().dispatch(cmd, data),
Some(MercuryReq) | Some(MercurySub) | Some(MercuryUnsub) | Some(MercuryEvent) => {
self.mercury().dispatch(cmd, data)
}
2021-12-10 23:03:35 +00:00
Some(ProductInfo) => {
let data = std::str::from_utf8(&data)?;
2021-12-10 23:03:35 +00:00
let mut reader = quick_xml::Reader::from_str(data);
let mut buf = Vec::new();
let mut current_element = String::new();
let mut user_attributes: UserAttributes = HashMap::new();
loop {
match reader.read_event(&mut buf) {
Ok(Event::Start(ref element)) => {
current_element = std::str::from_utf8(element.name())?.to_owned()
2021-12-10 23:03:35 +00:00
}
Ok(Event::End(_)) => {
current_element = String::new();
}
Ok(Event::Text(ref value)) => {
if !current_element.is_empty() {
let _ = user_attributes.insert(
current_element.clone(),
value.unescape_and_decode(&reader)?,
2021-12-10 23:03:35 +00:00
);
}
}
Ok(Event::Eof) => break,
Ok(_) => (),
Err(e) => error!(
"Error parsing XML at position {}: {:?}",
reader.buffer_position(),
e
),
}
}
2021-12-11 19:45:08 +00:00
trace!("Received product info: {:#?}", user_attributes);
Self::check_catalogue(&user_attributes);
self.0.data.write().user_data.attributes = user_attributes;
Ok(())
2021-12-10 23:03:35 +00:00
}
2021-06-23 19:26:52 +00:00
Some(PongAck)
| Some(SecretBlock)
| Some(LegacyWelcome)
| Some(UnknownDataAllZeros)
| Some(LicenseVersion) => Ok(()),
_ => {
trace!("Ignoring {:?} packet with data {:#?}", cmd, data);
Err(SessionError::Packet(cmd as u8).into())
}
}
}
pub fn send_packet(&self, cmd: PacketType, data: Vec<u8>) -> Result<(), Error> {
match self.0.tx_connection.get() {
Some(tx) => Ok(tx.send((cmd as u8, data))?),
None => Err(SessionError::NotConnected.into()),
}
}
pub fn cache(&self) -> Option<&Arc<Cache>> {
self.0.cache.as_ref()
}
pub fn config(&self) -> &SessionConfig {
&self.0.config
}
2021-12-30 21:36:38 +00:00
// This clones a fairly large struct, so use a specific getter or setter unless
// you need more fields at once, in which case this can spare multiple `read`
// locks.
pub fn user_data(&self) -> UserData {
self.0.data.read().user_data.clone()
}
pub fn device_id(&self) -> &str {
2017-01-18 17:07:20 +00:00
&self.config().device_id
}
2017-01-18 20:39:46 +00:00
pub fn client_id(&self) -> String {
self.0.data.read().client_id.clone()
}
pub fn set_client_id(&self, client_id: &str) {
self.0.data.write().client_id = client_id.to_owned();
}
2021-12-11 22:06:58 +00:00
pub fn connection_id(&self) -> String {
self.0.data.read().connection_id.clone()
2021-12-11 22:06:58 +00:00
}
pub fn set_connection_id(&self, connection_id: &str) {
self.0.data.write().connection_id = connection_id.to_owned();
2021-12-11 22:06:58 +00:00
}
pub fn username(&self) -> String {
self.0.data.read().user_data.canonical_username.clone()
2021-12-10 23:03:35 +00:00
}
pub fn set_username(&self, username: &str) {
self.0.data.write().user_data.canonical_username = username.to_owned();
}
2021-12-30 21:36:38 +00:00
pub fn country(&self) -> String {
self.0.data.read().user_data.country.clone()
}
2021-12-10 23:03:35 +00:00
pub fn set_user_attribute(&self, key: &str, value: &str) -> Option<String> {
let mut dummy_attributes = UserAttributes::new();
dummy_attributes.insert(key.to_owned(), value.to_owned());
Self::check_catalogue(&dummy_attributes);
2021-12-10 23:03:35 +00:00
self.0
.data
.write()
.user_data
.attributes
2021-12-10 23:03:35 +00:00
.insert(key.to_owned(), value.to_owned())
}
pub fn set_user_attributes(&self, attributes: UserAttributes) {
Self::check_catalogue(&attributes);
self.0.data.write().user_data.attributes.extend(attributes)
2021-12-10 23:03:35 +00:00
}
pub fn get_user_attribute(&self, key: &str) -> Option<String> {
self.0
.data
.read()
.user_data
.attributes
.get(key)
.map(Clone::clone)
}
2018-02-10 09:10:26 +00:00
fn weak(&self) -> SessionWeak {
2017-01-18 20:39:46 +00:00
SessionWeak(Arc::downgrade(&self.0))
}
2018-04-21 15:46:29 +00:00
pub fn shutdown(&self) {
debug!("Invalidating session");
self.0.data.write().invalid = true;
self.mercury().shutdown();
self.channel().shutdown();
2018-04-21 15:46:29 +00:00
}
pub fn is_invalid(&self) -> bool {
self.0.data.read().invalid
2018-04-21 15:46:29 +00:00
}
2017-01-18 20:39:46 +00:00
}
#[derive(Clone)]
2018-02-12 19:13:37 +00:00
pub struct SessionWeak(Weak<SessionInternal>);
2017-01-18 20:39:46 +00:00
impl SessionWeak {
fn try_upgrade(&self) -> Option<Session> {
self.0.upgrade().map(Session)
}
pub(crate) fn upgrade(&self) -> Session {
self.try_upgrade()
.expect("session was dropped and so should have this component")
}
}
impl Drop for SessionInternal {
fn drop(&mut self) {
debug!("drop Session");
}
}
struct DispatchTask<S>(S, SessionWeak)
2018-02-11 11:37:08 +00:00
where
2021-01-21 20:49:39 +00:00
S: TryStream<Ok = (u8, Bytes)> + Unpin;
2018-02-11 11:37:08 +00:00
impl<S> Future for DispatchTask<S>
where
2021-01-21 20:49:39 +00:00
S: TryStream<Ok = (u8, Bytes)> + Unpin,
<S as TryStream>::Ok: std::fmt::Debug,
{
2021-01-21 20:49:39 +00:00
type Output = Result<(), S::Error>;
2021-01-21 20:49:39 +00:00
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let session = match self.1.try_upgrade() {
Some(session) => session,
2021-01-21 20:49:39 +00:00
None => return Poll::Ready(Ok(())),
};
loop {
2021-04-17 09:55:57 +00:00
let (cmd, data) = match ready!(self.0.try_poll_next_unpin(cx)) {
Some(Ok(t)) => t,
None => {
warn!("Connection to server closed.");
session.shutdown();
2021-01-21 20:49:39 +00:00
return Poll::Ready(Ok(()));
2020-01-24 01:26:16 +00:00
}
2021-04-17 09:55:57 +00:00
Some(Err(e)) => {
2018-04-21 15:46:29 +00:00
session.shutdown();
2021-01-21 20:49:39 +00:00
return Poll::Ready(Err(e));
2018-04-21 15:46:29 +00:00
}
};
2018-04-21 15:46:29 +00:00
if let Err(e) = session.dispatch(cmd, data) {
error!("could not dispatch command: {}", e);
}
}
}
}
2018-02-11 11:37:08 +00:00
impl<S> Drop for DispatchTask<S>
where
2021-01-21 20:49:39 +00:00
S: TryStream<Ok = (u8, Bytes)> + Unpin,
{
fn drop(&mut self) {
debug!("drop Dispatch");
2017-01-18 20:39:46 +00:00
}
2015-04-25 20:32:07 +00:00
}