forked from plietar/librespot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery.rs
251 lines (210 loc) · 7.62 KB
/
discovery.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use base64;
use crypto::digest::Digest;
use crypto::mac::Mac;
use crypto;
use futures::sync::mpsc;
use futures::{Future, Stream, BoxFuture, Poll, Async};
use hyper::server::{Service, NewService, Request, Response, Http};
use hyper::{self, Get, Post, StatusCode};
use mdns;
use num_bigint::BigUint;
use rand;
use std::collections::BTreeMap;
use std::io;
use std::sync::Arc;
use tokio_core::net::TcpListener;
use tokio_core::reactor::Handle;
use url;
use core::diffie_hellman::{DH_GENERATOR, DH_PRIME};
use core::authentication::Credentials;
use core::util;
use core::config::ConnectConfig;
#[derive(Clone)]
struct Discovery(Arc<DiscoveryInner>);
struct DiscoveryInner {
config: ConnectConfig,
device_id: String,
private_key: BigUint,
public_key: BigUint,
tx: mpsc::UnboundedSender<Credentials>,
}
impl Discovery {
pub fn new(config: ConnectConfig, 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 {
config: config,
device_id: device_id,
private_key: private_key,
public_key: public_key,
tx: tx,
}));
(discovery, rx)
}
}
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();
let public_key = base64::encode(&public_key);
let result = json!({
"status": 101,
"statusString": "ERROR-OK",
"spotifyError": 0,
"version": "2.1.0",
"deviceID": (self.0.device_id),
"remoteName": (self.0.config.name),
"activeUser": "",
"publicKey": (public_key),
"deviceType": (self.0.config.device_type.to_string().to_uppercase()),
"libraryVersion": "0.1.0",
"accountReq": "PREMIUM",
"brandDisplayName": "librespot",
"modelDisplayName": "librespot",
});
let body = result.to_string();
::futures::finished(Response::new().with_body(body))
}
fn handle_add_user(&self, params: &BTreeMap<String, String>)
-> ::futures::Finished<Response, hyper::Error>
{
let username = params.get("userName").unwrap();
let encrypted_blob = params.get("blob").unwrap();
let client_key = params.get("clientKey").unwrap();
let encrypted_blob = base64::decode(encrypted_blob).unwrap();
let client_key = base64::decode(client_key).unwrap();
let client_key = BigUint::from_bytes_be(&client_key);
let shared_key = util::powm(&client_key, &self.0.private_key, &DH_PRIME);
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(b"checksum");
h.result().code().to_owned()
};
let encryption_key = {
let mut h = crypto::hmac::Hmac::new(crypto::sha1::Sha1::new(), &base_key);
h.input(b"encryption");
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()
};
let credentials = Credentials::with_blob(username.to_owned(), &decrypted, &self.0.device_id);
self.0.tx.send(credentials).unwrap();
let result = json!({
"status": 101,
"spotifyError": 0,
"statusString": "ERROR-OK"
});
let body = result.to_string();
::futures::finished(Response::new().with_body(body))
}
fn not_found(&self)
-> ::futures::Finished<Response, hyper::Error>
{
::futures::finished(Response::new().with_status(StatusCode::NotFound))
}
}
impl Service for Discovery {
type Request = Request;
type Response = Response;
type Error = hyper::Error;
type Future = BoxFuture<Response, hyper::Error>;
fn call(&self, request: Request) -> Self::Future {
let mut params = BTreeMap::new();
let (method, uri, _, _, body) = request.deconstruct();
if let Some(query) = uri.query() {
params.extend(url::form_urlencoded::parse(query.as_bytes()).into_owned());
}
if method != Get {
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())
}
}
pub struct DiscoveryStream {
credentials: mpsc::UnboundedReceiver<Credentials>,
_svc: mdns::Service,
task: Box<Future<Item=(), Error=io::Error>>,
}
pub fn discovery(handle: &Handle, config: ConnectConfig, device_id: String)
-> io::Result<DiscoveryStream>
{
let (discovery, creds_rx) = Discovery::new(config.clone(), device_id);
let listener = TcpListener::bind(&"0.0.0.0:0".parse().unwrap(), handle)?;
let addr = listener.local_addr()?;
let http = Http::new();
let handle_ = handle.clone();
let task = Box::new(listener.incoming().for_each(move |(socket, addr)| {
http.bind_connection(&handle_, socket, addr, discovery.clone());
Ok(())
}));
let responder = mdns::Responder::spawn(&handle)?;
let svc = responder.register(
"_spotify-connect._tcp".to_owned(),
config.name,
addr.port(),
&["VERSION=1.0", "CPath=/"]);
Ok(DiscoveryStream {
credentials: creds_rx,
_svc: svc,
task: task,
})
}
impl Stream for DiscoveryStream {
type Item = Credentials;
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.task.poll()? {
Async::Ready(()) => unreachable!(),
Async::NotReady => (),
}
Ok(self.credentials.poll().unwrap())
}
}