aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: a1dff3e8c26a3c8045a6b6ca3555cefa0fb9f1d3 (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
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Copyright 2019,2020 Hristo Venev

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

#[derive(serde_derive::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Source {
    pub name: String,
    pub url: String,
    pub psk: Option<Secret>,
    pub ipv4: Ipv4Set,
    pub ipv6: Ipv6Set,
    #[serde(default)]
    pub required: bool,
    #[serde(default = "default_allow_road_warriors")]
    pub allow_road_warriors: bool,
}

#[derive(serde_derive::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Peer {
    pub source: Option<String>,
    pub endpoint: Option<Endpoint>,
    pub psk: Option<Secret>,
    pub keepalive: Option<u32>,
}

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
    }
}

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::Deserialize)]
#[serde(from = "ConfigRepr")]
#[derive(Default)]
pub struct Config {
    pub runtime_directory: Option<PathBuf>,
    pub global: GlobalConfig,
    pub updater: UpdaterConfig,
    pub sources: Vec<Source>,
}

#[derive(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: Vec<Source>,
}

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_allow_road_warriors() -> bool {
    true
}

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

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

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