config: Add application configuration
Naturally, we need a configuration mechanism in order to specify connection parameters for the BURP stats socket. Using the `serde::Deserialize` trait will make this relatively straightforward and allows us to choose from a wide variety of data formats as our configuration language. We will probably use TOML since it is pretty simple to read and write for both humans and machines, and our data model is very simple.
This commit is contained in:
11
Cargo.lock
generated
11
Cargo.lock
generated
@@ -59,6 +59,7 @@ checksum = "a4a45a46ab1f2412e53d3a0ade76ffad2025804294569aae387231a0cd6e0899"
|
||||
name = "burp_exporter"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"gethostname",
|
||||
"openssl",
|
||||
"prometheus_exporter_base",
|
||||
"serde",
|
||||
@@ -216,6 +217,16 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gethostname"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4addc164932852d066774c405dbbdb7914742d2b39e39e1a7ca949c856d054d1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
|
||||
@@ -7,6 +7,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
openssl = "^0.10.38"
|
||||
gethostname = "^0.2.2"
|
||||
serde_json = "1.0.78"
|
||||
tokio-native-tls = "^0.3.0"
|
||||
tracing = "^0.1.30"
|
||||
|
||||
89
src/config.rs
Normal file
89
src/config.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
//! Application configuration
|
||||
use gethostname::gethostname;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Configuration options
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
/// http listen address (default: `::`)
|
||||
#[serde(default = "default_listen_host")]
|
||||
pub listen_host: String,
|
||||
|
||||
/// http listen port (default: `9645`)
|
||||
#[serde(default = "default_listen_port")]
|
||||
pub listen_port: u16,
|
||||
|
||||
/// server hostname or ip address (default: `localhost`)
|
||||
#[serde(default = "default_server")]
|
||||
pub server: String,
|
||||
|
||||
/// server port (default: `4972`)
|
||||
#[serde(default = "default_port")]
|
||||
pub port: u16,
|
||||
|
||||
/// server name (for certificate validation)
|
||||
#[serde(default)]
|
||||
pub server_name: Option<String>,
|
||||
|
||||
/// ca certificate file
|
||||
#[serde(default)]
|
||||
pub ca_cert: Option<String>,
|
||||
|
||||
/// client identity file
|
||||
#[serde(default)]
|
||||
pub identity: Option<String>,
|
||||
|
||||
/// client identity file password
|
||||
#[serde(default)]
|
||||
pub identity_password: Option<String>,
|
||||
|
||||
/// client name (default: *local host name*)
|
||||
#[serde(default = "default_client_name")]
|
||||
pub client_name: String,
|
||||
|
||||
/// client password
|
||||
#[serde(default)]
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen_host: default_listen_host(),
|
||||
listen_port: default_listen_port(),
|
||||
server: default_server(),
|
||||
port: default_port(),
|
||||
server_name: None,
|
||||
ca_cert: None,
|
||||
identity: None,
|
||||
identity_password: None,
|
||||
client_name: default_client_name(),
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn default_listen_host() -> String {
|
||||
"::".into()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn default_listen_port() -> u16 {
|
||||
9645
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn default_server() -> String {
|
||||
"localhost".into()
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn default_port() -> u16 {
|
||||
4972
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
fn default_client_name() -> String {
|
||||
gethostname().to_string_lossy().into()
|
||||
}
|
||||
Reference in New Issue
Block a user