@@ -21,13 +21,12 @@ use crate::{
2121 shannon_codec:: { ShannonDecoder , ShannonEncoder , ShannonMsg } ,
2222 } ,
2323 error:: Error ,
24- protocol:: authentication:: AuthenticationType ,
25- util:: {
26- default_ureq_agent_builder, deserialize_protobuf, serialize_protobuf, NET_CONNECT_TIMEOUT ,
27- NET_IO_TIMEOUT ,
28- } ,
24+ util:: { default_ureq_agent_builder, NET_CONNECT_TIMEOUT , NET_IO_TIMEOUT } ,
2925} ;
3026
27+ use librespot_protocol:: authentication:: AuthenticationType ;
28+ use protobuf:: { Enum , Message , MessageField , SpecialFields } ;
29+
3130// Device ID used for authentication message.
3231const DEVICE_ID : & str = "Psst" ;
3332
@@ -76,7 +75,7 @@ impl From<SerializedCredentials> for Credentials {
7675 Self {
7776 username : Some ( value. username ) ,
7877 auth_data : value. auth_data . into_bytes ( ) ,
79- auth_type : value. auth_type . into ( ) ,
78+ auth_type : AuthenticationType :: from_i32 ( value. auth_type ) . unwrap_or_default ( ) ,
8079 }
8180 }
8281}
@@ -215,7 +214,7 @@ impl Transport {
215214 }
216215
217216 pub fn exchange_keys ( mut stream : TcpStream ) -> Result < Self , Error > {
218- use crate :: protocol :: keyexchange:: APResponseMessage ;
217+ use librespot_protocol :: keyexchange:: APResponseMessage ;
219218
220219 let local_keys = DHLocalKeys :: random ( ) ;
221220
@@ -232,17 +231,18 @@ impl Transport {
232231 // hashed together with the shared secret to make a key pair).
233232 log:: trace!( "waiting for AP response" ) ;
234233 let apresp_packet = read_packet ( & mut stream) ?;
235- let apresp: APResponseMessage = deserialize_protobuf ( & apresp_packet[ 4 ..] ) ?;
234+ let apresp = APResponseMessage :: parse_from_bytes ( & apresp_packet[ 4 ..] ) ?;
236235 log:: trace!( "received AP response" ) ;
237236
238237 // Compute the challenge response and the sending/receiving keys.
239- let remote_key = & apresp
238+ let remote_key = apresp
240239 . challenge
241- . expect ( "Missing data" )
242240 . login_crypto_challenge
243241 . diffie_hellman
244- . expect ( "Missing data" )
245- . gs ;
242+ . gs
243+ . as_ref ( )
244+ . expect ( "Missing data" ) ;
245+
246246 let ( challenge, send_key, recv_key) = compute_keys (
247247 & local_keys. shared_secret ( remote_key) ,
248248 & hello_packet,
@@ -268,7 +268,7 @@ impl Transport {
268268 }
269269
270270 pub fn authenticate ( & mut self , credentials : Credentials ) -> Result < Credentials , Error > {
271- use crate :: protocol :: { authentication:: APWelcome , keyexchange:: APLoginFailed } ;
271+ use librespot_protocol :: { authentication:: APWelcome , keyexchange:: APLoginFailed } ;
272272
273273 // Send a login request with the client credentials.
274274 let request = client_response_encrypted ( credentials) ;
@@ -279,19 +279,20 @@ impl Transport {
279279
280280 match response. cmd {
281281 ShannonMsg :: AP_WELCOME => {
282- let welcome_data: APWelcome =
283- deserialize_protobuf ( & response. payload ) . expect ( "Missing data" ) ;
282+ let welcome_data =
283+ APWelcome :: parse_from_bytes ( & response. payload ) . expect ( "Missing data" ) ;
284+
284285 Ok ( Credentials {
285- username : Some ( welcome_data. canonical_username ) ,
286- auth_data : welcome_data. reusable_auth_credentials ,
287- auth_type : welcome_data. reusable_auth_credentials_type ,
286+ username : Some ( welcome_data. canonical_username ( ) . to_string ( ) ) ,
287+ auth_data : welcome_data. reusable_auth_credentials ( ) . to_vec ( ) ,
288+ auth_type : welcome_data. reusable_auth_credentials_type ( ) ,
288289 } )
289290 }
290291 ShannonMsg :: AUTH_FAILURE => {
291- let error_data: APLoginFailed =
292- deserialize_protobuf ( & response. payload ) . expect ( "Missing data" ) ;
292+ let error_data =
293+ APLoginFailed :: parse_from_bytes ( & response. payload ) . expect ( "Missing data" ) ;
293294 Err ( Error :: AuthFailed {
294- code : error_data. error_code as _ ,
295+ code : error_data. error_code ( ) as _ ,
295296 } )
296297 }
297298 _ => {
@@ -321,44 +322,57 @@ fn make_packet(prefix: &[u8], data: &[u8]) -> Vec<u8> {
321322}
322323
323324fn client_hello ( public_key : Vec < u8 > , nonce : Vec < u8 > ) -> Vec < u8 > {
324- use crate :: protocol :: keyexchange:: * ;
325+ use librespot_protocol :: keyexchange:: * ;
325326
326327 let hello = ClientHello {
327- build_info : BuildInfo {
328- platform : Platform :: PLATFORM_LINUX_X86 ,
329- product : Product :: PRODUCT_PARTNER ,
328+ build_info : MessageField :: some ( BuildInfo {
329+ platform : Some ( Platform :: PLATFORM_LINUX_X86 . into ( ) ) ,
330+ product : Some ( Product :: PRODUCT_PARTNER . into ( ) ) ,
330331 product_flags : vec ! [ ] ,
331- version : 109_800_078 ,
332- } ,
333- cryptosuites_supported : vec ! [ Cryptosuite :: CRYPTO_SUITE_SHANNON ] ,
332+ version : Some ( 109_800_078 ) ,
333+ special_fields : SpecialFields :: new ( ) ,
334+ } ) ,
335+ cryptosuites_supported : vec ! [ Cryptosuite :: CRYPTO_SUITE_SHANNON . into( ) ] ,
334336 fingerprints_supported : vec ! [ ] ,
335337 powschemes_supported : vec ! [ ] ,
336- login_crypto_hello : LoginCryptoHelloUnion {
337- diffie_hellman : Some ( LoginCryptoDiffieHellmanHello {
338- gc : public_key,
339- server_keys_known : 1 ,
338+ login_crypto_hello : MessageField :: some ( LoginCryptoHelloUnion {
339+ diffie_hellman : MessageField :: some ( LoginCryptoDiffieHellmanHello {
340+ gc : Some ( public_key) ,
341+ server_keys_known : Some ( 1 ) ,
342+ special_fields : SpecialFields :: new ( ) ,
340343 } ) ,
341- } ,
342- client_nonce : nonce,
344+ special_fields : SpecialFields :: new ( ) ,
345+ } ) ,
346+ client_nonce : Some ( nonce) ,
343347 padding : Some ( vec ! [ 0x1e ] ) ,
344- feature_set : None ,
348+ feature_set : None . into ( ) ,
349+ special_fields : SpecialFields :: new ( ) ,
345350 } ;
346351
347- serialize_protobuf ( & hello) . expect ( "Failed to serialize" )
352+ hello
353+ . write_to_bytes ( )
354+ . expect ( "Failed to serialize client hello" )
348355}
349356
350357fn client_response_plaintext ( challenge : Vec < u8 > ) -> Vec < u8 > {
351- use crate :: protocol :: keyexchange:: * ;
358+ use librespot_protocol :: keyexchange:: * ;
352359
353360 let response = ClientResponsePlaintext {
354- login_crypto_response : LoginCryptoResponseUnion {
355- diffie_hellman : Some ( LoginCryptoDiffieHellmanResponse { hmac : challenge } ) ,
356- } ,
357- pow_response : PoWResponseUnion :: default ( ) ,
358- crypto_response : CryptoResponseUnion :: default ( ) ,
361+ login_crypto_response : MessageField :: some ( LoginCryptoResponseUnion {
362+ diffie_hellman : MessageField :: some ( LoginCryptoDiffieHellmanResponse {
363+ hmac : Some ( challenge) ,
364+ special_fields : SpecialFields :: new ( ) ,
365+ } ) ,
366+ special_fields : SpecialFields :: new ( ) ,
367+ } ) ,
368+ pow_response : MessageField :: some ( PoWResponseUnion :: default ( ) ) ,
369+ crypto_response : MessageField :: some ( CryptoResponseUnion :: default ( ) ) ,
370+ special_fields : SpecialFields :: new ( ) ,
359371 } ;
360372
361- serialize_protobuf ( & response) . expect ( "Failed to serialize" )
373+ response
374+ . write_to_bytes ( )
375+ . expect ( "Failed to serialize client response" )
362376}
363377
364378fn compute_keys (
@@ -389,22 +403,27 @@ fn compute_keys(
389403}
390404
391405fn client_response_encrypted ( credentials : Credentials ) -> ShannonMsg {
392- use crate :: protocol:: authentication:: { ClientResponseEncrypted , LoginCredentials , SystemInfo } ;
406+ use librespot_protocol:: authentication:: {
407+ ClientResponseEncrypted , LoginCredentials , SystemInfo , Os , CpuFamily
408+ } ;
393409
394410 let response = ClientResponseEncrypted {
395- login_credentials : LoginCredentials {
411+ login_credentials : MessageField :: some ( LoginCredentials {
396412 username : credentials. username ,
397413 auth_data : Some ( credentials. auth_data ) ,
398- typ : credentials. auth_type ,
399- } ,
400- system_info : SystemInfo {
414+ typ : Some ( credentials. auth_type . into ( ) ) ,
415+ special_fields : SpecialFields :: new ( ) ,
416+ } ) ,
417+ system_info : MessageField :: some ( SystemInfo {
401418 device_id : Some ( DEVICE_ID . to_string ( ) ) ,
402419 system_information_string : Some ( "librespot_but_actually_psst" . to_string ( ) ) ,
420+ os : Some ( Os :: default ( ) . into ( ) ) ,
421+ cpu_family : Some ( CpuFamily :: default ( ) . into ( ) ) ,
403422 ..SystemInfo :: default ( )
404- } ,
423+ } ) ,
405424 ..ClientResponseEncrypted :: default ( )
406425 } ;
407426
408- let buf = serialize_protobuf ( & response) . expect ( "Failed to serialize" ) ;
427+ let buf = response. write_to_bytes ( ) . expect ( "Failed to serialize" ) ;
409428 ShannonMsg :: new ( ShannonMsg :: LOGIN , buf)
410429}
0 commit comments