Просмотр исходного кода

p2pnet: localnet flag implemented

aggstam 3 лет назад
Родитель
Сommit
d5a4654704

+ 3 - 0
bin/darkfid/darkfid_config.toml

@@ -66,6 +66,9 @@
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+#localnet = true
+
 # Whitelisted cashier addresses
 #cashier_pub = []
 

+ 6 - 0
bin/darkfid/src/main.rs

@@ -137,6 +137,10 @@ struct Args {
     /// Prefered transports of outbound connections for the syncing protocol (repeatable flag)
     sync_p2p_transports: Vec<String>,
 
+    #[structopt(long)]
+    /// Enable localnet hosts
+    localnet: bool,
+
     #[structopt(long)]
     /// Whitelisted cashier address (repeatable flag)
     cashier_pub: Vec<String>,
@@ -307,6 +311,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
             peers: args.sync_p2p_peer.clone(),
             seeds: args.sync_p2p_seed.clone(),
             outbound_transports: net::settings::get_outbound_transports(args.sync_p2p_transports),
+            localnet: args.localnet,
             ..Default::default()
         };
 
@@ -351,6 +356,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
                 outbound_transports: net::settings::get_outbound_transports(
                     args.consensus_p2p_transports,
                 ),
+                localnet: args.localnet,
                 ..Default::default()
             };
             let p2p = net::P2p::new(consensus_network_settings).await;

+ 3 - 0
bin/faucetd/faucetd_config.toml

@@ -39,6 +39,9 @@
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+#localnet = true
+
 # Airdrop timeout limit in seconds
 #airdrop_timeout = 600
 

+ 5 - 0
bin/faucetd/src/main.rs

@@ -100,6 +100,10 @@ struct Args {
     /// Prefered transports of outbound connections for the syncing protocol (repeatable flag)
     sync_p2p_transports: Vec<String>,
 
+    #[structopt(long)]
+    /// Enable localnet hosts
+    localnet: bool,
+
     #[structopt(long)]
     /// Whitelisted cashier address (repeatable flag)
     cashier_pub: Vec<String>,
@@ -365,6 +369,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         peers: args.sync_p2p_peer.clone(),
         seeds: args.sync_p2p_seed.clone(),
         outbound_transports: net::settings::get_outbound_transports(args.sync_p2p_transports),
+        localnet: args.localnet,
         ..Default::default()
     };
 

+ 4 - 0
bin/lilith/lilith_config.toml

@@ -17,18 +17,22 @@
 #port = 33032
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."darkfid_consensus"]
 #port = 33033
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."ircd"]
 #port = 25551
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."taud"]
 #port = 23331
 #seeds = []
 #peers = []
+#localnet = true

+ 9 - 1
bin/lilith/src/config.rs

@@ -38,6 +38,8 @@ pub struct NetInfo {
     pub seeds: Vec<Url>,
     /// Connect to peers (repeatable flag)
     pub peers: Vec<Url>,
+    /// Enable localnet hosts
+    pub localnet: bool,
 }
 
 /// Parse a TOML string for any configured network and return
@@ -91,7 +93,13 @@ pub fn parse_configured_networks(data: &str) -> Result<FxHashMap<String, NetInfo
                     }
                 }
 
-                let net_info = NetInfo { port, seeds, peers };
+                let localnet = if table.contains_key("localnet") {
+                    table["localnet"].as_bool().unwrap()
+                } else {
+                    false
+                };
+
+                let net_info = NetInfo { port, seeds, peers, localnet };
                 ret.insert(name, net_info);
             }
         }

+ 1 - 0
bin/lilith/src/main.rs

@@ -134,6 +134,7 @@ async fn spawn_network(
         seeds: info.seeds,
         peers: info.peers,
         outbound_connections: 0,
+        localnet: info.localnet,
         ..Default::default()
     };
 

+ 3 - 0
contrib/localnet/darkfid0.toml

