mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. mod codec;
  2. mod handshake;
  3. pub use self::codec::APCodec;
  4. pub use self::handshake::handshake;
  5. use futures::{Future, Sink, Stream};
  6. use protobuf::{self, Message};
  7. use std::io;
  8. use std::net::ToSocketAddrs;
  9. use tokio_core::net::TcpStream;
  10. use tokio_core::reactor::Handle;
  11. use tokio_io::codec::Framed;
  12. use url::Url;
  13. use authentication::Credentials;
  14. use version;
  15. use proxytunnel;
  16. pub type Transport = Framed<TcpStream, APCodec>;
  17. pub fn connect(
  18. addr: String,
  19. handle: &Handle,
  20. proxy: &Option<Url>,
  21. ) -> Box<Future<Item = Transport, Error = io::Error>> {
  22. let (addr, connect_url) = match *proxy {
  23. Some(ref url) => {
  24. info!("Using proxy \"{}\"", url);
  25. (url.to_socket_addrs().unwrap().next().unwrap(), Some(addr))
  26. }
  27. None => (addr.to_socket_addrs().unwrap().next().unwrap(), None),
  28. };
  29. let socket = TcpStream::connect(&addr, handle);
  30. if let Some(connect_url) = connect_url {
  31. let connection = socket
  32. .and_then(move |socket| proxytunnel::connect(socket, &connect_url).and_then(handshake));
  33. Box::new(connection)
  34. } else {
  35. let connection = socket.and_then(handshake);
  36. Box::new(connection)
  37. }
  38. }
  39. pub fn authenticate(
  40. transport: Transport,
  41. credentials: Credentials,
  42. device_id: String,
  43. ) -> Box<Future<Item = (Transport, Credentials), Error = io::Error>> {
  44. use protocol::authentication::{APWelcome, ClientResponseEncrypted, CpuFamily, Os};
  45. use protocol::keyexchange::APLoginFailed;
  46. let mut packet = ClientResponseEncrypted::new();
  47. packet.mut_login_credentials().set_username(credentials.username);
  48. packet.mut_login_credentials().set_typ(credentials.auth_type);
  49. packet
  50. .mut_login_credentials()
  51. .set_auth_data(credentials.auth_data);
  52. packet.mut_system_info().set_cpu_family(CpuFamily::CPU_UNKNOWN);
  53. packet.mut_system_info().set_os(Os::OS_UNKNOWN);
  54. packet.mut_system_info().set_system_information_string(format!(
  55. "librespot_{}_{}",
  56. version::short_sha(),
  57. version::build_id()
  58. ));
  59. packet.mut_system_info().set_device_id(device_id);
  60. packet.set_version_string(version::version_string());
  61. let cmd = 0xab;
  62. let data = packet.write_to_bytes().unwrap();
  63. Box::new(
  64. transport
  65. .send((cmd, data))
  66. .and_then(|transport| transport.into_future().map_err(|(err, _stream)| err))
  67. .and_then(|(packet, transport)| match packet {
  68. Some((0xac, data)) => {
  69. let welcome_data: APWelcome = protobuf::parse_from_bytes(data.as_ref()).unwrap();
  70. let reusable_credentials = Credentials {
  71. username: welcome_data.get_canonical_username().to_owned(),
  72. auth_type: welcome_data.get_reusable_auth_credentials_type(),
  73. auth_data: welcome_data.get_reusable_auth_credentials().to_owned(),
  74. };
  75. Ok((transport, reusable_credentials))
  76. }
  77. Some((0xad, data)) => {
  78. let error_data: APLoginFailed = protobuf::parse_from_bytes(data.as_ref()).unwrap();
  79. panic!(
  80. "Authentication failed with reason: {:?}",
  81. error_data.get_error_code()
  82. )
  83. }
  84. Some((cmd, _)) => panic!("Unexpected packet {:?}", cmd),
  85. None => panic!("EOF"),
  86. }),
  87. )
  88. }