2015-06-23 14:38:29 +00:00
|
|
|
use crypto::digest::Digest;
|
|
|
|
use crypto::sha1::Sha1;
|
2015-09-01 11:20:37 +00:00
|
|
|
use eventual::Future;
|
2016-01-01 23:16:12 +00:00
|
|
|
use std::io::Write;
|
2015-07-07 21:40:31 +00:00
|
|
|
use std::path::PathBuf;
|
2016-01-01 23:16:12 +00:00
|
|
|
use std::sync::{Mutex, RwLock, Arc, mpsc};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2016-01-01 23:16:12 +00:00
|
|
|
use audio_key::{AudioKeyManager, AudioKey, AudioKeyError};
|
|
|
|
use audio_file::{AudioFileManager, AudioFile};
|
2016-01-26 23:01:45 +00:00
|
|
|
use connection::{self, CipherConnection};
|
2016-01-01 23:16:12 +00:00
|
|
|
use connection::PacketHandler;
|
2015-07-02 17:24:25 +00:00
|
|
|
use mercury::{MercuryManager, MercuryRequest, MercuryResponse};
|
|
|
|
use metadata::{MetadataManager, MetadataRef, MetadataTrait};
|
|
|
|
use stream::{StreamManager, StreamEvent};
|
2016-01-01 23:16:12 +00:00
|
|
|
use util::{SpotifyId, FileId, mkdir_existing};
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2016-01-02 02:30:24 +00:00
|
|
|
pub enum Bitrate {
|
|
|
|
Bitrate96,
|
|
|
|
Bitrate160,
|
2016-01-02 15:19:39 +00:00
|
|
|
Bitrate320,
|
2016-01-02 02:30:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 10:07:24 +00:00
|
|
|
pub struct Config {
|
|
|
|
pub application_key: Vec<u8>,
|
|
|
|
pub user_agent: String,
|
2016-01-01 23:16:12 +00:00
|
|
|
pub device_name: String,
|
2015-07-07 21:40:31 +00:00
|
|
|
pub cache_location: PathBuf,
|
2016-01-02 02:30:24 +00:00
|
|
|
pub bitrate: Bitrate,
|
2015-05-09 10:07:24 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
pub struct SessionData {
|
2016-01-26 23:01:45 +00:00
|
|
|
country: String,
|
|
|
|
canonical_username: String,
|
|
|
|
device_id: String,
|
2015-12-29 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SessionInternal {
|
2016-01-26 22:34:57 +00:00
|
|
|
config: Config,
|
2016-01-26 23:01:45 +00:00
|
|
|
data: RwLock<SessionData>,
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
mercury: Mutex<MercuryManager>,
|
|
|
|
metadata: Mutex<MetadataManager>,
|
|
|
|
stream: Mutex<StreamManager>,
|
|
|
|
audio_key: Mutex<AudioKeyManager>,
|
2015-07-03 00:23:49 +00:00
|
|
|
audio_file: Mutex<AudioFileManager>,
|
2016-01-01 23:16:12 +00:00
|
|
|
rx_connection: Mutex<Option<CipherConnection>>,
|
|
|
|
tx_connection: Mutex<Option<CipherConnection>>,
|
2015-04-25 20:32:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
#[derive(Clone)]
|
2015-12-29 12:13:26 +00:00
|
|
|
pub struct Session(pub Arc<SessionInternal>);
|
2015-07-02 17:24:25 +00:00
|
|
|
|
2015-04-25 20:32:07 +00:00
|
|
|
impl Session {
|
2016-01-01 23:16:12 +00:00
|
|
|
pub fn new(config: Config) -> Session {
|
|
|
|
mkdir_existing(&config.cache_location).unwrap();
|
|
|
|
|
|
|
|
let device_id = {
|
2015-05-09 10:07:24 +00:00
|
|
|
let mut h = Sha1::new();
|
2016-01-01 23:16:12 +00:00
|
|
|
h.input_str(&config.device_name);
|
2015-05-09 10:07:24 +00:00
|
|
|
h.result_str()
|
|
|
|
};
|
|
|
|
|
2016-01-01 23:16:12 +00:00
|
|
|
Session(Arc::new(SessionInternal {
|
|
|
|
config: config,
|
|
|
|
data: RwLock::new(SessionData {
|
|
|
|
country: String::new(),
|
|
|
|
canonical_username: String::new(),
|
|
|
|
device_id: device_id,
|
|
|
|
}),
|
|
|
|
|
|
|
|
rx_connection: Mutex::new(None),
|
|
|
|
tx_connection: Mutex::new(None),
|
|
|
|
|
|
|
|
mercury: Mutex::new(MercuryManager::new()),
|
|
|
|
metadata: Mutex::new(MetadataManager::new()),
|
|
|
|
stream: Mutex::new(StreamManager::new()),
|
|
|
|
audio_key: Mutex::new(AudioKeyManager::new()),
|
|
|
|
audio_file: Mutex::new(AudioFileManager::new()),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-01-26 23:01:45 +00:00
|
|
|
pub fn authenticated(&self, username: String, connection: CipherConnection) {
|
|
|
|
self.0.data.write().unwrap().canonical_username = username;
|
|
|
|
*self.0.rx_connection.lock().unwrap() = Some(connection.clone());
|
|
|
|
*self.0.tx_connection.lock().unwrap() = Some(connection);
|
2015-06-23 14:38:29 +00:00
|
|
|
}
|
2015-05-09 10:07:24 +00:00
|
|
|
|
2015-06-23 14:38:29 +00:00
|
|
|
pub fn poll(&self) {
|
2016-01-01 23:16:12 +00:00
|
|
|
let (cmd, data) = self.recv();
|
2015-07-02 17:24:25 +00:00
|
|
|
|
|
|
|
match cmd {
|
|
|
|
0x4 => self.send_packet(0x49, &data).unwrap(),
|
|
|
|
0x4a => (),
|
2016-01-02 15:19:39 +00:00
|
|
|
0x9 => self.0.stream.lock().unwrap().handle(cmd, data),
|
2015-09-01 11:20:37 +00:00
|
|
|
0xd | 0xe => self.0.audio_key.lock().unwrap().handle(cmd, data),
|
2015-12-29 12:13:26 +00:00
|
|
|
0x1b => {
|
2016-01-02 15:19:39 +00:00
|
|
|
self.0.data.write().unwrap().country = String::from_utf8(data).unwrap();
|
|
|
|
}
|
2015-09-01 11:20:37 +00:00
|
|
|
0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data),
|
2016-01-02 15:19:39 +00:00
|
|
|
_ => (),
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-01 23:16:12 +00:00
|
|
|
pub fn recv(&self) -> (u8, Vec<u8>) {
|
|
|
|
self.0.rx_connection.lock().unwrap().as_mut().unwrap().recv_packet().unwrap()
|
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn send_packet(&self, cmd: u8, data: &[u8]) -> connection::Result<()> {
|
2016-01-01 23:16:12 +00:00
|
|
|
self.0.tx_connection.lock().unwrap().as_mut().unwrap().send_packet(cmd, data)
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
2015-12-29 12:13:26 +00:00
|
|
|
|
2015-12-29 22:12:02 +00:00
|
|
|
pub fn audio_key(&self, track: SpotifyId, file: FileId) -> Future<AudioKey, AudioKeyError> {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.0.audio_key.lock().unwrap().request(self, track, file)
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:23:49 +00:00
|
|
|
pub fn audio_file(&self, file: FileId) -> AudioFile {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.0.audio_file.lock().unwrap().request(self, file)
|
2015-07-03 00:23:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 17:24:25 +00:00
|
|
|
pub fn stream(&self, file: FileId, offset: u32, size: u32) -> mpsc::Receiver<StreamEvent> {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.0.stream.lock().unwrap().request(self, file, offset, size)
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn metadata<T: MetadataTrait>(&self, id: SpotifyId) -> MetadataRef<T> {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.0.metadata.lock().unwrap().get(self, id)
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 11:20:37 +00:00
|
|
|
pub fn mercury(&self, req: MercuryRequest) -> Future<MercuryResponse, ()> {
|
|
|
|
self.0.mercury.lock().unwrap().request(self, req)
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mercury_sub(&self, uri: String) -> mpsc::Receiver<MercuryResponse> {
|
2015-09-01 11:20:37 +00:00
|
|
|
self.0.mercury.lock().unwrap().subscribe(self, uri)
|
2015-04-25 20:32:07 +00:00
|
|
|
}
|
2016-01-26 22:34:57 +00:00
|
|
|
|
|
|
|
pub fn config(&self) -> &Config {
|
|
|
|
&self.0.config
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn username(&self) -> String {
|
|
|
|
self.0.data.read().unwrap().canonical_username.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn country(&self) -> String {
|
|
|
|
self.0.data.read().unwrap().country.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn device_id(&self) -> String {
|
|
|
|
self.0.data.read().unwrap().device_id.clone()
|
|
|
|
}
|
2015-04-25 20:32:07 +00:00
|
|
|
}
|