lilith/
main.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::{
20    collections::{HashMap, HashSet},
21    process::exit,
22    sync::Arc,
23    time::UNIX_EPOCH,
24};
25
26use async_trait::async_trait;
27use log::{debug, error, info, warn};
28use semver::Version;
29use smol::{
30    lock::{Mutex, MutexGuard},
31    stream::StreamExt,
32    Executor,
33};
34use structopt::StructOpt;
35use structopt_toml::StructOptToml;
36use tinyjson::JsonValue;
37use toml::Value;
38use url::Url;
39
40use darkfi::{
41    async_daemonize, cli_desc,
42    net::{self, hosts::HostColor, settings::BanPolicy, P2p, P2pPtr},
43    rpc::{
44        jsonrpc::*,
45        server::{listen_and_serve, RequestHandler},
46        settings::{RpcSettings, RpcSettingsOpt},
47    },
48    system::{sleep, StoppableTask, StoppableTaskPtr},
49    util::path::get_config_path,
50    Error, Result,
51};
52
53const CONFIG_FILE: &str = "lilith_config.toml";
54const CONFIG_FILE_CONTENTS: &str = include_str!("../lilith_config.toml");
55
56#[derive(Clone, Debug, serde::Deserialize, StructOpt, StructOptToml)]
57#[serde(default)]
58#[structopt(name = "lilith", about = cli_desc!())]
59struct Args {
60    #[structopt(flatten)]
61    /// JSON-RPC settings
62    rpc: RpcSettingsOpt,
63
64    #[structopt(short, long)]
65    /// Configuration file to use
66    config: Option<String>,
67
68    #[structopt(short, long)]
69    /// Set log file to ouput into
70    log: Option<String>,
71
72    #[structopt(short, parse(from_occurrences))]
73    /// Increase verbosity (-vvv supported)
74    verbose: u8,
75
76    #[structopt(long, default_value = "120")]
77    /// Interval after which to check whitelist peers
78    whitelist_refinery_interval: u64,
79}
80
81/// Struct representing a spawned P2P network
82struct Spawn {
83    /// String identifier,
84    pub name: String,
85    /// P2P pointer
86    pub p2p: P2pPtr,
87}
88
89impl Spawn {
90    async fn get_whitelist(&self) -> Vec<JsonValue> {
91        self.p2p
92            .hosts()
93            .container
94            .fetch_all(HostColor::White)
95            .iter()
96            .map(|(addr, _url)| JsonValue::String(addr.to_string()))
97            .collect()
98    }
99
100    async fn get_greylist(&self) -> Vec<JsonValue> {
101        self.p2p
102            .hosts()
103            .container
104            .fetch_all(HostColor::Grey)
105            .iter()
106            .map(|(addr, _url)| JsonValue::String(addr.to_string()))
107            .collect()
108    }
109
110    async fn get_goldlist(&self) -> Vec<JsonValue> {
111        self.p2p
112            .hosts()
113            .container
114            .fetch_all(HostColor::Gold)
115            .iter()
116            .map(|(addr, _url)| JsonValue::String(addr.to_string()))
117            .collect()
118    }
119
120    async fn info(&self) -> JsonValue {
121        let mut addr_vec = vec![];
122        for addr in &self.p2p.settings().read().await.inbound_addrs {
123            addr_vec.push(JsonValue::String(addr.as_ref().to_string()));
124        }
125
126        JsonValue::Object(HashMap::from([
127            ("name".to_string(), JsonValue::String(self.name.clone())),
128            ("urls".to_string(), JsonValue::Array(addr_vec)),
129            ("whitelist".to_string(), JsonValue::Array(self.get_whitelist().await)),
130            ("greylist".to_string(), JsonValue::Array(self.get_greylist().await)),
131            ("goldlist".to_string(), JsonValue::Array(self.get_goldlist().await)),
132        ]))
133    }
134}
135
136/// Defines the network-specific settings
137#[derive(Clone)]
138struct NetInfo {
139    /// Accept addresses the network will use
140    pub accept_addrs: Vec<Url>,
141    /// Other seeds to connect to
142    pub seeds: Vec<Url>,
143    /// Manual peers to connect to
144    pub peers: Vec<Url>,
145    /// Supported network version
146    pub version: Version,
147    /// Enable localnet hosts
148    pub localnet: bool,
149    /// Path to P2P datastore
150    pub datastore: String,
151    /// Path to hostlist
152    pub hostlist: String,
153}
154
155/// Struct representing the daemon
156struct Lilith {
157    /// Spawned networks
158    pub networks: Vec<Spawn>,
159    /// JSON-RPC connection tracker
160    pub rpc_connections: Mutex<HashSet<StoppableTaskPtr>>,
161}
162
163impl Lilith {
164    /// Since `Lilith` does not make outbound connections, if a peer is
165    /// upgraded to whitelist it will remain on the whitelist even if the
166    /// give peer is no longer online.
167    ///
168    /// To protect `Lilith` from sharing potentially offline nodes,
169    /// `whitelist_refinery` periodically ping nodes on the whitelist. If they
170    /// are reachable, we update their last seen field. Otherwise, we downgrade
171    /// them to the greylist.
172    ///
173    /// Note: if `Lilith` loses connectivity this method will delete peers from
174    /// the whitelist, meaning `Lilith` will need to rebuild its hostlist when
175    /// it comes back online.
176    async fn whitelist_refinery(
177        network_name: String,
178        p2p: P2pPtr,
179        refinery_interval: u64,
180    ) -> Result<()> {
181        debug!(target: "net::refinery::whitelist_refinery", "Starting whitelist refinery for \"{}\"",
182           network_name);
183
184        let hosts = p2p.hosts();
185
186        loop {
187            sleep(refinery_interval).await;
188
189            match hosts.container.fetch_last(HostColor::White) {
190                Some(entry) => {
191                    let url = &entry.0;
192                    let last_seen = &entry.1;
193
194                    if !hosts.refinable(url.clone()) {
195                        debug!(target: "net::refinery::whitelist_refinery", "Addr={} not available!",
196                       url.clone());
197
198                        continue
199                    }
200
201                    if !p2p.session_refine().handshake_node(url.clone(), p2p.clone()).await {
202                        debug!(target: "net::refinery:::whitelist_refinery",
203                       "Host {} is not responsive. Downgrading from whitelist", url);
204
205                        hosts.greylist_host(url, *last_seen)?;
206
207                        continue
208                    }
209
210                    debug!(target: "net::refinery::whitelist_refinery",
211                   "Peer {} is responsive. Updating last_seen", url);
212
213                    // This node is active. Update the last seen field.
214                    let last_seen = UNIX_EPOCH.elapsed().unwrap().as_secs();
215
216                    hosts.whitelist_host(url, last_seen)?;
217                }
218                None => {
219                    debug!(target: "net::refinery::whitelist_refinery",
220                              "Whitelist is empty! Cannot start refinery process");
221
222                    continue
223                }
224            }
225        }
226    }
227    // RPCAPI:
228    // Returns all spawned networks names with their node addresses.
229    // --> {"jsonrpc": "2.0", "method": "spawns", "params": [], "id": 42}
230    // <-- {"jsonrpc": "2.0", "result": {"spawns": spawns_info}, "id": 42}
231    async fn spawns(&self, id: u16, _params: JsonValue) -> JsonResult {
232        let mut spawns = vec![];
233        for spawn in &self.networks {
234            spawns.push(spawn.info().await);
235        }
236
237        let json =
238            JsonValue::Object(HashMap::from([("spawns".to_string(), JsonValue::Array(spawns))]));
239
240        JsonResponse::new(json, id).into()
241    }
242}
243
244#[async_trait]
245impl RequestHandler<()> for Lilith {
246    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
247        return match req.method.as_str() {
248            "ping" => self.pong(req.id, req.params).await,
249            "spawns" => self.spawns(req.id, req.params).await,
250            _ => JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
251        }
252    }
253
254    async fn connections_mut(&self) -> MutexGuard<'life0, HashSet<StoppableTaskPtr>> {
255        self.rpc_connections.lock().await
256    }
257}
258
259/// Parse a TOML string for any configured network and return a map containing
260/// said configurations.
261fn parse_configured_networks(data: &str) -> Result<HashMap<String, NetInfo>> {
262    let mut ret = HashMap::new();
263
264    if let Value::Table(map) = toml::from_str(data)? {
265        if map.contains_key("network") && map["network"].is_table() {
266            for net in map["network"].as_table().unwrap() {
267                info!(target: "lilith", "Found configuration for network: {}", net.0);
268                let table = net.1.as_table().unwrap();
269                if !table.contains_key("accept_addrs") {
270                    warn!(target: "lilith", "Network accept addrs are mandatory, skipping network.");
271                    continue
272                }
273
274                if !table.contains_key("hostlist") {
275                    error!(target: "lilith", "Hostlist path is mandatory! Configure and try again.");
276                    exit(1)
277                }
278
279                let name = net.0.to_string();
280                let accept_addrs: Vec<Url> = table["accept_addrs"]
281                    .as_array()
282                    .unwrap()
283                    .iter()
284                    .map(|x| Url::parse(x.as_str().unwrap()).unwrap())
285                    .collect();
286
287                let mut seeds = vec![];
288                if table.contains_key("seeds") {
289                    if let Some(s) = table["seeds"].as_array() {
290                        for seed in s {
291                            if let Some(u) = seed.as_str() {
292                                if let Ok(url) = Url::parse(u) {
293                                    seeds.push(url);
294                                }
295                            }
296                        }
297                    }
298                }
299
300                let mut peers = vec![];
301                if table.contains_key("peers") {
302                    if let Some(p) = table["peers"].as_array() {
303                        for peer in p {
304                            if let Some(u) = peer.as_str() {
305                                if let Ok(url) = Url::parse(u) {
306                                    peers.push(url);
307                                }
308                            }
309                        }
310                    }
311                }
312
313                let localnet = if table.contains_key("localnet") {
314                    table["localnet"].as_bool().unwrap()
315                } else {
316                    false
317                };
318
319                let version = if table.contains_key("version") {
320                    semver::Version::parse(table["version"].as_str().unwrap())?
321                } else {
322                    semver::Version::parse(option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0"))?
323                };
324
325                let datastore: String = table["datastore"].as_str().unwrap().to_string();
326
327                let hostlist: String = table["hostlist"].as_str().unwrap().to_string();
328
329                let net_info =
330                    NetInfo { accept_addrs, seeds, peers, version, localnet, datastore, hostlist };
331                ret.insert(name, net_info);
332            }
333        }
334    }
335
336    Ok(ret)
337}
338
339async fn spawn_net(name: String, info: &NetInfo, ex: Arc<Executor<'static>>) -> Result<Spawn> {
340    let mut listen_urls = vec![];
341
342    // Configure listen addrs for this network
343    for url in &info.accept_addrs {
344        listen_urls.push(url.clone());
345    }
346
347    // P2P network settings
348    let settings = net::Settings {
349        inbound_addrs: listen_urls.clone(),
350        seeds: info.seeds.clone(),
351        peers: info.peers.clone(),
352        outbound_connections: 0,
353        outbound_connect_timeout: 30,
354        inbound_connections: 512,
355        app_version: info.version.clone(),
356        localnet: info.localnet,
357        p2p_datastore: Some(info.datastore.clone()),
358        hostlist: Some(info.hostlist.clone()),
359        allowed_transports: vec![
360            "tcp".to_string(),
361            "tcp+tls".to_string(),
362            "tor".to_string(),
363            "tor+tls".to_string(),
364            "nym".to_string(),
365            "nym+tls".to_string(),
366            "i2p".to_string(),
367            "i2p+tls".to_string(),
368        ],
369        ban_policy: BanPolicy::Relaxed,
370        ..Default::default()
371    };
372
373    // Create P2P instance
374    let p2p = P2p::new(settings, ex.clone()).await?;
375
376    let addrs_str: Vec<&str> = listen_urls.iter().map(|x| x.as_str()).collect();
377    info!(target: "lilith", "Starting seed network node for \"{}\" on {:?}", name, addrs_str);
378    p2p.clone().start().await?;
379
380    let spawn = Spawn { name, p2p };
381    Ok(spawn)
382}
383
384async_daemonize!(realmain);
385async fn realmain(args: Args, ex: Arc<Executor<'static>>) -> Result<()> {
386    // Pick up network settings from the TOML config
387    let cfg_path = get_config_path(args.config, CONFIG_FILE)?;
388    let toml_contents = std::fs::read_to_string(cfg_path)?;
389    let configured_nets = parse_configured_networks(&toml_contents)?;
390
391    if configured_nets.is_empty() {
392        error!(target: "lilith", "No networks are enabled in config");
393        exit(1);
394    }
395
396    // Spawn configured networks
397    let mut networks = vec![];
398    for (name, info) in &configured_nets {
399        match spawn_net(name.to_string(), info, ex.clone()).await {
400            Ok(spawn) => networks.push(spawn),
401            Err(e) => {
402                error!(target: "lilith", "Failed to start P2P network seed for \"{}\": {}", name, e);
403                exit(1);
404            }
405        }
406    }
407
408    // Set up main daemon and background refinery_tasks
409    let lilith = Arc::new(Lilith { networks, rpc_connections: Mutex::new(HashSet::new()) });
410    let mut refinery_tasks = HashMap::new();
411    for network in &lilith.networks {
412        let name = network.name.clone();
413        let task = StoppableTask::new();
414        task.clone().start(
415            Lilith::whitelist_refinery(name.clone(), network.p2p.clone(), args.whitelist_refinery_interval),
416            |res| async move {
417                match res {
418                    Ok(()) | Err(Error::DetachedTaskStopped) => { /* Do nothing */ }
419                    Err(e) => error!(target: "lilith", "Failed starting refinery task for \"{}\": {}", name, e),
420                }
421            },
422            Error::DetachedTaskStopped,
423            ex.clone(),
424        );
425        refinery_tasks.insert(network.name.clone(), task);
426    }
427
428    // JSON-RPC server
429    let rpc_settings: RpcSettings = args.rpc.into();
430    info!(target: "lilith", "Starting JSON-RPC server on {}", rpc_settings.listen);
431    let lilith_ = lilith.clone();
432    let rpc_task = StoppableTask::new();
433    rpc_task.clone().start(
434        listen_and_serve(rpc_settings, lilith.clone(), None, ex.clone()),
435        |res| async move {
436            match res {
437                Ok(()) | Err(Error::RpcServerStopped) => lilith_.stop_connections().await,
438                Err(e) => error!(target: "lilith", "Failed starting JSON-RPC server: {}", e),
439            }
440        },
441        Error::RpcServerStopped,
442        ex.clone(),
443    );
444
445    // Signal handling for graceful termination.
446    let (signals_handler, signals_task) = SignalHandler::new(ex)?;
447    signals_handler.wait_termination(signals_task).await?;
448    info!(target: "lilith", "Caught termination signal, cleaning up and exiting...");
449
450    info!(target: "lilith", "Stopping JSON-RPC server...");
451    rpc_task.stop().await;
452
453    // Cleanly stop p2p networks
454    for spawn in &lilith.networks {
455        info!(target: "lilith", "Stopping \"{}\" task", spawn.name);
456        refinery_tasks.get(&spawn.name).unwrap().stop().await;
457        info!(target: "lilith", "Stopping \"{}\" P2P", spawn.name);
458        spawn.p2p.stop().await;
459    }
460
461    info!(target: "lilith", "Bye!");
462    Ok(())
463}