1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2023 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::{
    collections::{HashMap, HashSet},
    path::Path,
};

use async_std::sync::Arc;
use async_trait::async_trait;
use log::{error, info, warn};
use serde_json::{json, Value};
use structopt_toml::StructOptToml;
use url::Url;

use darkfi::{
    async_daemonize, net,
    net::P2pPtr,
    rpc::{
        jsonrpc::{
            ErrorCode::{InvalidParams, MethodNotFound},
            JsonError, JsonRequest, JsonResponse, JsonResult,
        },
        server::{listen_and_serve, RequestHandler},
    },
    util::{
        file::{load_file, save_file},
        path::{expand_path, get_config_path},
    },
    Result,
};

mod config;
use config::{parse_configured_networks, Args, NetInfo};

const CONFIG_FILE: &str = "lilith_config.toml";
const CONFIG_FILE_CONTENTS: &str = include_str!("../lilith_config.toml");

/// Struct representing a spawned p2p network.
struct Spawn {
    name: String,
    p2p: P2pPtr,
}

impl Spawn {
    async fn addresses(&self) -> Vec<String> {
        self.p2p.hosts().load_all().await.iter().map(|addr| addr.to_string()).collect()
    }

    pub async fn info(&self) -> serde_json::Value {
        // Building addr_vec string
        let mut addr_vec = vec![];
        for addr in &self.p2p.settings().inbound {
            addr_vec.push(addr.as_ref().to_string());
        }

        json!({
            "name": self.name.clone(),
            "urls": addr_vec,
            "hosts": self.addresses().await,
        })
    }
}

/// Struct representing the daemon.
struct Lilith {
    /// Configured urls
    urls: Vec<Url>,
    /// Spawned networks
    spawns: Vec<Spawn>,
}

impl Lilith {
    async fn spawns_hosts(&self) -> HashMap<String, Vec<String>> {
        // Building urls string
        let mut spawns = HashMap::new();
        for spawn in &self.spawns {
            spawns.insert(spawn.name.clone(), spawn.addresses().await);
        }

        spawns
    }

    // RPCAPI:
    // Returns all spawned networks names with their node addresses.
    // --> {"jsonrpc": "2.0", "method": "spawns", "params": [], "id": 42}
    // <-- {"jsonrpc": "2.0", "result": "{spawns}", "id": 42}
    async fn spawns(&self, id: Value, _params: &[Value]) -> JsonResult {
        // Building urls string
        let mut urls_vec = vec![];
        for url in &self.urls {
            urls_vec.push(url.as_ref().to_string());
        }

        // Gathering spawns info
        let mut spawns = vec![];
        for spawn in &self.spawns {
            spawns.push(spawn.info().await);
        }

        // Generating json
        let json = json!({
            "urls": urls_vec,
            "spawns": spawns,
        });
        JsonResponse::new(json, id).into()
    }

    // RPCAPI:
    // Replies to a ping method.
    // --> {"jsonrpc": "2.0", "method": "ping", "params": [], "id": 42}
    // <-- {"jsonrpc": "2.0", "result": "pong", "id": 42}
    async fn pong(&self, id: Value, _params: &[Value]) -> JsonResult {
        JsonResponse::new(json!("pong"), id).into()
    }
}

#[async_trait]
impl RequestHandler for Lilith {
    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
        if !req.params.is_array() {
            return JsonError::new(InvalidParams, None, req.id).into()
        }

        let params = req.params.as_array().unwrap();

        match req.method.as_str() {
            Some("spawns") => return self.spawns(req.id, params).await,
            Some("ping") => return self.pong(req.id, params).await,
            Some(_) | None => return JsonError::new(MethodNotFound, None, req.id).into(),
        }
    }
}

async fn spawn_network(
    name: &str,
    info: NetInfo,
    urls: Vec<Url>,
    saved_hosts: Option<&HashSet<Url>>,
    ex: Arc<smol::Executor<'_>>,
) -> Result<Spawn> {
    let mut full_urls = Vec::new();
    for url in &urls {
        let mut url = url.clone();
        url.set_port(Some(info.port))?;
        full_urls.push(url);
    }
    let network_settings = net::Settings {
        inbound: full_urls.clone(),
        seeds: info.seeds,
        peers: info.peers,
        outbound_connections: 0,
        localnet: info.localnet,
        channel_log: info.channel_log,
        app_version: None,
        ..Default::default()
    };

    let p2p = net::P2p::new(network_settings).await;

    // Setting saved hosts
    match saved_hosts {
        Some(hosts) => {
            // Converting hashet to vec
            let mut vec = vec![];
            for url in hosts {
                vec.push(url.clone());
            }
            p2p.hosts().store(vec).await;
        }
        None => info!("No saved hosts found for {}", name),
    }

    // Building ext_addr_vec string
    let mut urls_vec = vec![];
    for url in &full_urls {
        urls_vec.push(url.as_ref().to_string());
    }
    info!("Starting seed network node for {} at: {:?}", name, urls_vec);
    p2p.clone().start(ex.clone()).await?;
    let _ex = ex.clone();
    let _p2p = p2p.clone();
    ex.spawn(async move {
        if let Err(e) = _p2p.run(_ex).await {
            error!("Failed starting P2P network seed: {}", e);
        }
    })
    .detach();

    let spawn = Spawn { name: name.to_string(), p2p };

    Ok(spawn)
}

