openconnect_core/
config.rs

1use crate::{
2    protocols::{get_anyconnect_protocol, Protocol},
3    result::{OpenconnectError, OpenconnectResult},
4};
5use openconnect_sys::{PRG_DEBUG, PRG_ERR, PRG_INFO, PRG_TRACE};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
8pub enum LogLevel {
9    Err = PRG_ERR as isize,
10    Info = PRG_INFO as isize,
11    Debug = PRG_DEBUG as isize,
12    Trace = PRG_TRACE as isize,
13}
14
15#[derive(Debug, Clone)]
16pub struct Config {
17    pub vpncscript: Option<String>,
18    pub http_proxy: Option<String>,
19    pub loglevel: LogLevel,
20}
21
22pub struct ConfigBuilder {
23    vpncscript: Option<String>,
24    http_proxy: Option<String>,
25    loglevel: Option<LogLevel>,
26}
27
28impl ConfigBuilder {
29    pub fn new() -> Self {
30        Self {
31            vpncscript: None,
32            http_proxy: None,
33            loglevel: None,
34        }
35    }
36
37    pub fn vpncscript(&mut self, vpncscript: &str) -> &mut Self {
38        self.vpncscript = Some(vpncscript.to_string());
39        self
40    }
41
42    pub fn loglevel(&mut self, loglevel: LogLevel) -> &mut Self {
43        self.loglevel = Some(loglevel);
44        self
45    }
46
47    pub fn http_proxy(&mut self, http_proxy: &str) -> &mut Self {
48        self.http_proxy = Some(http_proxy.to_string());
49        self
50    }
51
52    pub fn build(&self) -> OpenconnectResult<Config> {
53        Ok(Config {
54            http_proxy: self.http_proxy.clone(),
55            vpncscript: self.vpncscript.clone(),
56            loglevel: self.loglevel.unwrap_or(LogLevel::Info),
57        })
58    }
59}
60
61impl Config {
62    pub fn builder() -> ConfigBuilder {
63        ConfigBuilder::new()
64    }
65}
66
67impl Default for ConfigBuilder {
68    fn default() -> Self {
69        Self::new()
70    }
71}
72
73#[derive(Debug, Clone)]
74pub struct Entrypoint {
75    pub name: Option<String>,
76    pub server: String,
77    pub username: Option<String>,
78    pub password: Option<String>,
79    pub protocol: Protocol,
80    pub cookie: Option<String>,
81    pub enable_udp: bool,
82    pub accept_insecure_cert: bool,
83}
84
85pub struct EntrypointBuilder {
86    name: Option<String>,
87    server: Option<String>,
88    username: Option<String>,
89    password: Option<String>,
90    protocol: Option<Protocol>,
91    cookie: Option<String>,
92    enable_udp: bool,
93    accept_insecure_cert: Option<bool>,
94}
95
96impl EntrypointBuilder {
97    pub fn new() -> Self {
98        Self {
99            name: None,
100            server: None,
101            username: None,
102            password: None,
103            protocol: None,
104            cookie: None,
105            enable_udp: true,
106            accept_insecure_cert: None,
107        }
108    }
109
110    pub fn name(&mut self, name: &str) -> &mut Self {
111        self.name = Some(name.to_string());
112        self
113    }
114
115    pub fn server(&mut self, server: &str) -> &mut Self {
116        self.server = Some(server.to_string());
117        self
118    }
119
120    pub fn username(&mut self, username: &str) -> &mut Self {
121        self.username = Some(username.to_string());
122        self
123    }
124
125    pub fn password(&mut self, password: &str) -> &mut Self {
126        self.password = Some(password.to_string());
127        self
128    }
129
130    pub fn protocol(&mut self, protocol: Protocol) -> &mut Self {
131        self.protocol = Some(protocol);
132        self
133    }
134
135    pub fn cookie(&mut self, cookie: &str) -> &mut Self {
136        self.cookie = Some(cookie.to_string());
137        self
138    }
139
140    pub fn enable_udp(&mut self, enable_udp: bool) -> &mut Self {
141        self.enable_udp = enable_udp;
142        self
143    }
144
145    pub fn accept_insecure_cert(&mut self, accept_insecure_cert: bool) -> &mut Self {
146        self.accept_insecure_cert = Some(accept_insecure_cert);
147        self
148    }
149
150    pub fn build(&self) -> OpenconnectResult<Entrypoint> {
151        let server = self
152            .server
153            .clone()
154            .ok_or(OpenconnectError::EntrypointConfigError(
155                "Server is required".to_string(),
156            ))?;
157
158        let protocol = self
159            .protocol
160            .clone()
161            .unwrap_or_else(get_anyconnect_protocol);
162
163        Ok(Entrypoint {
164            name: self.name.clone(),
165            server,
166            username: self.username.clone(),
167            password: self.password.clone(),
168            protocol,
169            cookie: self.cookie.clone(),
170            enable_udp: self.enable_udp,
171            accept_insecure_cert: self.accept_insecure_cert.unwrap_or(false),
172        })
173    }
174}
175
176impl Default for EntrypointBuilder {
177    fn default() -> Self {
178        Self::new()
179    }
180}