allow using config file instead of env

This commit is contained in:
Robin Appelman 2024-10-28 13:42:18 +01:00
commit eb35a4abad
5 changed files with 306 additions and 30 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
/target
.env
.direnv
result
result
config.toml

184
Cargo.lock generated
View file

@ -17,6 +17,55 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "anstream"
version = "0.6.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8365de52b16c035ff4fcafe0092ba9390540e3e352870ac09933bebcaa2c8c56"
[[package]]
name = "anstyle-parse"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"
dependencies = [
"anstyle",
"windows-sys 0.59.0",
]
[[package]]
name = "async-stream"
version = "0.3.6"
@ -126,6 +175,46 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "color-eyre"
version = "0.6.3"
@ -153,6 +242,12 @@ dependencies = [
"tracing-error",
]
[[package]]
name = "colorchoice"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -425,6 +520,12 @@ dependencies = [
"http 0.2.12",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.3.9"
@ -557,6 +658,12 @@ version = "2.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itoa"
version = "1.0.11"
@ -1014,6 +1121,15 @@ dependencies = [
"untrusted",
]
[[package]]
name = "secretfile"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "746c54b939ab8d393b536765393c0bd7634fca94eed62321ec3e3559293f6c21"
dependencies = [
"thiserror",
]
[[package]]
name = "security-framework"
version = "2.11.1"
@ -1069,6 +1185,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@ -1150,6 +1275,12 @@ dependencies = [
"lock_api",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "subtle"
version = "2.6.1"
@ -1200,6 +1331,7 @@ version = "0.1.0"
dependencies = [
"async-stream",
"base64 0.22.1",
"clap",
"color-eyre",
"dashmap",
"dotenv",
@ -1208,8 +1340,11 @@ dependencies = [
"json",
"pin-utils",
"rumqttc",
"secretfile",
"serde",
"tokio",
"tokio-stream",
"toml",
"warp",
"warp-reverse-proxy",
]
@ -1344,6 +1479,40 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.8.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tower-service"
version = "0.3.3"
@ -1473,6 +1642,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "valuable"
version = "0.1.0"
@ -1806,6 +1981,15 @@ version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.6.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.50.0"

View file

@ -21,3 +21,7 @@ hostname = "0.4.0"
warp-reverse-proxy = { version = "1.0.0", default-features = false, features = ["rustls-tls"] }
tokio-stream = { version = "0.1.16", features = ["net"] }
base64 = "0.22.1"
secretfile = "0.1.0"
serde = { version = "1.0.213", features = ["derive"] }
toml = "0.8.19"
clap = { version = "4.5.20", features = ["derive"] }

View file

@ -1,38 +1,101 @@
use base64::prelude::*;
use color_eyre::{eyre::WrapErr, Report, Result};
use rumqttc::MqttOptions;
use serde::Deserialize;
use std::fs::read_to_string;
use std::net::{IpAddr, Ipv4Addr};
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub mqtt_host: String,
pub mqtt_port: u16,
pub mqtt_credentials: Option<Credentials>,
pub listen: Listen,
pub tasmota_credentials: Option<Credentials>,
pub mqtt: MqttConfig,
#[serde(default)]
pub tasmota: TasmotaConfig,
}
#[derive(Clone)]
#[derive(Clone, Debug, Deserialize)]
pub struct MqttConfig {
#[serde(rename = "hostname")]
pub host: String,
#[serde(default = "default_mqtt_port")]
pub port: u16,
#[serde(flatten)]
pub credentials: Option<Credentials>,
}
fn default_mqtt_port() -> u16 {
1883
}
#[derive(Clone, Debug, Deserialize, Default)]
pub struct TasmotaConfig {
#[serde(flatten)]
pub credentials: Option<Credentials>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum Listen {
Tcp(u16),
Unix(String),
Tcp {
#[serde(default = "default_address")]
address: IpAddr,
port: u16,
},
Unix {
socket: String,
},
}
#[derive(Clone)]
pub struct Credentials {
pub username: String,
pub password: String,
fn default_address() -> IpAddr {
Ipv4Addr::UNSPECIFIED.into()
}
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum Credentials {
Raw {
username: String,
password: String,
},
File {
username: String,
password_file: String,
},
}
impl Credentials {
pub fn username(&self) -> String {
match self {
Credentials::Raw { username, .. } => username.clone(),
Credentials::File { username, .. } => username.clone(),
}
}
pub fn password(&self) -> String {
match self {
Credentials::Raw { password, .. } => password.clone(),
Credentials::File { password_file, .. } => secretfile::load(password_file).unwrap(),
}
}
pub fn auth_header(&self) -> String {
let mut header = "Basic ".to_string();
BASE64_STANDARD.encode_string(format!("{}:{}", self.username, self.password), &mut header);
BASE64_STANDARD.encode_string(
format!("{}:{}", self.username(), self.password()),
&mut header,
);
header
}
}
impl Config {
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config> {
let raw = read_to_string(path)?;
Ok(toml::from_str(&raw)?)
}
pub fn from_env() -> Result<Self> {
let mqtt_host = dotenv::var("MQTT_HOSTNAME").wrap_err("MQTT_HOSTNAME not set")?;
let mqtt_port = dotenv::var("MQTT_PORT")
@ -40,13 +103,16 @@ impl Config {
.and_then(|port| u16::from_str(&port).ok())
.unwrap_or(1883);
let listen = match dotenv::var("SOCKET") {
Ok(socket) => Listen::Unix(socket),
Ok(socket) => Listen::Unix { socket },
_ => {
let port = dotenv::var("PORT")
.ok()
.and_then(|port| u16::from_str(&port).ok())
.unwrap_or(80);
Listen::Tcp(port)
Listen::Tcp {
address: default_address(),
port,
}
}
};
@ -54,7 +120,7 @@ impl Config {
Ok(username) => {
let password = dotenv::var("MQTT_PASSWORD")
.wrap_err("MQTT_USERNAME set, but MQTT_PASSWORD not set")?;
Some(Credentials { username, password })
Some(Credentials::Raw { username, password })
}
Err(_) => None,
};
@ -63,16 +129,20 @@ impl Config {
Ok(username) => {
let password = dotenv::var("TASMOTA_PASSWORD")
.wrap_err("TASMOTA_USERNAME set, but TASMOTA_PASSWORD not set")?;
Some(Credentials { username, password })
Some(Credentials::Raw { username, password })
}
Err(_) => None,
};
Ok(Config {
mqtt_host,
mqtt_port,
mqtt_credentials,
tasmota_credentials,
mqtt: MqttConfig {
host: mqtt_host,
port: mqtt_port,
credentials: mqtt_credentials,
},
tasmota: TasmotaConfig {
credentials: tasmota_credentials,
},
listen,
})
}
@ -83,11 +153,11 @@ impl Config {
.map_err(|_| Report::msg("invalid hostname"))?;
let mut mqtt_options = MqttOptions::new(
format!("tasproxy-{}", hostname),
&self.mqtt_host,
self.mqtt_port,
&self.mqtt.host,
self.mqtt.port,
);
if let Some(credentials) = self.mqtt_credentials.as_ref() {
mqtt_options.set_credentials(&credentials.username, &credentials.password);
if let Some(credentials) = self.mqtt.credentials.as_ref() {
mqtt_options.set_credentials(credentials.username(), credentials.password());
}
mqtt_options.set_keep_alive(Duration::from_secs(5));
Ok(mqtt_options)

View file

@ -2,6 +2,7 @@ use crate::config::{Config, Listen};
use crate::devices::{Device, DeviceState};
use crate::mqtt::mqtt_stream;
use crate::topic::Topic;
use clap::Parser;
use color_eyre::{eyre::WrapErr, Result};
use dashmap::DashMap;
use futures_util::future::{Either, FutureExt};
@ -32,12 +33,25 @@ mod topic;
type DeviceStates = Arc<DashMap<String, DeviceState>>;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Config file to use, if omitted the config will be loaded from environment variables
config: Option<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
let config = Config::from_env()?;
let args = Args::parse();
let config = match args.config {
Some(path) => Config::from_file(path)?,
_ => Config::from_env()?,
};
let listen = config.listen.clone();
let tasmota_credentials = config
.tasmota_credentials
.tasmota
.credentials
.as_ref()
.map(|auth| auth.auth_header());
@ -114,12 +128,15 @@ async fn main() -> Result<()> {
let warp_server = warp::serve(proxy);
let server = match listen {
Listen::Tcp(host_port) => Either::Left(
Listen::Tcp {
port: host_port,
address,
} => Either::Left(
warp_server
.bind_with_graceful_shutdown(([0, 0, 0, 0], host_port), cancel)
.bind_with_graceful_shutdown((address, host_port), cancel)
.1,
),
Listen::Unix(socket) => {
Listen::Unix { socket } => {
remove_file(&socket).ok();
let listener = UnixListener::bind(&socket)?;