mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_codec::Framed;
  10. use tokio_core::net::TcpStream;
  11. use tokio_core::reactor::Handle;
  12. use url::Url;
  13. use crate::authentication::Credentials;
  14. use crate::version;
  15. use crate::proxytunnel;
  16. pub type Transport = Framed<TcpStream, APCodec>;
  17. pub fn connect(
  18. addr: String,
  19. handle: &Handle,
  20. proxy: &Option<Url>,
  21. ) -> Box<dyn Future<Item = Transport, Error = io::Error>> {
  22. let (addr, connect_url) = match *proxy {
  23. Some(ref url) => {
  24. info!("Using proxy \"{}\"", url);
  25. match url.to_socket_addrs().and_then(|mut iter| {
  26. iter.next().ok_or(io::Error::new(
  27. io::ErrorKind::NotFound,
  28. "Can't resolve proxy server address",
  29. ))
  30. }) {
  31. Ok(socket_addr) => (socket_addr, Some(addr)),
  32. Err(error) => return Box::new(futures::future::err(error)),
  33. }
  34. }
  35. None => {
  36. match addr.to_socket_addrs().and_then(|mut iter| {
  37. iter.next().ok_or(io::Error::new(
  38. io::ErrorKind::NotFound,
  39. "Can't resolve server address",
  40. ))
  41. }) {
  42. Ok(socket_addr) => (socket_addr, None),
  43. Err(error) => return Box::new(futures::future::err(error)),
  44. }
  45. }
  46. };
  47. let socket = TcpStream::connect(&addr, handle);
  48. if let Some(connect_url) = connect_url {
  49. let connection = socket
  50. .and_then(move |socket| proxytunnel::connect(socket, &connect_url).and_then(handshake));
  51. Box::new(connection)
  52. } else {
  53. let connection = socket.and_then(handshake);
  54. Box::new(connection)
  55. }
  56. }
  57. pub fn authenticate(
  58. transport: Transport,
  59. credentials: Credentials,
  60. device_id: String,
  61. ) -> Box<dyn Future<Item = (Transport, Credentials), Error = io::Error>> {
  62. use crate::protocol::authentication::{APWelcome, ClientResponseEncrypted, CpuFamily, Os};
  63. use crate::protocol::keyexchange::APLoginFailed;
  64. let mut packet = ClientResponseEncrypted::new();
  65. packet
  66. .mut_login_credentials()
  67. .set_username(credentials.username);
  68. packet
  69. .mut_login_credentials()
  70. .set_typ(credentials.auth_type);
  71. packet
  72. .mut_login_credentials()
  73. .set_auth_data(credentials.auth_data);
  74. packet
  75. .mut_system_info()
  76. .set_cpu_family(CpuFamily::CPU_UNKNOWN);
  77. packet.mut_system_info().set_os(Os::OS_UNKNOWN);
  78. packet
  79. .mut_system_info()
  80. .set_system_information_string(format!(
  81. "librespot_{}_{}",
  82. version::short_sha(),
  83. version::build_id()
  84. ));
  85. packet.mut_system_info().set_device_id(device_id);
  86. packet.set_version_string(version::version_string());
  87. let cmd = 0xab;
  88. let data = packet.write_to_bytes().unwrap();
  89. Box::new(
  90. transport
  91. .send((cmd, data))
  92. .and_then(|transport| transport.into_future().map_err(|(err, _stream)| err))
  93. .and_then(|(packet, transport)| match packet {
  94. Some((0xac, data)) => {
  95. let welcome_data: APWelcome =
  96. protobuf::parse_from_bytes(data.as_ref()).unwrap();
  97. let reusable_credentials = Credentials {
  98. username: welcome_data.get_canonical_username().to_owned(),
  99. auth_type: welcome_data.get_reusable_auth_credentials_type(),
  100. auth_data: welcome_data.get_reusable_auth_credentials().to_owned(),
  101. };
  102. Ok((transport, reusable_credentials))
  103. }
  104. Some((0xad, data)) => {
  105. let error_data: APLoginFailed =
  106. protobuf::parse_from_bytes(data.as_ref()).unwrap();
  107. panic!(
  108. "Authentication failed with reason: {:?}",
  109. error_data.get_error_code()
  110. )
  111. }
  112. Some((cmd, _)) => panic!("Unexpected packet {:?}", cmd),
  113. None => panic!("EOF"),
  114. }),
  115. )
  116. }