@@ -61,6 +61,9 @@ sync_p2p_seed = ["tcp://127.0.0.1:33032"]
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+localnet = true
+
 # Whitelisted faucet public key
 faucet_pub = ["1z3JuN456MieHtpoF4cr5a1ntFF87L8Z3m2E5ACppFodFcjnLS"]
 

+ 3 - 0
contrib/localnet/darkfid1.toml

@@ -66,6 +66,9 @@ sync_p2p_seed = ["tcp://127.0.0.1:33032"]
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+localnet = true
+
 # Whitelisted faucet public key
 faucet_pub = ["1z3JuN456MieHtpoF4cr5a1ntFF87L8Z3m2E5ACppFodFcjnLS"]
 

+ 3 - 0
contrib/localnet/darkfid2.toml

@@ -60,6 +60,9 @@ sync_p2p_seed = ["tcp://127.0.0.1:33032"]
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+localnet = true
+
 # Whitelisted faucet public key
 faucet_pub = ["1z3JuN456MieHtpoF4cr5a1ntFF87L8Z3m2E5ACppFodFcjnLS"]
 

+ 3 - 0
contrib/localnet/faucetd.toml

@@ -39,6 +39,9 @@ sync_p2p_seed = ["tcp://127.0.0.1:33032"]
 # Prefered transports of outbound connections for the syncing protocol
 #sync_p2p_transports = ["tls", "tcp"]
 
+# Enable localnet hosts
+localnet = true
+
 # Airdrop timeout limit in seconds
 airdrop_timeout = 10
 

+ 2 - 0
contrib/localnet/lilith_config.toml

@@ -17,11 +17,13 @@ urls = ["tcp://127.0.0.1"]
 port = 33032
 #seeds = []
 #peers = []
+localnet = true
 
 [network."darkfid_consensus"]
 port = 33033
 #seeds = []
 #peers = []
+localnet = true
 
 #[network."ircd"]
 #port = 25551

+ 3 - 0
script/research/fud/fud/fud_config.toml

@@ -29,3 +29,6 @@ seeds = ["tls://lilith0.dark.fi:13337", "tls://lilith1.dark.fi:13337"]
 
 # Prefered transports for outbound connections
 #transports = ["tls", "tcp"]
+
+# Enable localnet hosts
+#localnet = true

+ 5 - 0
script/research/fud/fud/src/main.rs

@@ -74,6 +74,10 @@ struct Args {
     #[structopt(long)]
     /// Prefered transports for outbound connections (repeatable flag)
     transports: Vec<String>,
+    
+    #[structopt(long)]
+    /// Enable localnet hosts
+    localnet: bool,
 
     #[structopt(short, parse(from_occurrences))]
     /// Increase verbosity (-vvv supported)
@@ -383,6 +387,7 @@ async fn realmain(args: Args, ex: Arc<Executor<'_>>) -> Result<()> {
         peers: args.peers.clone(),
         seeds: args.seeds.clone(),
         outbound_transports: net::settings::get_outbound_transports(args.transports),
+        localnet: args.localnet,
         ..Default::default()
     };
 

+ 3 - 0
script/research/fud/localnet/fud_config0.toml

@@ -29,3 +29,6 @@ seeds = ["tcp://127.0.0.1:13337"]
 
 # Prefered transports for outbound connections
 #transports = ["tls", "tcp"]
+
+# Enable localnet hosts
+localnet = true

+ 3 - 0
script/research/fud/localnet/fud_config1.toml

@@ -29,3 +29,6 @@ seeds = ["tcp://127.0.0.1:13337"]
 
 # Prefered transports for outbound connections
 #transports = ["tls", "tcp"]
+
+# Enable localnet hosts
+localnet = true

+ 5 - 0
script/research/fud/localnet/lilith_config.toml

@@ -17,23 +17,28 @@ url = ["tls://127.0.0.1"]
 #port = 33032
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."darkfid_consensus"]
 #port = 33033
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."ircd"]
 #port = 25551
 #seeds = []
 #peers = []
+#localnet = true
 
 #[network."taud"]
 #port = 23331
 #seeds = []
 #peers = []
