aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 00790bd0fc270ea328a9a480d886a27e1fca9238 (plain)
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
// SPDX-FileCopyrightText: 2025 Tomasz Kramkowski <tomasz@kramkow.ski>
// SPDX-License-Identifier: GPL-3.0-or-later

use std::{
    collections::HashMap,
    fs::File,
    io::Read,
    os::unix::fs::PermissionsExt,
    path::Path,
    process,
    time::Duration,
};

use anyhow::bail;
use rumqttc::{AsyncClient, EventLoop, MqttOptions};
use serde::Deserialize;

use crate::PROGRAM;

#[derive(Deserialize, Debug)]
pub struct Credentials {
    pub username: String,
    pub password: String,
}

fn default_host() -> String {
    "localhost".to_string()
}

fn default_port() -> u16 {
    1883
}

fn default_id() -> String {
    PROGRAM.to_string()
}

#[derive(Deserialize, Debug)]
pub struct Config {
    #[serde(default = "default_host")]
    pub host: String,
    #[serde(default = "default_port")]
    pub port: u16,
    pub credentials: Option<Credentials>,
    #[serde(default = "default_id")]
    pub id: String,
    // TODO: Figure out a way to allow arbitrary unix paths (arbitrary
    // non-unicode) without base64
    pub routes: HashMap<String, Vec<Vec<String>>>,
}

impl Config {
    pub fn mqtt_client(&self) -> (AsyncClient, EventLoop) {
        let client_id = format!("{}_{}", self.id, process::id());
        let mut options = MqttOptions::new(client_id, &self.host, self.port);
        if let Some(credentials) = &self.credentials {
            options.set_credentials(&credentials.username, &credentials.password);
        }
        options.set_keep_alive(Duration::from_secs(5));
        options.set_max_packet_size(10 * 1024 * 1024, 10 * 1024 * 1024);
        AsyncClient::new(options, 10)
    }
}

pub fn load<P: AsRef<Path>>(path: P) -> anyhow::Result<Config> {
    let mut f = File::open(path)?;
    let mut config = String::new();
    f.read_to_string(&mut config)?;
    let config: Config = toml::from_str(&config)?;
    if config.credentials.is_some() {
        let mode = f.metadata()?.permissions().mode();
        if mode & 0o044 != 0o000 {
            bail!("Config file contains credentials while being group or world readable.");
        }
    }
    Ok(config)
}