Parcourir la source

net/ dnetview: add inbound accept_addr to get_info and display

lunar-mining il y a 4 ans
Parent
commit
ede61e628a

+ 42 - 9
bin/dnetview/src/main.rs

@@ -34,6 +34,8 @@ use dnetview::{
     view::{IdListView, NodeInfoView, View},
 };
 
+use log::debug;
+
 struct DnetView {
     name: String,
     rpc_client: RpcClient,
@@ -244,6 +246,7 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
     let mut connects: Vec<ConnectInfo> = Vec::new();
     let connections = &inbound["connected"];
     let mut connect_count = 0;
+    let mut accept_vec = Vec::new();
 
     match connections.as_object() {
         Some(connect) => {
@@ -276,11 +279,23 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
                     for k in connect.keys() {
                         let node = connect.get(k);
                         let addr = k.to_string();
-                        let id = node.unwrap().get("random_id").unwrap().as_u64().unwrap();
+                        let info = node.unwrap().as_array();
+                        // get the accept address
+                        let accept_addr = info.unwrap().get(0);
+                        let acc_addr = accept_addr
+                            .unwrap()
+                            .get("accept_addr")
+                            .unwrap()
+                            .as_str()
+                            .unwrap()
+                            .to_string();
+                        accept_vec.push(acc_addr);
+                        let info2 = info.unwrap().get(1);
+                        let id = info2.unwrap().get("random_id").unwrap().as_u64().unwrap();
                         let id = make_connect_id(&id)?;
                         let state = "state".to_string();
                         let parent = parent.clone();
-                        let msg_values = node.unwrap().get("log").unwrap().as_array().unwrap();
+                        let msg_values = info2.unwrap().get("log").unwrap().as_array().unwrap();
                         let mut msg_log: Vec<(Timestamp, String, String)> = Vec::new();
                         for msg in msg_values {
                             let msg: (Timestamp, String, String) =
@@ -289,9 +304,14 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
                         }
                         let is_empty = false;
                         let last_msg =
-                            node.unwrap().get("last_msg").unwrap().as_str().unwrap().to_string();
-                        let last_status =
-                            node.unwrap().get("last_status").unwrap().as_str().unwrap().to_string();
+                            info2.unwrap().get("last_msg").unwrap().as_str().unwrap().to_string();
+                        let last_status = info2
+                            .unwrap()
+                            .get("last_status")
+                            .unwrap()
+                            .as_str()
+                            .unwrap()
+                            .to_string();
                         let connect_info = ConnectInfo::new(
                             id,
                             addr,
@@ -308,8 +328,18 @@ async fn parse_inbound(inbound: &Value, node_id: &String) -> DnetViewResult<Sess
             }
             let is_empty = is_empty_session(&connects);
 
-            let session_info = SessionInfo::new(id, name, is_empty, parent, connects);
-            Ok(session_info)
+            // TODO: clean this up
+            if accept_vec.is_empty() {
+                let accept_addr = None;
+                let session_info =
+                    SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
+                Ok(session_info)
+            } else {
+                let accept_addr = Some(accept_vec[0].clone());
+                let session_info =
+                    SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
+                Ok(session_info)
+            }
         }
         None => Err(DnetViewError::ValueIsNotObject),
     }
@@ -336,7 +366,9 @@ async fn parse_manual(_manual: &Value, node_id: &String) -> DnetViewResult<Sessi
     connects.push(connect_info.clone());
     let parent = connect_id.clone();
     let is_empty = is_empty_session(&connects);
-    let session_info = SessionInfo::new(session_id, name, is_empty, parent, connects.clone());
+    let accept_addr = None;
+    let session_info =
+        SessionInfo::new(session_id, name, is_empty, parent, connects.clone(), accept_addr);
 
     Ok(session_info)
 }
@@ -415,7 +447,8 @@ async fn parse_outbound(outbound: &Value, node_id: &String) -> DnetViewResult<Se
 
             let is_empty = is_empty_session(&connects);
 
-            let session_info = SessionInfo::new(id, name, is_empty, parent, connects);
+            let accept_addr = None;
+            let session_info = SessionInfo::new(id, name, is_empty, parent, connects, accept_addr);
             Ok(session_info)
         }
         None => Err(DnetViewError::ValueIsNotObject),

+ 5 - 1
bin/dnetview/src/model.rs

@@ -56,11 +56,13 @@ impl NodeInfo {
 
 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
 pub struct SessionInfo {
+    // TODO: make all values optional to handle empty sessions
     pub id: String,
     pub name: String,
     pub parent: String,
     pub is_empty: bool,
     pub children: Vec<ConnectInfo>,
+    pub accept_addr: Option<String>,
 }
 
 impl SessionInfo {
@@ -70,13 +72,15 @@ impl SessionInfo {
         is_empty: bool,
         parent: String,
         children: Vec<ConnectInfo>,
+        accept_addr: Option<String>,
     ) -> SessionInfo {
-        SessionInfo { id, name, is_empty, parent, children }
+        SessionInfo { id, name, is_empty, parent, children, accept_addr }
     }
 }
 
 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
 pub struct ConnectInfo {
+    // TODO: make all values optional to handle empty connections
     pub id: String,
     pub addr: String,
     pub state: String,

+ 8 - 3
bin/dnetview/src/view.rs

@@ -201,9 +201,14 @@ impl View {
                         Span::styled(format!("External addr: {}", node.external_addr), style);
                     lines.push(Spans::from(node_info));
                 }
-                Some(SelectableObject::Session(_session)) => {
-                    //let name_span = Spans::from("Session Info");
-                    //spans.push(name_span);
+                Some(SelectableObject::Session(session)) => {
+                    if session.accept_addr.is_some() {
+                        let session_info = Span::styled(
+                            format!("Accept addr: {}", session.accept_addr.as_ref().unwrap()),
+                            style,
+                        );
+                        lines.push(Spans::from(session_info));
+                    }
                 }
                 Some(SelectableObject::Connect(connect)) => {
                     let log = self.msg_log.get(&connect.id);

+ 11 - 3
src/net/session/inbound_session.rs

@@ -138,10 +138,18 @@ impl InboundSession {
 impl Session for InboundSession {
     async fn get_info(&self) -> serde_json::Value {
         let mut infos = FxHashMap::default();
-        for (addr, info) in self.connect_infos.lock().await.iter() {
-            infos.insert(addr.to_string(), info.get_info().await);
+        match self.p2p().settings().inbound.as_ref() {
+            Some(accept_addr) => {
+                for (addr, info) in self.connect_infos.lock().await.iter() {
+                    let json_addr = json!({ "accept_addr": accept_addr });
+                    let info = vec![json_addr, info.get_info().await];
+                    infos.insert(addr.to_string(), info);
+                }
+            }
+            None => {
+                info!(target: "net", "Not configured for accepting incoming connections.");
+            }
         }
-
         json!({
             "connected": infos,
         })