2016-03-17 03:06:56 +00:00
|
|
|
use crypto::digest::Digest;
|
2017-01-18 15:17:10 +00:00
|
|
|
use crypto::mac::Mac;
|
|
|
|
use crypto;
|
|
|
|
use diffie_hellman::{DH_GENERATOR, DH_PRIME};
|
|
|
|
use futures::{Future, Stream, BoxFuture};
|
|
|
|
use futures::sync::mpsc;
|
|
|
|
use hyper::{self, Get, Post, StatusCode};
|
|
|
|
use hyper::server::{Server, Service, NewService, Request, Response};
|
|
|
|
use mdns;
|
2016-03-17 03:06:56 +00:00
|
|
|
use num::BigUint;
|
|
|
|
use rand;
|
|
|
|
use rustc_serialize::base64::{self, ToBase64, FromBase64};
|
2017-01-18 15:17:10 +00:00
|
|
|
use std::io;
|
2016-03-17 03:06:56 +00:00
|
|
|
use std::collections::BTreeMap;
|
2017-01-18 15:17:10 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use url;
|
|
|
|
use tokio_core::reactor::Handle;
|
|
|
|
use std::net::SocketAddr;
|
2016-03-17 03:06:56 +00:00
|
|
|
|
|
|
|
use authentication::Credentials;
|
|
|
|
use util;
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct Discovery(Arc<DiscoveryInner>);
|
|
|
|
struct DiscoveryInner {
|
2016-03-17 03:06:56 +00:00
|
|
|
private_key: BigUint,
|
|
|
|
public_key: BigUint,
|
|
|
|
device_id: String,
|
|
|
|
device_name: String,
|
2017-01-18 15:17:10 +00:00
|
|
|
tx: mpsc::UnboundedSender<Credentials>,
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
impl Discovery {
|
|
|
|
pub fn new(device_name: String, device_id: String)
|
|
|
|
-> (Discovery, mpsc::UnboundedReceiver<Credentials>)
|
|
|
|
{
|
|
|
|
let (tx, rx) = mpsc::unbounded();
|
|
|
|
|
|
|
|
let key_data = util::rand_vec(&mut rand::thread_rng(), 95);
|
|
|
|
let private_key = BigUint::from_bytes_be(&key_data);
|
|
|
|
let public_key = util::powm(&DH_GENERATOR, &private_key, &DH_PRIME);
|
|
|
|
|
|
|
|
let discovery = Discovery(Arc::new(DiscoveryInner {
|
|
|
|
device_name: device_name.to_owned(),
|
|
|
|
device_id: device_id.to_owned(),
|
|
|
|
private_key: private_key,
|
|
|
|
public_key: public_key,
|
|
|
|
tx: tx,
|
|
|
|
}));
|
|
|
|
|
|
|
|
(discovery, rx)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serve(&self, addr: &SocketAddr, handle: &Handle)
|
|
|
|
-> hyper::Result<SocketAddr>
|
|
|
|
{
|
|
|
|
let server = Server::http(&addr, handle)?;
|
|
|
|
server.handle(self.clone(), handle)
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
impl Discovery {
|
|
|
|
fn handle_get_info(&self, _params: &BTreeMap<String, String>)
|
|
|
|
-> ::futures::Finished<Response, hyper::Error>
|
|
|
|
{
|
|
|
|
let public_key = self.0.public_key.to_bytes_be()
|
2016-03-17 03:06:56 +00:00
|
|
|
.to_base64(base64::STANDARD);
|
|
|
|
|
|
|
|
let result = json!({
|
|
|
|
"status": 101,
|
|
|
|
"statusString": "ERROR-OK",
|
|
|
|
"spotifyError": 0,
|
|
|
|
"version": "2.1.0",
|
2017-01-18 15:17:10 +00:00
|
|
|
"deviceID": (self.0.device_id),
|
|
|
|
"remoteName": (self.0.device_name),
|
2016-03-17 03:06:56 +00:00
|
|
|
"activeUser": "",
|
|
|
|
"publicKey": (public_key),
|
|
|
|
"deviceType": "UNKNOWN",
|
|
|
|
"libraryVersion": "0.1.0",
|
|
|
|
"accountReq": "PREMIUM",
|
|
|
|
"brandDisplayName": "librespot",
|
|
|
|
"modelDisplayName": "librespot",
|
|
|
|
});
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let body = result.to_string();
|
|
|
|
::futures::finished(Response::new().with_body(body))
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
fn handle_add_user(&self, params: &BTreeMap<String, String>)
|
|
|
|
-> ::futures::Finished<Response, hyper::Error>
|
|
|
|
{
|
2016-03-17 03:06:56 +00:00
|
|
|
let username = params.get("userName").unwrap();
|
|
|
|
let encrypted_blob = params.get("blob").unwrap();
|
|
|
|
let client_key = params.get("clientKey").unwrap();
|
|
|
|
|
|
|
|
let encrypted_blob = encrypted_blob.from_base64().unwrap();
|
|
|
|
|
|
|
|
let client_key = client_key.from_base64().unwrap();
|
|
|
|
let client_key = BigUint::from_bytes_be(&client_key);
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let shared_key = util::powm(&client_key, &self.0.private_key, &DH_PRIME);
|
2016-03-17 03:06:56 +00:00
|
|
|
|
|
|
|
let iv = &encrypted_blob[0..16];
|
|
|
|
let encrypted = &encrypted_blob[16..encrypted_blob.len() - 20];
|
|
|
|
let cksum = &encrypted_blob[encrypted_blob.len() - 20..encrypted_blob.len()];
|
|
|
|
|
|
|
|
let base_key = {
|
|
|
|
let mut data = [0u8; 20];
|
|
|
|
let mut h = crypto::sha1::Sha1::new();
|
|
|
|
h.input(&shared_key.to_bytes_be());
|
|
|
|
h.result(&mut data);
|
|
|
|
data[..16].to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
let checksum_key = {
|
|
|
|
let mut h = crypto::hmac::Hmac::new(crypto::sha1::Sha1::new(), &base_key);
|
|
|
|
h.input("checksum".as_bytes());
|
|
|
|
h.result().code().to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
let encryption_key = {
|
|
|
|
let mut h = crypto::hmac::Hmac::new(crypto::sha1::Sha1::new(), &base_key);
|
|
|
|
h.input("encryption".as_bytes());
|
|
|
|
h.result().code().to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mac = {
|
|
|
|
let mut h = crypto::hmac::Hmac::new(crypto::sha1::Sha1::new(), &checksum_key);
|
|
|
|
h.input(encrypted);
|
|
|
|
h.result().code().to_owned()
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(&mac[..], cksum);
|
|
|
|
|
|
|
|
let decrypted = {
|
|
|
|
let mut data = vec![0u8; encrypted.len()];
|
|
|
|
let mut cipher = crypto::aes::ctr(crypto::aes::KeySize::KeySize128,
|
|
|
|
&encryption_key[0..16],
|
|
|
|
&iv);
|
|
|
|
cipher.process(&encrypted, &mut data);
|
|
|
|
String::from_utf8(data).unwrap()
|
|
|
|
};
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let credentials = Credentials::with_blob(username.to_owned(), &decrypted, &self.0.device_id);
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
self.0.tx.send(credentials).unwrap();
|
2016-03-17 03:06:56 +00:00
|
|
|
|
|
|
|
let result = json!({
|
|
|
|
"status": 101,
|
|
|
|
"spotifyError": 0,
|
|
|
|
"statusString": "ERROR-OK"
|
|
|
|
});
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let body = result.to_string();
|
|
|
|
::futures::finished(Response::new().with_body(body))
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
fn not_found(&self)
|
|
|
|
-> ::futures::Finished<Response, hyper::Error>
|
|
|
|
{
|
|
|
|
::futures::finished(Response::new().with_status(StatusCode::NotFound))
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
impl Service for Discovery {
|
|
|
|
type Request = Request;
|
|
|
|
type Response = Response;
|
|
|
|
type Error = hyper::Error;
|
|
|
|
type Future = BoxFuture<Response, hyper::Error>;
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
fn call(&self, request: Request) -> Self::Future {
|
|
|
|
let mut params = BTreeMap::new();
|
2016-04-24 11:15:53 +00:00
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let (method, uri, _, _, body) = request.deconstruct();
|
|
|
|
if let Some(query) = uri.query() {
|
|
|
|
params.extend(url::form_urlencoded::parse(query.as_bytes()).into_owned());
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
2017-01-18 15:17:10 +00:00
|
|
|
|
|
|
|
debug!("{:?} {:?} {:?}", method, uri.path(), params);
|
|
|
|
|
|
|
|
let this = self.clone();
|
|
|
|
body.fold(Vec::new(), |mut acc, chunk| {
|
|
|
|
acc.extend_from_slice(chunk.as_ref());
|
|
|
|
Ok::<_, hyper::Error>(acc)
|
|
|
|
}).map(move |body| {
|
|
|
|
params.extend(url::form_urlencoded::parse(&body).into_owned());
|
|
|
|
params
|
|
|
|
}).and_then(move |params| {
|
|
|
|
match (method, params.get("action").map(AsRef::as_ref)) {
|
|
|
|
(Get, Some("getInfo")) => this.handle_get_info(¶ms),
|
|
|
|
(Post, Some("addUser")) => this.handle_add_user(¶ms),
|
|
|
|
_ => this.not_found(),
|
|
|
|
}
|
|
|
|
}).boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NewService for Discovery {
|
|
|
|
type Request = Request;
|
|
|
|
type Response = Response;
|
|
|
|
type Error = hyper::Error;
|
|
|
|
type Instance = Self;
|
|
|
|
|
|
|
|
fn new_service(&self) -> io::Result<Self::Instance> {
|
|
|
|
Ok(self.clone())
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
pub fn discovery_login<A,B>(device_name: A, device_id: B) -> Result<Credentials, ()>
|
|
|
|
where A: Into<String>,
|
|
|
|
B: Into<String>
|
|
|
|
{
|
|
|
|
let device_name = device_name.into();
|
|
|
|
let device_id = device_id.into();
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
let (discovery, creds_rx) = Discovery::new(device_name.clone(), device_id);
|
|
|
|
|
|
|
|
let creds_rx = creds_rx.into_future()
|
|
|
|
.map(move |(creds, _)| creds.unwrap()).map_err(|(e, _)| e);
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-18 15:17:10 +00:00
|
|
|
let addr = "0.0.0.0:0".parse().unwrap();
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
let mut core = Core::new().unwrap();
|
|
|
|
let handle = core.handle();
|
|
|
|
let listening_addr = discovery.serve(&addr, &handle).unwrap();
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
let responder = mdns::Responder::spawn(&handle).unwrap();
|
|
|
|
let _svc = responder.register(
|
|
|
|
"_spotify-connect._tcp".to_owned(),
|
|
|
|
device_name,
|
|
|
|
listening_addr.port(),
|
|
|
|
&["VERSION=1.0", "CPath=/"]);
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
let creds = core.run(creds_rx).unwrap();
|
2016-03-17 03:06:56 +00:00
|
|
|
|
2017-01-20 02:37:02 +00:00
|
|
|
Ok(creds)
|
2016-03-17 03:06:56 +00:00
|
|
|
}
|