Explorar o código

bin: set unique magic bytes for each seprate p2p network

- add magic bytes to p2p net configs for bins
- hardcode darkirc and fud magic bytes in bin/app darkirc and fud plugins
- make lilith use the separate magic bytes for different p2p networks
oars hai 1 ano
pai
achega
fcf0571a1a

+ 2 - 1
bin/app/src/plugin/darkirc.rs

@@ -37,7 +37,7 @@ use std::{
     sync::{Arc, Mutex as SyncMutex, OnceLock, Weak},
     time::UNIX_EPOCH,
 };
-
+use darkfi::net::settings::MagicBytes;
 use crate::{
     error::{Error, Result},
     prop::{BatchGuardPtr, PropertyAtomicGuard, PropertyStr, Role},
@@ -200,6 +200,7 @@ impl DarkIrc {
         let settings = PluginSettings { setting_root, sled_tree: setting_tree };
 
         let mut p2p_settings: NetSettings = Default::default();
+        p2p_settings.magic_bytes = MagicBytes([251, 229, 199, 181]);
         p2p_settings.app_version = semver::Version::parse("0.5.0").unwrap();
         if get_use_tor_filename().exists() {
             i!("Setup P2P network [tor]");

+ 2 - 1
bin/app/src/plugin/fud.rs

@@ -152,7 +152,8 @@ impl FudPlugin {
 
         let mut fud_settings: FudSettings = Default::default();
         fud_settings.base_dir = basedir.to_string_lossy().to_string();
-        let mut p2p_settings: NetSettings = fud_settings.net.clone().into();
+        let mut p2p_settings: NetSettings = Default::default();
+        p2p_settings.magic_bytes = MagicBytes([73, 59, 41, 23]);
         p2p_settings.app_version = semver::Version::parse("0.5.0").unwrap();
         if get_use_tor_filename().exists() {
             i!("Setup P2P network [tor]");

+ 9 - 0
bin/darkfid/darkfid_config.toml

@@ -61,6 +61,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Testnet P2P network settings
 [network_config."testnet".net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [163, 139, 113, 101]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/darkfid/testnet"
 
@@ -192,6 +195,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Mainnet P2P network settings
 [network_config."mainnet".net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [101, 79, 61, 43]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/darkfid/mainnet"
 
@@ -328,6 +334,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Localnet P2P network settings
 [network_config."localnet".net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [241, 227, 197, 179]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/darkfid/localnet"
 

+ 3 - 0
bin/darkirc/config/darkirc-clearnet.toml

@@ -11,6 +11,9 @@ autojoin = [
 
 ## P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## Path to the P2P datastore
 datastore = "~/.local/share/darkfi/darkirc"
 

+ 3 - 0
bin/darkirc/config/darkirc-i2p.toml

@@ -11,6 +11,9 @@ autojoin = [
 
 ## P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## Path to the P2P datastore
 datastore = "~/.local/share/darkfi/darkirc"
 

+ 3 - 0
bin/darkirc/config/darkirc-mixed.toml

@@ -11,6 +11,9 @@ autojoin = [
 
 ## P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## Path to the P2P datastore
 datastore = "~/.local/share/darkfi/darkirc"
 

+ 3 - 0
bin/darkirc/config/darkirc-tor.toml

@@ -11,6 +11,9 @@ autojoin = [
 
 ## P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## Path to the P2P datastore
 datastore = "~/.local/share/darkfi/darkirc"
 

+ 3 - 0
bin/darkirc/config/inbound.toml

@@ -14,6 +14,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Raft net settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## P2P accept addresses
 inbound = ["127.0.0.1:11002"]
 

+ 3 - 0
bin/darkirc/config/outbound.toml

@@ -14,6 +14,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Raft net settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## P2P accept addresses
 # inbound=["127.0.0.1:11002"]
 

+ 3 - 0
bin/darkirc/config/seed.toml

@@ -14,6 +14,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 ## Raft net settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 ## P2P accept addresses
 inbound = ["127.0.0.1:11001"]
 

+ 3 - 0
bin/darkirc/darkirc_config.toml

@@ -52,6 +52,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 # P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [251, 229, 199, 181]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/darkirc"
 

+ 3 - 0
bin/fud/fud/fud_config.toml

@@ -67,6 +67,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 # P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [73, 59, 41, 23]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/fud"
 

+ 3 - 0
bin/lilith/lilith_config.toml

@@ -23,6 +23,7 @@ rpc_listen = "tcp://127.0.0.1:18927"
 #localnet = false
 #hostlist ="~/.local/share/darkfi/lilith/darkfid_sync/hostlist.tsv"
 #datastore = "~/.local/share/darkfi/lilith/darkfid_sync"
+#magic_bytes = [163, 139, 113, 101]
 
 #[network."darkirc_v5"]
 #accept_addrs = ["tcp+tls://0.0.0.0:25551"]
@@ -32,6 +33,7 @@ rpc_listen = "tcp://127.0.0.1:18927"
 #localnet = false
 #datastore = "~/.local/share/darkfi/lilith/darkirc"
 #hostlist ="~/.local/share/darkfi/lilith/darkirc/hostlist.tsv"
+#magic_bytes = [251, 229, 199, 181]
 
 #[network."taud_v5"]
 #accept_addrs = ["tcp+tls://0.0.0.0:23331"]
@@ -41,3 +43,4 @@ rpc_listen = "tcp://127.0.0.1:18927"
 #localnet = false
 #datastore = "~/.local/share/darkfi/lilith/taud"
 #hostlist ="~/.local/share/darkfi/lilith/taud/hostlist.tsv"
+#magic_bytes = [157, 137, 109, 97]

+ 28 - 3
bin/lilith/src/main.rs

@@ -39,7 +39,12 @@ use url::Url;
 
 use darkfi::{
     async_daemonize, cli_desc,
-    net::{self, hosts::HostColor, settings::BanPolicy, P2p, P2pPtr},
+    net::{
+        self,
+        hosts::HostColor,
+        settings::{BanPolicy, MagicBytes},
+        P2p, P2pPtr,
+    },
     rpc::{
         jsonrpc::*,
         server::{listen_and_serve, RequestHandler},
@@ -150,6 +155,8 @@ struct NetInfo {
     pub datastore: String,
     /// Path to hostlist
     pub hostlist: String,
+    /// Magic bytes used to distinguish the p2p network
+    pub magic_bytes: MagicBytes,
 }
 
 /// Struct representing the daemon
@@ -325,8 +332,25 @@ fn parse_configured_networks(data: &str) -> Result<HashMap<String, NetInfo>> {
 
                 let hostlist: String = table["hostlist"].as_str().unwrap().to_string();
 
-                let net_info =
-                    NetInfo { accept_addrs, seeds, peers, version, localnet, datastore, hostlist };
+                let magic_bytes: [u8; 4] = table["magic_bytes"]
+                    .as_array()
+                    .unwrap()
+                    .iter()
+                    .map(|v| v.as_integer().unwrap() as u8)
+                    .collect::<Vec<u8>>()
+                    .try_into()
+                    .expect("Wrong magic bytes value");
+
+                let net_info = NetInfo {
+                    accept_addrs,
+                    seeds,
+                    peers,
+                    version,
+                    localnet,
+                    datastore,
+                    hostlist,
+                    magic_bytes: MagicBytes(magic_bytes),
+                };
                 ret.insert(name, net_info);
             }
         }
@@ -345,6 +369,7 @@ async fn spawn_net(name: String, info: &NetInfo, ex: Arc<Executor<'static>>) ->
 
     // P2P network settings
     let settings = net::Settings {
+        magic_bytes: info.magic_bytes.clone(),
         inbound_addrs: listen_urls.clone(),
         seeds: info.seeds.clone(),
         peers: info.peers.clone(),

+ 3 - 0
bin/tau/taud/taud_config.toml

@@ -53,6 +53,9 @@ rpc_disabled_methods = ["p2p.get_info"]
 
 # P2P network settings
 [net]
+# Magic Bytes to distinguish the p2p network
+magic_bytes = [157, 137, 109, 97]
+
 # Path to the P2P datastore
 p2p_datastore = "~/.local/share/darkfi/taud"