/// Retrieve saved hosts for provided networks
fn load_hosts(path: &Path, networks: &[&str]) -> HashMap<String, HashSet<Url>> {
    let mut saved_hosts = HashMap::new();
    info!("Retrieving saved hosts from: {:?}", path);
    let contents = load_file(path);
    if let Err(e) = contents {
        warn!("Failed retrieving saved hosts: {}", e);
        return saved_hosts
    }

    for line in contents.unwrap().lines() {
        let data: Vec<&str> = line.split('\t').collect();
        if networks.contains(&data[0]) {
            let mut hosts = match saved_hosts.get(data[0]) {
                Some(hosts) => hosts.clone(),
                None => HashSet::new(),
            };
            let url = match Url::parse(data[1]) {
                Ok(u) => u,
                Err(e) => {
                    warn!("Skipping malformed url: {} ({})", data[1], e);
                    continue
                }
            };
            hosts.insert(url);
            saved_hosts.insert(data[0].to_string(), hosts);
        }
    }

    saved_hosts
}

/// Save spawns current hosts
fn save_hosts(path: &Path, spawns: HashMap<String, Vec<String>>) {
    let mut string = "".to_string();
    for (name, urls) in spawns {
        for url in urls {
            string.push_str(&name);
            string.push('\t');
            string.push_str(&url);
            string.push('\n');
        }
    }

    if !string.eq("") {
        info!("Saving current hosts of spawnned networks to: {:?}", path);
        if let Err(e) = save_file(path, &string) {
            error!("Failed saving hosts: {}", e);
        }
    }
}

async_daemonize!(realmain);
async fn realmain(args: Args, ex: Arc<smol::Executor<'_>>) -> Result<()> {
    // We use this handler to block this function after detaching all
    // tasks, and to catch a shutdown signal, where we can clean up and
    // exit gracefully.
    let (signal, shutdown) = smol::channel::bounded::<()>(1);
    ctrlc::set_handler(move || {
        async_std::task::block_on(signal.send(())).unwrap();
    })
    .unwrap();

    // Pick up network settings from the TOML configuration
    let cfg_path = get_config_path(args.config, CONFIG_FILE)?;
    let toml_contents = std::fs::read_to_string(cfg_path)?;
    let configured_nets = parse_configured_networks(&toml_contents)?;

    // Verify any daemon network is enabled
    if configured_nets.is_empty() {
        info!("No daemon network is enabled!");
        return Ok(())
    }

    // Setting urls
    let mut urls = args.urls.clone();
    if urls.is_empty() {
        info!("Urls are not provided, will use: tcp://127.0.0.1");
        let url = Url::parse("tcp://127.0.0.1")?;
        urls.push(url);
    }

    // Retrieve saved hosts for configured networks
    let full_path = expand_path(&args.hosts_file)?;
    let nets: Vec<&str> = configured_nets.keys().map(|x| x.as_str()).collect();
    let saved_hosts = load_hosts(&full_path, &nets);

    // Spawn configured networks
    let mut spawns = vec![];
    for (name, info) in &configured_nets {
        match spawn_network(name, info.clone(), urls.clone(), saved_hosts.get(name), ex.clone())
            .await
        {
            Ok(spawn) => spawns.push(spawn),
            Err(e) => error!("Failed starting {} P2P network seed: {}", name, e),
        }
    }

    let lilith = Lilith { urls, spawns };
    let lilith = Arc::new(lilith);

    // JSON-RPC server
    info!("Starting JSON-RPC server");
    let _ex = ex.clone();
    ex.spawn(listen_and_serve(args.rpc_listen, lilith.clone(), _ex)).detach();

    // Wait for SIGINT
    shutdown.recv().await?;
    print!("\r");
    info!("Caught termination signal, cleaning up and exiting...");

    // Save spawns current hosts
    save_hosts(&full_path, lilith.spawns_hosts().await);

    Ok(())
}