librespot/src/session.rs

217 lines
7.4 KiB
Rust
Raw Normal View History

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;
2015-06-23 14:38:29 +00:00
use protobuf::{self, Message};
use rand::thread_rng;
use std::sync::{Mutex, RwLock, Arc, mpsc};
2015-07-07 21:40:31 +00:00
use std::path::PathBuf;
2015-06-23 14:38:29 +00:00
use connection::{self, PlainConnection, CipherConnection};
use keys::PrivateKeys;
use librespot_protocol as protocol;
2015-07-07 21:40:31 +00:00
use util::{SpotifyId, FileId, mkdir_existing};
use mercury::{MercuryManager, MercuryRequest, MercuryResponse};
use metadata::{MetadataManager, MetadataRef, MetadataTrait};
use stream::{StreamManager, StreamEvent};
use audio_key::{AudioKeyManager, AudioKey};
2015-07-03 00:23:49 +00:00
use audio_file::{AudioFileManager, AudioFile};
use connection::PacketHandler;
2015-04-25 20:32:07 +00:00
use util;
pub struct Config {
pub application_key: Vec<u8>,
pub user_agent: String,
pub device_id: String,
2015-07-07 21:40:31 +00:00
pub cache_location: PathBuf,
}
2015-09-01 11:20:37 +00:00
pub struct SessionData {
pub country: String,
}
pub struct SessionInternal {
2015-07-01 22:40:38 +00:00
pub config: Config,
pub data: RwLock<SessionData>,
2015-06-23 14:38:29 +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>,
rx_connection: Mutex<CipherConnection>,
tx_connection: Mutex<CipherConnection>,
2015-04-25 20:32:07 +00:00
}
2015-09-01 11:20:37 +00:00
#[derive(Clone)]
pub struct Session(pub Arc<SessionInternal>);
2015-04-25 20:32:07 +00:00
impl Session {
pub fn new(mut config: Config) -> Session {
config.device_id = {
let mut h = Sha1::new();
h.input_str(&config.device_id);
h.result_str()
};
2015-07-07 21:40:31 +00:00
mkdir_existing(&config.cache_location).unwrap();
let keys = PrivateKeys::new();
let mut connection = PlainConnection::connect().unwrap();
let request = protobuf_init!(protocol::keyexchange::ClientHello::new(), {
build_info => {
product: protocol::keyexchange::Product::PRODUCT_LIBSPOTIFY_EMBEDDED,
platform: protocol::keyexchange::Platform::PLATFORM_LINUX_X86,
version: 0x10800000000,
2015-04-25 20:32:07 +00:00
},
/*
fingerprints_supported => [
protocol::keyexchange::Fingerprint::FINGERPRINT_GRAIN
],
*/
cryptosuites_supported => [
protocol::keyexchange::Cryptosuite::CRYPTO_SUITE_SHANNON,
//protocol::keyexchange::Cryptosuite::CRYPTO_SUITE_RC4_SHA1_HMAC
],
/*
powschemes_supported => [
protocol::keyexchange::Powscheme::POW_HASH_CASH
],
*/
login_crypto_hello.diffie_hellman => {
gc: keys.public_key(),
server_keys_known: 1,
2015-04-25 20:32:07 +00:00
},
client_nonce: util::rand_vec(&mut thread_rng(), 0x10),
padding: vec![0x1e],
feature_set => {
autoupdate2: true,
}
2015-04-25 20:32:07 +00:00
});
let init_client_packet =
connection.send_packet_prefix(&[0,4], &request.write_to_bytes().unwrap()).unwrap();
2015-04-25 20:32:07 +00:00
let init_server_packet =
connection.recv_packet().unwrap();
2015-04-25 20:32:07 +00:00
let response : protocol::keyexchange::APResponseMessage =
2015-06-23 14:38:29 +00:00
protobuf::parse_from_bytes(&init_server_packet[4..]).unwrap();
2015-04-25 20:32:07 +00:00
protobuf_bind!(response, {
challenge => {
login_crypto_challenge.diffie_hellman => {
gs: remote_key,
}
}
});
2015-04-25 20:32:07 +00:00
let shared_keys = keys.add_remote_key(remote_key, &init_client_packet, &init_server_packet);
2015-04-25 20:32:07 +00:00
let packet = protobuf_init!(protocol::keyexchange::ClientResponsePlaintext::new(), {
login_crypto_response.diffie_hellman => {
hmac: shared_keys.challenge().to_vec()
2015-04-25 20:32:07 +00:00
},
pow_response => {},
crypto_response => {},
});
connection.send_packet(&packet.write_to_bytes().unwrap()).unwrap();
2015-06-23 14:38:29 +00:00
let cipher_connection = connection.setup_cipher(shared_keys);
Session(Arc::new(SessionInternal {
config: config,
data: RwLock::new(SessionData {
country: String::new(),
}),
rx_connection: Mutex::new(cipher_connection.clone()),
tx_connection: Mutex::new(cipher_connection),
mercury: Mutex::new(MercuryManager::new()),
metadata: Mutex::new(MetadataManager::new()),
stream: Mutex::new(StreamManager::new()),
audio_key: Mutex::new(AudioKeyManager::new()),
2015-07-03 00:23:49 +00:00
audio_file: Mutex::new(AudioFileManager::new()),
2015-09-01 11:20:37 +00:00
}))
}
2015-06-23 14:38:29 +00:00
pub fn login(&self, username: String, password: String) {
let packet = protobuf_init!(protocol::authentication::ClientResponseEncrypted::new(), {
login_credentials => {
username: username,
2015-07-01 23:27:19 +00:00
typ: protocol::authentication::AuthenticationType::AUTHENTICATION_USER_PASS,
auth_data: password.into_bytes(),
},
system_info => {
cpu_family: protocol::authentication::CpuFamily::CPU_UNKNOWN,
os: protocol::authentication::Os::OS_UNKNOWN,
2015-09-01 11:20:37 +00:00
system_information_string: "librespot".to_owned(),
device_id: self.0.config.device_id.clone()
2015-04-25 20:32:07 +00:00
},
version_string: util::version::version_string(),
appkey => {
2015-09-01 11:20:37 +00:00
version: self.0.config.application_key[0] as u32,
devkey: self.0.config.application_key[0x1..0x81].to_vec(),
signature: self.0.config.application_key[0x81..0x141].to_vec(),
useragent: self.0.config.user_agent.clone(),
callback_hash: vec![0; 20],
2015-04-25 20:32:07 +00:00
}
});
self.send_packet(0xab, &packet.write_to_bytes().unwrap()).unwrap();
2015-06-23 14:38:29 +00:00
}
2015-06-23 14:38:29 +00:00
pub fn poll(&self) {
let (cmd, data) =
2015-09-01 11:20:37 +00:00
self.0.rx_connection.lock().unwrap().recv_packet().unwrap();
match cmd {
0x4 => self.send_packet(0x49, &data).unwrap(),
0x4a => (),
2015-09-01 11:20:37 +00:00
0x9 => self.0.stream.lock().unwrap().handle(cmd, data),
0xd | 0xe => self.0.audio_key.lock().unwrap().handle(cmd, data),
0x1b => {
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),
0xac => eprintln!("Authentication succeedded"),
0xad => eprintln!("Authentication failed"),
2015-06-23 14:38:29 +00:00
_ => ()
}
}
pub fn send_packet(&self, cmd: u8, data: &[u8]) -> connection::Result<()> {
2015-09-01 11:20:37 +00:00
self.0.tx_connection.lock().unwrap().send_packet(cmd, data)
}
2015-09-01 11:20:37 +00:00
pub fn audio_key(&self, track: SpotifyId, file: FileId) -> Future<AudioKey, ()> {
self.0.audio_key.lock().unwrap().request(self, track, file)
}
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
}
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)
}
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-09-01 11:20:37 +00:00
pub fn mercury(&self, req: MercuryRequest) -> Future<MercuryResponse, ()> {
self.0.mercury.lock().unwrap().request(self, req)
}
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
}
}