+#localnet = true
 
 [network."fud"]
 port = 13337
 #seeds = []
 #peers = []
+localnet = true

+ 22 - 13
src/net/protocol/protocol_address.rs

@@ -15,6 +15,7 @@ use super::{
 };
 
 const SEND_ADDR_SLEEP_SECONDS: u64 = 900;
+const LOCALNET: [&'static str; 5] = ["localhost", "0.0.0.0", "[::]", "127.0.0.1", "[::1]"];
 
 /// Defines address and get-address messages.
 pub struct ProtocolAddress {
@@ -59,21 +60,33 @@ impl ProtocolAddress {
 
     /// Handles receiving the address message. Loops to continually recieve
     /// address messages on the address subsciption. Adds the recieved
-    /// addresses to the list of hosts.
+    /// addresses to the list of hosts, after filtering localnet hosts,
+    /// if configured to do so.
     async fn handle_receive_addrs(self: Arc<Self>) -> Result<()> {
         debug!(target: "net", "ProtocolAddress::handle_receive_addrs() [START]");
         loop {
             let addrs_msg = self.addrs_sub.receive().await?;
-
-            debug!(
-            target: "net",
-            "ProtocolAddress::handle_receive_addrs() received {} addrs",
-            addrs_msg.addrs.len()
-            );
+            debug!(target: "net", "ProtocolAddress::handle_receive_addrs() received {} addrs", addrs_msg.addrs.len());
+            let mut filtered = vec![];
             for (i, addr) in addrs_msg.addrs.iter().enumerate() {
                 debug!(target: "net", "  addr[{}]: {}", i, addr);
+                if !self.settings.localnet {
+                    match addr.host_str() {
+                        Some(host_str) => {
+                            if LOCALNET.contains(&host_str) {
+                                debug!(target: "net", "  localnet host({}) detected, ignoring", host_str);
+                                continue
+                            }
+                        }
+                        None => {
+                            debug!(target: "net", "  empty host({}) detected, ignoring...", addr);
+                            continue
+                        }
+                    }
+                }
+                filtered.push(addr.clone());
             }
-            self.hosts.store(addrs_msg.addrs.clone()).await;
+            self.hosts.store(filtered).await;
         }
     }
 
@@ -89,11 +102,7 @@ impl ProtocolAddress {
 
             // Loads the list of hosts.
             let addrs = self.hosts.load_all().await;
-            debug!(
-            target: "net",
-            "ProtocolAddress::handle_receive_get_addrs() sending {} addrs",
-            addrs.len()
-            );
+            debug!(target: "net", "ProtocolAddress::handle_receive_get_addrs() sending {} addrs", addrs.len());
             // Creates an address messages containing host address.
             let addrs_msg = message::AddrsMessage { addrs };
             // Sends the address message across the channel.

+ 8 - 0
src/net/settings.rs

@@ -27,6 +27,7 @@ pub struct Settings {
     pub node_id: String,
     pub app_version: String,
     pub outbound_transports: Vec<TransportName>,
+    pub localnet: bool,
 }
 
 impl Default for Settings {
@@ -46,6 +47,7 @@ impl Default for Settings {
             node_id: String::new(),
             app_version: option_env!("CARGO_PKG_VERSION").unwrap_or("").to_string(),
             outbound_transports: get_outbound_transports(vec![]),
+            localnet: false,
         }
     }
 }
@@ -103,6 +105,11 @@ pub struct SettingsOpt {
     #[serde(default)]
     #[structopt(long = "transports")]
     pub outbound_transports: Vec<String>,
+
+    /// Enable localnet hosts
+    #[serde(default)]
+    #[structopt(long)]
+    pub localnet: bool,
 }
 
 impl From<SettingsOpt> for Settings {
@@ -122,6 +129,7 @@ impl From<SettingsOpt> for Settings {
             node_id: settings_opt.node_id,
             app_version: settings_opt.app_version,
             outbound_transports: get_outbound_transports(settings_opt.outbound_transports),
+            localnet: settings_opt.localnet,
         }
     }
 }