librespot/core/src/connection/mod.rs

96 lines
3 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;
2017-01-18 05:57:36 +00:00
use authentication::Credentials;
use version;
pub type Transport = Framed<TcpStream, APCodec>;
2018-02-11 11:37:08 +00:00
pub fn connect<A: ToSocketAddrs>(
addr: A,
handle: &Handle,
) -> Box<Future<Item = Transport, Error = io::Error>> {
2017-01-18 05:57:36 +00:00
let addr = addr.to_socket_addrs().unwrap().next().unwrap();
let socket = TcpStream::connect(&addr, handle);
2018-02-11 11:37:08 +00:00
let connection = socket.and_then(|socket| handshake(socket));
2017-01-18 05:57:36 +00:00
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
}