aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 362c9623be3c9ad7090708f2fa3612acba3ca0bc (plain) (blame)
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
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Copyright 2019 Hristo Venev

use crate::model::{Ipv4Set, Ipv6Set, Key, Secret};
use serde_derive;
use std::collections::HashMap;
use std::path::PathBuf;

#[derive(serde_derive::Serialize, serde_derive::Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(deny_unknown_fields)]
pub struct Source {
    pub url: String,
    pub psk: Option<Secret>,
    pub ipv4: Ipv4Set,
    pub ipv6: Ipv6Set,
    #[serde(default)]
    pub required: bool,
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(deny_unknown_fields)]
pub struct Peer {
    pub source: Option<String>,
    pub psk: Option<Secret>,
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct GlobalConfig {
    pub min_keepalive: u32,
    pub max_keepalive: u32,
    pub peers: HashMap<Key, Peer>,
}

impl Default for GlobalConfig {
    #[inline]
    fn default() -> Self {
        Self {
            min_keepalive: default_min_keepalive(),
            max_keepalive: default_max_keepalive(),
            peers: HashMap::new(),
        }
    }
}

impl GlobalConfig {
    pub fn fix_keepalive(&self, mut k: u32) -> u32 {
        if self.max_keepalive != 0 && (k == 0 || k > self.max_keepalive) {
            k = self.max_keepalive;
        }
        if k != 0 && k < self.min_keepalive {
            k = self.min_keepalive;
        }
        k
    }
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct UpdaterConfig {
    pub cache_directory: Option<PathBuf>,

    // Number of seconds between regular updates.
    pub refresh_sec: u32,
}

impl Default for UpdaterConfig {
    #[inline]
    fn default() -> Self {
        Self {
            cache_directory: None,
            refresh_sec: default_refresh_sec(),
        }
    }
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize, Default, Clone, Debug)]
#[serde(from = "ConfigRepr", into = "ConfigRepr")]
pub struct Config {
    pub runtime_directory: Option<PathBuf>,
    pub global: GlobalConfig,
    pub updater: UpdaterConfig,
    pub sources: HashMap<String, Source>,
}

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(deny_unknown_fields)]
struct ConfigRepr {
    runtime_directory: Option<PathBuf>,
    cache_directory: Option<PathBuf>,

    #[serde(default = "default_min_keepalive")]
    min_keepalive: u32,
    #[serde(default = "default_max_keepalive")]
    max_keepalive: u32,
    #[serde(default, rename = "peer")]
    peers: HashMap<Key, Peer>,

    #[serde(default = "default_refresh_sec")]
    refresh_sec: u32,

    #[serde(default, rename = "source")]
    sources: HashMap<String, Source>,
}

impl From<Config> for ConfigRepr {
    #[inline]
    fn from(v: Config) -> Self {
        let Config {
            runtime_directory,
            global,
            updater,
            sources,
        } = v;
        let GlobalConfig {
            min_keepalive,
            max_keepalive,
            peers,
        } = global;
        let UpdaterConfig {
            cache_directory,
            refresh_sec,
        } = updater;
        Self {
            runtime_directory,
            cache_directory,
            min_keepalive,
            max_keepalive,
            peers,
            refresh_sec,
            sources,
        }
    }
}

impl From<ConfigRepr> for Config {
    #[inline]
    fn from(v: ConfigRepr) -> Self {
        let ConfigRepr {
            runtime_directory,
            cache_directory,
            min_keepalive,
            max_keepalive,
            peers,
            refresh_sec,
            sources,
        } = v;
        Self {
            runtime_directory,
            global: GlobalConfig {
                min_keepalive,
                max_keepalive,
                peers,
            },
            updater: UpdaterConfig {
                cache_directory,
                refresh_sec,
            },
            sources,
        }
    }
}

#[inline]
const fn default_min_keepalive() -> u32 {
    10
}

#[inline]
const fn default_max_keepalive() -> u32 {
    0
}

#[inline]
const fn default_refresh_sec() -> u32 {
    1200
}