@@ -10,7 +10,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
1010use tokio_tungstenite:: tungstenite:: http:: StatusCode ;
1111use tokio_tungstenite:: tungstenite:: Message ;
1212use tokio_tungstenite:: { connect_async, WebSocketStream } ;
13- use tracing:: { debug, error} ;
13+ use tracing:: { debug, error, trace } ;
1414
1515use super :: channel:: CableChannel ;
1616use crate :: transport:: error:: Error ;
@@ -22,6 +22,14 @@ const BASE32_CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyz234567";
2222const TLDS : & [ & str ] = & [ ".com" , ".org" , ".net" , ".info" ] ;
2323const P256_X962_LENGTH : usize = 65 ;
2424
25+ // const CABLE_PROLOGUE_STATE_ASSISTED = [0 as u8];
26+ const CABLE_PROLOGUE_QR_INITIATED : & [ u8 ] = & [ 1 as u8 ] ;
27+
28+ enum TransactionType {
29+ StateAssisted ,
30+ QRInitiated ,
31+ }
32+
2533pub fn decode_tunnel_server_domain ( encoded : u16 ) -> Option < String > {
2634 if encoded < 256 {
2735 if encoded as usize >= KNOWN_TUNNEL_DOMAINS . len ( ) {
@@ -81,7 +89,13 @@ pub async fn connect<'d>(
8189 }
8290 debug ! ( "Tunnel server returned success" ) ;
8391
84- do_handshake ( & mut ws_stream, psk, private_key) . await ?;
92+ do_handshake (
93+ & mut ws_stream,
94+ psk,
95+ private_key,
96+ TransactionType :: QRInitiated ,
97+ )
98+ . await ?;
8599 // After this, the handshake should be complete and you can start sending/receiving encrypted messages.
86100 // ...
87101
@@ -92,12 +106,20 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
92106 ws_stream : & mut WebSocketStream < T > ,
93107 psk : & [ u8 ; 32 ] ,
94108 private_key : & NonZeroScalar ,
109+ transaction_type : TransactionType ,
95110) -> Result < ( ) , Error > {
96111 let local_private_key = private_key. to_bytes ( ) ;
97- let noise_params: NoiseParams = "Noise_KNpsk0_P256_AESGCM_SHA256" . parse ( ) . unwrap ( ) ;
98- let noise_builder = Builder :: new ( noise_params)
99- . local_private_key ( & local_private_key. as_slice ( ) ) ?
100- . psk ( 0 , psk) ?;
112+
113+ let noise_builder = match transaction_type {
114+ TransactionType :: QRInitiated => Builder :: new ( "Noise_KNpsk0_P256_AESGCM_SHA256" . parse ( ) ?)
115+ . prologue ( CABLE_PROLOGUE_QR_INITIATED ) ?
116+ . local_private_key ( & local_private_key. as_slice ( ) ) ?
117+ . psk ( 0 , psk) ?,
118+ TransactionType :: StateAssisted => {
119+ // Builder::new("Noise_NKpsk0_P256_AESGCM_SHA256".parse().unwrap())
120+ todo ! ( )
121+ }
122+ } ;
101123
102124 // Build the Noise handshake as the initiator
103125 let mut noise_handshake = match noise_builder. build_initiator ( ) {
@@ -116,7 +138,7 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
116138 return Err ( Error :: Transport ( TransportError :: ConnectionFailed ) ) ;
117139 }
118140 } ;
119- debug ! (
141+ trace ! (
120142 { handshake = ?initial_msg_buffer[ ..initial_msg_len] } ,
121143 "Sending initial handshake message"
122144 ) ;
@@ -134,7 +156,12 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
134156
135157 // Read the response from the server and process it
136158 let response = match ws_stream. next ( ) . await {
137- Some ( Ok ( Message :: Binary ( response) ) ) => response,
159+ Some ( Ok ( Message :: Binary ( response) ) ) => {
160+ debug ! ( response_len = response. len( ) , "Received handshake response" ) ;
161+ trace ! ( ?response) ;
162+ response
163+ }
164+
138165 Some ( Ok ( msg) ) => {
139166 error ! ( ?msg, "Unexpected message type received" ) ;
140167 return Err ( Error :: Transport ( TransportError :: ConnectionFailed ) ) ;
@@ -161,8 +188,8 @@ async fn do_handshake<T: AsyncRead + AsyncWrite + Unpin>(
161188 return Err ( Error :: Transport ( TransportError :: ConnectionFailed ) ) ;
162189 }
163190
164- let peer_point_bytes = & response[ ..P256_X962_LENGTH ] ;
165- let ciphertext = & response[ P256_X962_LENGTH ..] ;
191+ // let peer_point_bytes = &response[..P256_X962_LENGTH];
192+ // let ciphertext = &response[P256_X962_LENGTH..];
166193
167194 let mut payload = [ 0u8 ; 1024 ] ;
168195 let payload_len = noise_handshake
0 commit comments