11use crate :: client:: Args ;
22use crate :: client:: prettylog:: log_handshake_success;
3+ use crate :: client:: http:: SelfInfo ;
34use crate :: codec:: frame:: { Frame , HandshakeFrame , HandshakeReplyFrame , KeepAliveFrame } ;
45use crate :: crypto:: Block ;
56use crate :: network:: { create_connection, Connection , ConnectionConfig , TCPConnectionConfig } ;
67use crate :: utils;
78use std:: sync:: Arc ;
89use std:: time:: Instant ;
9- use tokio:: sync:: mpsc;
10+ use tokio:: sync:: { mpsc, RwLock } ;
1011use tokio:: time:: { Duration , interval} ;
1112
1213const OUTBOUND_BUFFER_SIZE : usize = 1000 ;
@@ -211,6 +212,9 @@ pub struct RelayHandler {
211212 inbound_tx : mpsc:: Sender < Frame > ,
212213 block : Arc < Box < dyn Block > > ,
213214 metrics : RelayStatus ,
215+ // Self information
216+ config : Option < RelayClientConfig > ,
217+ handshake_reply : Arc < RwLock < Option < HandshakeReplyFrame > > > ,
214218}
215219
216220impl RelayHandler {
@@ -222,11 +226,37 @@ impl RelayHandler {
222226 inbound_tx,
223227 block,
224228 metrics : Default :: default ( ) ,
229+ config : None ,
230+ handshake_reply : Arc :: new ( RwLock :: new ( None ) ) ,
231+ }
232+ }
233+
234+ /// Get self information
235+ pub async fn get_self_info ( & self ) -> Option < SelfInfo > {
236+ let reply_guard = self . handshake_reply . read ( ) . await ;
237+ match ( & self . config , reply_guard. as_ref ( ) ) {
238+ ( Some ( cfg) , Some ( reply) ) => {
239+ Some ( SelfInfo {
240+ identity : cfg. identity . clone ( ) ,
241+ private_ip : reply. private_ip . clone ( ) ,
242+ mask : reply. mask . clone ( ) ,
243+ gateway : reply. gateway . clone ( ) ,
244+ ciders : reply. ciders . clone ( ) ,
245+ ipv6 : cfg. ipv6 . clone ( ) ,
246+ port : cfg. port ,
247+ stun_ip : cfg. stun_ip . clone ( ) ,
248+ stun_port : cfg. stun_port ,
249+ } )
250+ }
251+ _ => None ,
225252 }
226253 }
227254
228255 pub fn run_client ( & mut self , cfg : RelayClientConfig ,
229256 on_ready : mpsc:: Sender < HandshakeReplyFrame > ) {
257+ // Store config
258+ self . config = Some ( cfg. clone ( ) ) ;
259+
230260 let ( outbound_tx, outbound_rx) = mpsc:: channel ( cfg. outbound_buffer_size ) ;
231261 let mut client = RelayClient :: new (
232262 cfg. clone ( ) ,
@@ -235,6 +265,9 @@ impl RelayHandler {
235265 self . block . clone ( ) ,
236266 ) ;
237267 self . outbound_tx = Some ( outbound_tx) ;
268+
269+ // Store handshake reply when received
270+ let handshake_reply = self . handshake_reply . clone ( ) ;
238271
239272 tokio:: spawn ( async move {
240273 loop {
@@ -258,6 +291,12 @@ impl RelayHandler {
258291
259292 tracing:: info!( "Handshake complete with {} peers" , frame. peer_details. len( ) ) ;
260293
294+ // Store handshake reply in handler
295+ {
296+ let mut guard = handshake_reply. write ( ) . await ;
297+ * guard = Some ( frame. clone ( ) ) ;
298+ }
299+
261300 if let Err ( e) = on_ready. send ( frame. clone ( ) ) . await {
262301 tracing:: error!( "on ready send fail: {}" , e) ;
263302 }
0 commit comments