2015-06-23 14:38:29 +00:00
|
|
|
use crypto::digest::Digest;
|
|
|
|
use crypto::sha1::Sha1;
|
2016-03-13 20:45:31 +00:00
|
|
|
use crypto::hmac::Hmac;
|
|
|
|
use crypto::mac::Mac;
|
2016-03-13 20:03:40 +00:00
|
|
|
use eventual;
|
2015-09-01 11:20:37 +00:00
|
|
|
use eventual::Future;
|
2016-03-16 04:07:04 +00:00
|
|
|
use eventual::Async;
|
2016-03-13 20:45:31 +00:00
|
|
|
use protobuf::{self, Message};
|
|
|
|
use rand::thread_rng;
|
2016-03-16 04:07:04 +00:00
|
|
|
use std::io::{Read, Write, Cursor};
|
2016-03-13 20:45:31 +00:00
|
|
|
use std::result::Result;
|
2016-01-01 23:16:12 +00:00
|
|
|
use std::sync::{Mutex, RwLock, Arc, mpsc};
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2016-05-09 11:22:51 +00:00
|
|
|
use album_cover::AlbumCover;
|
2016-03-13 20:45:31 +00:00
|
|
|
use apresolve::apresolve;
|
2016-01-01 23:16:12 +00:00
|
|
|
use audio_key::{AudioKeyManager, AudioKey, AudioKeyError};
|
2016-03-16 04:07:04 +00:00
|
|
|
use audio_file::AudioFile;
|
2016-03-13 20:45:31 +00:00
|
|
|
use authentication::Credentials;
|
2016-03-16 04:07:04 +00:00
|
|
|
use cache::Cache;
|
2016-05-09 11:22:51 +00:00
|
|
|
use connection::{self, PlainConnection, CipherConnection};
|
2016-03-13 20:45:31 +00:00
|
|
|
use diffie_hellman::DHLocalKeys;
|
2015-07-02 17:24:25 +00:00
|
|
|
use mercury::{MercuryManager, MercuryRequest, MercuryResponse};
|
|
|
|
use metadata::{MetadataManager, MetadataRef, MetadataTrait};
|
2016-03-13 20:45:31 +00:00
|
|
|
use protocol;
|
2016-05-09 11:22:51 +00:00
|
|
|
use stream::StreamManager;
|
2016-03-16 04:07:04 +00:00
|
|
|
use util::{self, SpotifyId, FileId, ReadSeek};
|
2016-03-17 03:31:57 +00:00
|
|
|
use version;
|
2016-03-13 20:45:31 +00:00
|
|
|
|
2016-05-09 11:22:51 +00:00
|
|
|
use stream;
|
|
|
|
|
2017-01-06 11:56:34 +00:00
|
|
|
#[derive(Clone, Copy)]
|
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 user_agent: String,
|
2016-01-01 23:16:12 +00:00
|
|
|
pub device_name: String,
|
2016-01-02 02:30:24 +00:00
|
|
|
pub bitrate: Bitrate,
|
2016-09-08 18:49:17 +00:00
|
|
|
pub onstart: Option<String>,
|
|
|
|
pub onstop: Option<String>,
|
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,
|
2015-12-29 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SessionInternal {
|
2016-01-26 22:34:57 +00:00
|
|
|
config: Config,
|
2016-03-17 03:06:56 +00:00
|
|
|
device_id: String,
|
2016-01-26 23:01:45 +00:00
|
|
|
data: RwLock<SessionData>,
|
2015-06-23 14:38:29 +00:00
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
cache: Box<Cache + Send + Sync>,
|
2015-07-02 17:24:25 +00:00
|
|
|
mercury: Mutex<MercuryManager>,
|
|
|
|
metadata: Mutex<MetadataManager>,
|
|
|
|
stream: Mutex<StreamManager>,
|
|
|
|
audio_key: Mutex<AudioKeyManager>,
|
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-03-16 04:07:04 +00:00
|
|
|
pub fn new(config: Config, cache: Box<Cache + Send + Sync>) -> Session {
|
2016-01-01 23:16:12 +00:00
|
|
|
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,
|
2016-03-17 03:06:56 +00:00
|
|
|
device_id: device_id,
|
2016-01-01 23:16:12 +00:00
|
|
|
data: RwLock::new(SessionData {
|
|
|
|
country: String::new(),
|
|
|
|
canonical_username: String::new(),
|
|
|
|
}),
|
|
|
|
|
|
|
|
rx_connection: Mutex::new(None),
|
|
|
|
tx_connection: Mutex::new(None),
|
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
cache: cache,
|
2016-01-01 23:16:12 +00:00
|
|
|
mercury: Mutex::new(MercuryManager::new()),
|
|
|
|
metadata: Mutex::new(MetadataManager::new()),
|
|
|
|
stream: Mutex::new(StreamManager::new()),
|
|
|
|
audio_key: Mutex::new(AudioKeyManager::new()),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-03-13 20:45:31 +00:00
|
|
|
fn connect(&self) -> CipherConnection {
|
|
|
|
let local_keys = DHLocalKeys::random(&mut thread_rng());
|
|
|
|
|
2016-12-30 11:01:43 +00:00
|
|
|
let ap = apresolve();
|
2016-03-13 20:45:31 +00:00
|
|
|
|
2016-03-24 09:31:33 +00:00
|
|
|
info!("Connecting to AP {}", ap);
|
2016-12-30 11:01:43 +00:00
|
|
|
let mut connection = PlainConnection::connect(&ap).unwrap();
|
2016-03-13 20:45:31 +00:00
|
|
|
|
|
|
|
let request = protobuf_init!(protocol::keyexchange::ClientHello::new(), {
|
|
|
|
build_info => {
|
2016-07-06 07:32:43 +00:00
|
|
|
product: protocol::keyexchange::Product::PRODUCT_PARTNER,
|
2016-03-13 20:45:31 +00:00
|
|
|
platform: protocol::keyexchange::Platform::PLATFORM_LINUX_X86,
|
|
|
|
version: 0x10800000000,
|
|
|
|
},
|
|
|
|
cryptosuites_supported => [
|
|
|
|
protocol::keyexchange::Cryptosuite::CRYPTO_SUITE_SHANNON,
|
|
|
|
],
|
|
|
|
login_crypto_hello.diffie_hellman => {
|
|
|
|
gc: local_keys.public_key(),
|
|
|
|
server_keys_known: 1,
|
|
|
|
},
|
|
|
|
client_nonce: util::rand_vec(&mut thread_rng(), 0x10),
|
|
|
|
padding: vec![0x1e],
|
|
|
|
});
|
|
|
|
|
|
|
|
let init_client_packet = connection.send_packet_prefix(&[0, 4],
|
|
|
|
&request.write_to_bytes().unwrap())
|
|
|
|
.unwrap();
|
|
|
|
let init_server_packet = connection.recv_packet().unwrap();
|
|
|
|
|
|
|
|
let response: protocol::keyexchange::APResponseMessage =
|
|
|
|
protobuf::parse_from_bytes(&init_server_packet[4..]).unwrap();
|
|
|
|
|
|
|
|
let remote_key = response.get_challenge()
|
|
|
|
.get_login_crypto_challenge()
|
|
|
|
.get_diffie_hellman()
|
|
|
|
.get_gs();
|
|
|
|
|
|
|
|
let shared_secret = local_keys.shared_secret(remote_key);
|
|
|
|
let (challenge, send_key, recv_key) = {
|
|
|
|
let mut data = Vec::with_capacity(0x64);
|
|
|
|
let mut mac = Hmac::new(Sha1::new(), &shared_secret);
|
|
|
|
|
|
|
|
for i in 1..6 {
|
|
|
|
mac.input(&init_client_packet);
|
|
|
|
mac.input(&init_server_packet);
|
|
|
|
mac.input(&[i]);
|
|
|
|
data.write(&mac.result().code()).unwrap();
|
|
|
|
mac.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
mac = Hmac::new(Sha1::new(), &data[..0x14]);
|
|
|
|
mac.input(&init_client_packet);
|
|
|
|
mac.input(&init_server_packet);
|
|
|
|
|
|
|
|
(mac.result().code().to_vec(),
|
|
|
|
data[0x14..0x34].to_vec(),
|
|
|
|
data[0x34..0x54].to_vec())
|
|
|
|
};
|
|
|
|
|
|
|
|
let packet = protobuf_init!(protocol::keyexchange::ClientResponsePlaintext::new(), {
|
|
|
|
login_crypto_response.diffie_hellman => {
|
|
|
|
hmac: challenge
|
|
|
|
},
|
|
|
|
pow_response => {},
|
|
|
|
crypto_response => {},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
connection.send_packet(&packet.write_to_bytes().unwrap()).unwrap();
|
|
|
|
|
|
|
|
CipherConnection::new(connection.into_stream(),
|
|
|
|
&send_key,
|
|
|
|
&recv_key)
|
|
|
|
}
|
|
|
|
|
2016-03-13 22:35:09 +00:00
|
|
|
pub fn login(&self, credentials: Credentials) -> Result<Credentials, ()> {
|
2016-03-13 20:45:31 +00:00
|
|
|
let packet = protobuf_init!(protocol::authentication::ClientResponseEncrypted::new(), {
|
|
|
|
login_credentials => {
|
|
|
|
username: credentials.username,
|
|
|
|
typ: credentials.auth_type,
|
|
|
|
auth_data: credentials.auth_data,
|
|
|
|
},
|
|
|
|
system_info => {
|
|
|
|
cpu_family: protocol::authentication::CpuFamily::CPU_UNKNOWN,
|
|
|
|
os: protocol::authentication::Os::OS_UNKNOWN,
|
|
|
|
system_information_string: "librespot".to_owned(),
|
2016-03-17 03:06:56 +00:00
|
|
|
device_id: self.device_id().to_owned(),
|
2016-03-13 20:45:31 +00:00
|
|
|
},
|
2016-03-17 03:31:57 +00:00
|
|
|
version_string: version::version_string(),
|
2016-03-13 20:45:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut connection = self.connect();
|
|
|
|
connection.send_packet(0xab, &packet.write_to_bytes().unwrap()).unwrap();
|
|
|
|
let (cmd, data) = connection.recv_packet().unwrap();
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
0xac => {
|
|
|
|
let welcome_data: protocol::authentication::APWelcome =
|
|
|
|
protobuf::parse_from_bytes(&data).unwrap();
|
|
|
|
|
|
|
|
let username = welcome_data.get_canonical_username().to_owned();
|
2016-03-13 22:35:09 +00:00
|
|
|
self.0.data.write().unwrap().canonical_username = username.clone();
|
2016-03-13 20:45:31 +00:00
|
|
|
*self.0.rx_connection.lock().unwrap() = Some(connection.clone());
|
|
|
|
*self.0.tx_connection.lock().unwrap() = Some(connection);
|
|
|
|
|
2016-03-24 09:31:33 +00:00
|
|
|
info!("Authenticated !");
|
2016-03-13 22:35:09 +00:00
|
|
|
|
|
|
|
let reusable_credentials = Credentials {
|
|
|
|
username: username,
|
|
|
|
auth_type: welcome_data.get_reusable_auth_credentials_type(),
|
|
|
|
auth_data: welcome_data.get_reusable_auth_credentials().to_owned(),
|
|
|
|
};
|
|
|
|
|
2016-04-24 12:29:26 +00:00
|
|
|
self.0.cache.put_credentials(&reusable_credentials);
|
|
|
|
|
2016-03-13 22:35:09 +00:00
|
|
|
Ok(reusable_credentials)
|
2016-03-13 20:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
0xad => {
|
|
|
|
let msg: protocol::keyexchange::APLoginFailed =
|
|
|
|
protobuf::parse_from_bytes(&data).unwrap();
|
2016-03-24 09:31:33 +00:00
|
|
|
error!("Authentication failed, {:?}", msg);
|
2016-03-13 20:45:31 +00:00
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
_ => {
|
2016-03-24 09:31:33 +00:00
|
|
|
error!("Unexpected message {:x}", cmd);
|
2016-03-13 20:45:31 +00:00
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
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-05-09 11:22:51 +00:00
|
|
|
0x9 | 0xa => self.0.stream.lock().unwrap().handle(cmd, data, self),
|
|
|
|
0xd | 0xe => self.0.audio_key.lock().unwrap().handle(cmd, data, self),
|
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();
|
|
|
|
}
|
2016-05-09 11:22:51 +00:00
|
|
|
0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data, self),
|
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
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
pub fn audio_key(&self, track: SpotifyId, file_id: FileId) -> Future<AudioKey, AudioKeyError> {
|
|
|
|
self.0.cache
|
|
|
|
.get_audio_key(track, file_id)
|
|
|
|
.map(Future::of)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
let self_ = self.clone();
|
|
|
|
self.0.audio_key.lock().unwrap()
|
|
|
|
.request(self, track, file_id)
|
|
|
|
.map(move |key| {
|
|
|
|
self_.0.cache.put_audio_key(track, file_id, key);
|
|
|
|
key
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn audio_file(&self, file_id: FileId) -> Box<ReadSeek> {
|
|
|
|
self.0.cache
|
|
|
|
.get_file(file_id)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
let (audio_file, complete_rx) = AudioFile::new(self, file_id);
|
|
|
|
|
|
|
|
let self_ = self.clone();
|
|
|
|
complete_rx.map(move |mut complete_file| {
|
|
|
|
self_.0.cache.put_file(file_id, &mut complete_file)
|
|
|
|
}).fire();
|
|
|
|
|
2016-05-09 11:22:51 +00:00
|
|
|
Box::new(audio_file.await().unwrap())
|
2016-03-16 04:07:04 +00:00
|
|
|
})
|
2015-07-02 17:24:25 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
pub fn album_cover(&self, file_id: FileId) -> eventual::Future<Vec<u8>, ()> {
|
|
|
|
self.0.cache
|
|
|
|
.get_file(file_id)
|
|
|
|
.map(|mut f| {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
f.read_to_end(&mut data).unwrap();
|
|
|
|
Future::of(data)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
let self_ = self.clone();
|
2016-05-09 11:22:51 +00:00
|
|
|
AlbumCover::get(file_id, self)
|
2016-03-16 04:07:04 +00:00
|
|
|
.map(move |data| {
|
|
|
|
self_.0.cache.put_file(file_id, &mut Cursor::new(&data));
|
|
|
|
data
|
|
|
|
})
|
|
|
|
})
|
2015-07-03 00:23:49 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 11:22:51 +00:00
|
|
|
pub fn stream(&self, handler: Box<stream::Handler>) {
|
|
|
|
self.0.stream.lock().unwrap().create(handler, self)
|
2016-03-13 20:03:40 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-03-16 04:07:04 +00:00
|
|
|
pub fn cache(&self) -> &Cache {
|
|
|
|
self.0.cache.as_ref()
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2016-03-17 03:06:56 +00:00
|
|
|
pub fn device_id(&self) -> &str {
|
|
|
|
&self.0.device_id
|
2016-01-26 22:34:57 +00:00
|
|
|
}
|
2015-04-25 20:32:07 +00:00
|
|
|
}
|
2016-05-09 11:22:51 +00:00
|
|
|
|
|
|
|
pub trait PacketHandler {
|
|
|
|
fn handle(&mut self, cmd: u8, data: Vec<u8>, session: &Session);
|
|
|
|
}
|