Explorar el Código

do not connect to self

narodnik hace 5 años
padre
commit
7996ed0e24
Se han modificado 2 ficheros con 28 adiciones y 8 borrados
  1. 8 0
      scripts/monitor-p2p.py
  2. 20 8
      src/net/sessions/outbound_session.rs

+ 8 - 0
scripts/monitor-p2p.py

@@ -20,6 +20,8 @@ def debug(line):
     pass
 
 def process(info, line):
+    regex_listen = re.compile(
+        ".* Listening on (\d+[.]\d+[.]\d+[.]\d+:\d+)")
     regex_inbound_connect = re.compile(
         ".* Connected inbound \[(\d+[.]\d+[.]\d+[.]\d+:\d+)\]")
     regex_outbound_slots = re.compile(
@@ -43,6 +45,9 @@ def process(info, line):
         info["status"] = "p2p-run"
     elif "Not configured for accepting incoming connections." in line:
         info["inbounds"] = ["Disabled"]
+    elif (match := regex_listen.match(line)) is not None:
+        address = match.group(1)
+        info["listen"] = address
     elif (match := regex_inbound_connect.match(line)) is not None:
         address = match.group(1)
         info["inbounds"].append(address)
@@ -106,6 +111,9 @@ def table_format(ninfo):
         table_data.append([filename, "", ""])
         table_data.append(["", "status", info["status"]])
 
+        if "listen" in info:
+            table_data.append(["", "listen", info["listen"]])
+
         inbounds = info["inbounds"]
         if inbounds:
             table_data.append(["", "inbounds", inbounds[0],

+ 20 - 8
src/net/sessions/outbound_session.rs

@@ -92,15 +92,27 @@ impl OutboundSession {
 
     async fn load_address(&self, slot_number: u32) -> NetResult<SocketAddr> {
         let hosts = self.p2p().hosts();
+        let inbound_addr = self.p2p().settings().inbound;
 
-        match hosts.load_single().await {
-            Some(addr) => Ok(addr),
-            None => {
-                error!(
-                    "Hosts address pool is empty. Closing connect slot #{}",
-                    slot_number
-                );
-                Err(NetError::ServiceStopped)
+        loop {
+            match hosts.load_single().await {
+                Some(addr) => match inbound_addr {
+                    Some(inbound_addr) => {
+                        if inbound_addr != addr {
+                            return Ok(addr);
+                        }
+                    }
+                    None => {
+                        return Ok(addr);
+                    }
+                },
+                None => {
+                    error!(
+                        "Hosts address pool is empty. Closing connect slot #{}",
+                        slot_number
+                    );
+                    return Err(NetError::ServiceStopped);
+                }
             }
         }
     }