@@ -4,7 +4,7 @@ use std::{
44 convert:: Infallible ,
55 net:: { Ipv4Addr , SocketAddr , TcpListener } ,
66 pin:: Pin ,
7- sync:: Arc ,
7+ sync:: { Arc , Mutex } ,
88 task:: { Context , Poll } ,
99} ;
1010
@@ -45,7 +45,7 @@ pub struct Config {
4545
4646struct RequestHandler {
4747 config : Config ,
48- username : Option < String > ,
48+ username : Mutex < Option < String > > ,
4949 keys : DhLocalKeys ,
5050 tx : mpsc:: UnboundedSender < Credentials > ,
5151}
@@ -56,21 +56,28 @@ impl RequestHandler {
5656
5757 let discovery = Self {
5858 config,
59- username : None ,
59+ username : Mutex :: new ( None ) ,
6060 keys : DhLocalKeys :: random ( & mut rand:: thread_rng ( ) ) ,
6161 tx,
6262 } ;
6363
6464 ( discovery, rx)
6565 }
6666
67+ fn active_user ( & self ) -> String {
68+ if let Ok ( maybe_username) = self . username . lock ( ) {
69+ maybe_username. clone ( ) . unwrap_or ( String :: new ( ) )
70+ } else {
71+ warn ! ( "username lock corrupted; read failed" ) ;
72+ String :: from ( "!" )
73+ }
74+ }
75+
6776 fn handle_get_info ( & self ) -> Response < Full < Bytes > > {
6877 let public_key = BASE64 . encode ( self . keys . public_key ( ) ) ;
6978 let device_type: & str = self . config . device_type . into ( ) ;
70- let mut active_user = String :: new ( ) ;
71- if let Some ( username) = & self . username {
72- active_user = username. to_string ( ) ;
73- }
79+ let active_user = self . active_user ( ) ;
80+
7481 // options based on zeroconf guide, search for `groupStatus` on page
7582 let group_status = if self . config . is_group {
7683 "GROUP"
@@ -193,7 +200,15 @@ impl RequestHandler {
193200
194201 let credentials = Credentials :: with_blob ( username, decrypted, & self . config . device_id ) ?;
195202
196- self . tx . send ( credentials) ?;
203+ {
204+ let maybe_username = self . username . lock ( ) ;
205+ self . tx . send ( credentials) ?;
206+ if let Ok ( mut username_field) = maybe_username {
207+ * username_field = Some ( String :: from ( username) ) ;
208+ } else {
209+ warn ! ( "username lock corrupted; write failed" ) ;
210+ }
211+ }
197212
198213 let result = json ! ( {
199214 "status" : 101 ,
0 commit comments