mirror of
https://codeberg.org/icewind/tasproxy.git
synced 2026-06-03 10:14:09 +02:00
allow using config file instead of env
This commit is contained in:
parent
4ff6bdeab5
commit
eb35a4abad
5 changed files with 306 additions and 30 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
||||||
/target
|
/target
|
||||||
.env
|
.env
|
||||||
.direnv
|
.direnv
|
||||||
result
|
result
|
||||||
|
config.toml
|
||||||
184
Cargo.lock
generated
184
Cargo.lock
generated
|
|
@ -17,6 +17,55 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
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]]
|
[[package]]
|
||||||
name = "async-stream"
|
name = "async-stream"
|
||||||
version = "0.3.6"
|
version = "0.3.6"
|
||||||
|
|
@ -126,6 +175,46 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
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]]
|
[[package]]
|
||||||
name = "color-eyre"
|
name = "color-eyre"
|
||||||
version = "0.6.3"
|
version = "0.6.3"
|
||||||
|
|
@ -153,6 +242,12 @@ dependencies = [
|
||||||
"tracing-error",
|
"tracing-error",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colorchoice"
|
||||||
|
version = "1.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "core-foundation"
|
name = "core-foundation"
|
||||||
version = "0.9.4"
|
version = "0.9.4"
|
||||||
|
|
@ -425,6 +520,12 @@ dependencies = [
|
||||||
"http 0.2.12",
|
"http 0.2.12",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
@ -557,6 +658,12 @@ version = "2.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
|
checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "is_terminal_polyfill"
|
||||||
|
version = "1.70.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.11"
|
version = "1.0.11"
|
||||||
|
|
@ -1014,6 +1121,15 @@ dependencies = [
|
||||||
"untrusted",
|
"untrusted",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "secretfile"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "746c54b939ab8d393b536765393c0bd7634fca94eed62321ec3e3559293f6c21"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "security-framework"
|
name = "security-framework"
|
||||||
version = "2.11.1"
|
version = "2.11.1"
|
||||||
|
|
@ -1069,6 +1185,15 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_spanned"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
|
@ -1150,6 +1275,12 @@ dependencies = [
|
||||||
"lock_api",
|
"lock_api",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "strsim"
|
||||||
|
version = "0.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "subtle"
|
name = "subtle"
|
||||||
version = "2.6.1"
|
version = "2.6.1"
|
||||||
|
|
@ -1200,6 +1331,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-stream",
|
"async-stream",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"dashmap",
|
"dashmap",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
|
@ -1208,8 +1340,11 @@ dependencies = [
|
||||||
"json",
|
"json",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
"rumqttc",
|
"rumqttc",
|
||||||
|
"secretfile",
|
||||||
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
|
"toml",
|
||||||
"warp",
|
"warp",
|
||||||
"warp-reverse-proxy",
|
"warp-reverse-proxy",
|
||||||
]
|
]
|
||||||
|
|
@ -1344,6 +1479,40 @@ dependencies = [
|
||||||
"tokio",
|
"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]]
|
[[package]]
|
||||||
name = "tower-service"
|
name = "tower-service"
|
||||||
version = "0.3.3"
|
version = "0.3.3"
|
||||||
|
|
@ -1473,6 +1642,12 @@ version = "0.7.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utf8parse"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "valuable"
|
name = "valuable"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
@ -1806,6 +1981,15 @@ version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.6.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winreg"
|
name = "winreg"
|
||||||
version = "0.50.0"
|
version = "0.50.0"
|
||||||
|
|
|
||||||
|
|
@ -21,3 +21,7 @@ hostname = "0.4.0"
|
||||||
warp-reverse-proxy = { version = "1.0.0", default-features = false, features = ["rustls-tls"] }
|
warp-reverse-proxy = { version = "1.0.0", default-features = false, features = ["rustls-tls"] }
|
||||||
tokio-stream = { version = "0.1.16", features = ["net"] }
|
tokio-stream = { version = "0.1.16", features = ["net"] }
|
||||||
base64 = "0.22.1"
|
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"] }
|
||||||
|
|
|
||||||
118
src/config.rs
118
src/config.rs
|
|
@ -1,38 +1,101 @@
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use color_eyre::{eyre::WrapErr, Report, Result};
|
use color_eyre::{eyre::WrapErr, Report, Result};
|
||||||
use rumqttc::MqttOptions;
|
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::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub mqtt_host: String,
|
|
||||||
pub mqtt_port: u16,
|
|
||||||
pub mqtt_credentials: Option<Credentials>,
|
|
||||||
pub listen: Listen,
|
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 {
|
pub enum Listen {
|
||||||
Tcp(u16),
|
Tcp {
|
||||||
Unix(String),
|
#[serde(default = "default_address")]
|
||||||
|
address: IpAddr,
|
||||||
|
port: u16,
|
||||||
|
},
|
||||||
|
Unix {
|
||||||
|
socket: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
fn default_address() -> IpAddr {
|
||||||
pub struct Credentials {
|
Ipv4Addr::UNSPECIFIED.into()
|
||||||
pub username: String,
|
}
|
||||||
pub password: String,
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum Credentials {
|
||||||
|
Raw {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
},
|
||||||
|
File {
|
||||||
|
username: String,
|
||||||
|
password_file: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Credentials {
|
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 {
|
pub fn auth_header(&self) -> String {
|
||||||
let mut header = "Basic ".to_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
|
header
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
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> {
|
pub fn from_env() -> Result<Self> {
|
||||||
let mqtt_host = dotenv::var("MQTT_HOSTNAME").wrap_err("MQTT_HOSTNAME not set")?;
|
let mqtt_host = dotenv::var("MQTT_HOSTNAME").wrap_err("MQTT_HOSTNAME not set")?;
|
||||||
let mqtt_port = dotenv::var("MQTT_PORT")
|
let mqtt_port = dotenv::var("MQTT_PORT")
|
||||||
|
|
@ -40,13 +103,16 @@ impl Config {
|
||||||
.and_then(|port| u16::from_str(&port).ok())
|
.and_then(|port| u16::from_str(&port).ok())
|
||||||
.unwrap_or(1883);
|
.unwrap_or(1883);
|
||||||
let listen = match dotenv::var("SOCKET") {
|
let listen = match dotenv::var("SOCKET") {
|
||||||
Ok(socket) => Listen::Unix(socket),
|
Ok(socket) => Listen::Unix { socket },
|
||||||
_ => {
|
_ => {
|
||||||
let port = dotenv::var("PORT")
|
let port = dotenv::var("PORT")
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|port| u16::from_str(&port).ok())
|
.and_then(|port| u16::from_str(&port).ok())
|
||||||
.unwrap_or(80);
|
.unwrap_or(80);
|
||||||
Listen::Tcp(port)
|
Listen::Tcp {
|
||||||
|
address: default_address(),
|
||||||
|
port,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -54,7 +120,7 @@ impl Config {
|
||||||
Ok(username) => {
|
Ok(username) => {
|
||||||
let password = dotenv::var("MQTT_PASSWORD")
|
let password = dotenv::var("MQTT_PASSWORD")
|
||||||
.wrap_err("MQTT_USERNAME set, but MQTT_PASSWORD not set")?;
|
.wrap_err("MQTT_USERNAME set, but MQTT_PASSWORD not set")?;
|
||||||
Some(Credentials { username, password })
|
Some(Credentials::Raw { username, password })
|
||||||
}
|
}
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
@ -63,16 +129,20 @@ impl Config {
|
||||||
Ok(username) => {
|
Ok(username) => {
|
||||||
let password = dotenv::var("TASMOTA_PASSWORD")
|
let password = dotenv::var("TASMOTA_PASSWORD")
|
||||||
.wrap_err("TASMOTA_USERNAME set, but TASMOTA_PASSWORD not set")?;
|
.wrap_err("TASMOTA_USERNAME set, but TASMOTA_PASSWORD not set")?;
|
||||||
Some(Credentials { username, password })
|
Some(Credentials::Raw { username, password })
|
||||||
}
|
}
|
||||||
Err(_) => None,
|
Err(_) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
mqtt_host,
|
mqtt: MqttConfig {
|
||||||
mqtt_port,
|
host: mqtt_host,
|
||||||
mqtt_credentials,
|
port: mqtt_port,
|
||||||
tasmota_credentials,
|
credentials: mqtt_credentials,
|
||||||
|
},
|
||||||
|
tasmota: TasmotaConfig {
|
||||||
|
credentials: tasmota_credentials,
|
||||||
|
},
|
||||||
listen,
|
listen,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -83,11 +153,11 @@ impl Config {
|
||||||
.map_err(|_| Report::msg("invalid hostname"))?;
|
.map_err(|_| Report::msg("invalid hostname"))?;
|
||||||
let mut mqtt_options = MqttOptions::new(
|
let mut mqtt_options = MqttOptions::new(
|
||||||
format!("tasproxy-{}", hostname),
|
format!("tasproxy-{}", hostname),
|
||||||
&self.mqtt_host,
|
&self.mqtt.host,
|
||||||
self.mqtt_port,
|
self.mqtt.port,
|
||||||
);
|
);
|
||||||
if let Some(credentials) = self.mqtt_credentials.as_ref() {
|
if let Some(credentials) = self.mqtt.credentials.as_ref() {
|
||||||
mqtt_options.set_credentials(&credentials.username, &credentials.password);
|
mqtt_options.set_credentials(credentials.username(), credentials.password());
|
||||||
}
|
}
|
||||||
mqtt_options.set_keep_alive(Duration::from_secs(5));
|
mqtt_options.set_keep_alive(Duration::from_secs(5));
|
||||||
Ok(mqtt_options)
|
Ok(mqtt_options)
|
||||||
|
|
|
||||||
27
src/main.rs
27
src/main.rs
|
|
@ -2,6 +2,7 @@ use crate::config::{Config, Listen};
|
||||||
use crate::devices::{Device, DeviceState};
|
use crate::devices::{Device, DeviceState};
|
||||||
use crate::mqtt::mqtt_stream;
|
use crate::mqtt::mqtt_stream;
|
||||||
use crate::topic::Topic;
|
use crate::topic::Topic;
|
||||||
|
use clap::Parser;
|
||||||
use color_eyre::{eyre::WrapErr, Result};
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use futures_util::future::{Either, FutureExt};
|
use futures_util::future::{Either, FutureExt};
|
||||||
|
|
@ -32,12 +33,25 @@ mod topic;
|
||||||
|
|
||||||
type DeviceStates = Arc<DashMap<String, DeviceState>>;
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
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 listen = config.listen.clone();
|
||||||
let tasmota_credentials = config
|
let tasmota_credentials = config
|
||||||
.tasmota_credentials
|
.tasmota
|
||||||
|
.credentials
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|auth| auth.auth_header());
|
.map(|auth| auth.auth_header());
|
||||||
|
|
||||||
|
|
@ -114,12 +128,15 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let warp_server = warp::serve(proxy);
|
let warp_server = warp::serve(proxy);
|
||||||
let server = match listen {
|
let server = match listen {
|
||||||
Listen::Tcp(host_port) => Either::Left(
|
Listen::Tcp {
|
||||||
|
port: host_port,
|
||||||
|
address,
|
||||||
|
} => Either::Left(
|
||||||
warp_server
|
warp_server
|
||||||
.bind_with_graceful_shutdown(([0, 0, 0, 0], host_port), cancel)
|
.bind_with_graceful_shutdown((address, host_port), cancel)
|
||||||
.1,
|
.1,
|
||||||
),
|
),
|
||||||
Listen::Unix(socket) => {
|
Listen::Unix { socket } => {
|
||||||
remove_file(&socket).ok();
|
remove_file(&socket).ok();
|
||||||
|
|
||||||
let listener = UnixListener::bind(&socket)?;
|
let listener = UnixListener::bind(&socket)?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue