librespot/core/src/connection/mod.rs

104 lines
3.4 KiB
Rust
Raw Normal View History

2017-01-18 05:57:36 +00:00
mod codec;
mod handshake;
pub use self::codec::APCodec;
pub use self::handshake::handshake;
use futures::{Future, Sink, Stream};
2018-02-11 11:37:08 +00:00
use protobuf::{self, Message};
2017-01-18 05:57:36 +00:00
use std::io;
use std::net::ToSocketAddrs;
use tokio_core::net::TcpStream;
use tokio_core::reactor::Handle;
use tokio_io::codec::Framed;
use url::Url;
2017-01-18 05:57:36 +00:00
use authentication::Credentials;
use version;
use proxytunnel;
2017-01-18 05:57:36 +00:00
pub type Transport = Framed<TcpStream, APCodec>;
2018-03-23 15:52:24 +00:00
pub fn connect(
addr: String,
2018-02-11 11:37:08 +00:00
handle: &Handle,
proxy: &Option<Url>,
2018-02-11 11:37:08 +00:00
) -> Box<Future<Item = Transport, Error = io::Error>> {
let (addr, connect_url) = match *proxy {
Some(ref url) => {
info!("Using proxy \"{}\"", url);
(url.to_socket_addrs().unwrap().next().unwrap(), Some(addr))
}
None => (addr.to_socket_addrs().unwrap().next().unwrap(), None),
};
let socket = TcpStream::connect(&addr, handle);
if let Some(connect_url) = connect_url {
2018-03-23 15:52:24 +00:00
let connection = socket
.and_then(move |socket| proxytunnel::connect(socket, &connect_url).and_then(handshake));
Box::new(connection)
} else {
let connection = socket.and_then(handshake);
Box::new(connection)
}
2017-01-18 05:57:36 +00:00
}
2018-02-11 11:37:08 +00:00
pub fn authenticate(
transport: Transport,
credentials: Credentials,
device_id: String,
) -> Box<Future<Item = (Transport, Credentials), Error = io::Error>> {
2017-01-18 05:57:36 +00:00
use protocol::authentication::{APWelcome, ClientResponseEncrypted, CpuFamily, Os};
2018-02-13 14:29:01 +00:00
use protocol::keyexchange::APLoginFailed;
2017-01-18 05:57:36 +00:00
let mut packet = ClientResponseEncrypted::new();
packet.mut_login_credentials().set_username(credentials.username);
packet.mut_login_credentials().set_typ(credentials.auth_type);
packet
.mut_login_credentials()
.set_auth_data(credentials.auth_data);
packet.mut_system_info().set_cpu_family(CpuFamily::CPU_UNKNOWN);
packet.mut_system_info().set_os(Os::OS_UNKNOWN);
packet.mut_system_info().set_system_information_string(format!(
"librespot_{}_{}",
version::short_sha(),
version::build_id()
));
packet.mut_system_info().set_device_id(device_id);
packet.set_version_string(version::version_string());
2017-01-18 05:57:36 +00:00
let cmd = 0xab;
let data = packet.write_to_bytes().unwrap();
2018-02-11 11:37:08 +00:00
Box::new(
transport
.send((cmd, data))
.and_then(|transport| transport.into_future().map_err(|(err, _stream)| err))
.and_then(|(packet, transport)| match packet {
Some((0xac, data)) => {
let welcome_data: APWelcome = protobuf::parse_from_bytes(data.as_ref()).unwrap();
2017-01-18 05:57:36 +00:00
2018-02-11 11:37:08 +00:00
let reusable_credentials = Credentials {
username: welcome_data.get_canonical_username().to_owned(),
auth_type: welcome_data.get_reusable_auth_credentials_type(),
auth_data: welcome_data.get_reusable_auth_credentials().to_owned(),
};
2017-01-18 05:57:36 +00:00
2018-02-11 11:37:08 +00:00
Ok((transport, reusable_credentials))
}
2017-01-18 05:57:36 +00:00
2018-02-13 14:29:01 +00:00
Some((0xad, data)) => {
let error_data: APLoginFailed = protobuf::parse_from_bytes(data.as_ref()).unwrap();
2018-02-14 04:50:51 +00:00
panic!(
"Authentication failed with reason: {:?}",
error_data.get_error_code()
)
2018-02-13 14:29:01 +00:00
}
2018-02-11 11:37:08 +00:00
Some((cmd, _)) => panic!("Unexpected packet {:?}", cmd),
None => panic!("EOF"),
}),
)
2017-01-18 05:57:36 +00:00
}