mirror of
https://github.com/aramperes/nut-rs.git
synced 2025-09-08 05:08:31 -04:00
parent
d78fd8c141
commit
d36999db6d
16 changed files with 494 additions and 104 deletions
7
.github/workflows/ci.yml
vendored
7
.github/workflows/ci.yml
vendored
|
@ -81,7 +81,12 @@ jobs:
|
||||||
uses: actions-rs/cargo@v1
|
uses: actions-rs/cargo@v1
|
||||||
with:
|
with:
|
||||||
command: test
|
command: test
|
||||||
args: --features env-file
|
|
||||||
|
- name: Run cargo test with ssl
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: test
|
||||||
|
args: --features ssl
|
||||||
|
|
||||||
lints:
|
lints:
|
||||||
name: Lints
|
name: Lints
|
||||||
|
|
222
Cargo.lock
generated
222
Cargo.lock
generated
|
@ -28,12 +28,36 @@ dependencies = [
|
||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base64"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bumpalo"
|
||||||
|
version = "3.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cc"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap"
|
name = "clap"
|
||||||
version = "2.33.3"
|
version = "2.33.3"
|
||||||
|
@ -49,12 +73,6 @@ dependencies = [
|
||||||
"vec_map",
|
"vec_map",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "dotenv"
|
|
||||||
version = "0.15.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.1.19"
|
version = "0.1.19"
|
||||||
|
@ -64,18 +82,82 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "js-sys"
|
||||||
|
version = "0.3.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062"
|
||||||
|
dependencies = [
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.98"
|
version = "0.2.98"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
|
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "log"
|
||||||
|
version = "0.4.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nut-client"
|
name = "nut-client"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"dotenv",
|
"rustls",
|
||||||
"shell-words",
|
"shell-words",
|
||||||
|
"webpki",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.28"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ring"
|
||||||
|
version = "0.16.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3053cf52e236a3ed746dfc745aa9cacf1b791d846bdaf412f60a8d7d6e17c8fc"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
"spin",
|
||||||
|
"untrusted",
|
||||||
|
"web-sys",
|
||||||
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -87,18 +169,58 @@ dependencies = [
|
||||||
"nut-client",
|
"nut-client",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustls"
|
||||||
|
version = "0.19.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7"
|
||||||
|
dependencies = [
|
||||||
|
"base64",
|
||||||
|
"log",
|
||||||
|
"ring",
|
||||||
|
"sct",
|
||||||
|
"webpki",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sct"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce"
|
||||||
|
dependencies = [
|
||||||
|
"ring",
|
||||||
|
"untrusted",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "shell-words"
|
name = "shell-words"
|
||||||
version = "1.0.0"
|
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 = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074"
|
checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spin"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-xid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "textwrap"
|
name = "textwrap"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
|
@ -114,12 +236,98 @@ version = "0.1.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "untrusted"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "vec_map"
|
name = "vec_map"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen"
|
||||||
|
version = "0.2.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"wasm-bindgen-macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-backend"
|
||||||
|
version = "0.2.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900"
|
||||||
|
dependencies = [
|
||||||
|
"bumpalo",
|
||||||
|
"lazy_static",
|
||||||
|
"log",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
"wasm-bindgen-shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-macro"
|
||||||
|
version = "0.2.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4"
|
||||||
|
dependencies = [
|
||||||
|
"quote",
|
||||||
|
"wasm-bindgen-macro-support",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-macro-support"
|
||||||
|
version = "0.2.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
"wasm-bindgen-backend",
|
||||||
|
"wasm-bindgen-shared",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-bindgen-shared"
|
||||||
|
version = "0.2.74"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "web-sys"
|
||||||
|
version = "0.3.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582"
|
||||||
|
dependencies = [
|
||||||
|
"js-sys",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "webpki"
|
||||||
|
version = "0.21.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b8e38c0608262c46d4a56202ebabdeb094cef7e560ca7a226c6bf055188aa4ea"
|
||||||
|
dependencies = [
|
||||||
|
"ring",
|
||||||
|
"untrusted",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
|
|
12
README.md
12
README.md
|
@ -11,6 +11,7 @@ A [Network UPS Tools](https://github.com/networkupstools/nut) (NUT) client libra
|
||||||
- Login with username and password
|
- Login with username and password
|
||||||
- List UPS devices
|
- List UPS devices
|
||||||
- List variables for a UPS device
|
- List variables for a UPS device
|
||||||
|
- Connect securely with SSL (optional feature)
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
|
@ -60,7 +61,7 @@ fn main() -> nut_client::Result<()> {
|
||||||
.with_debug(false) // Turn this on for debugging network chatter
|
.with_debug(false) // Turn this on for debugging network chatter
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut conn = Connection::new(config)?;
|
let mut conn = Connection::new(&config)?;
|
||||||
|
|
||||||
// Print a list of all UPS devices
|
// Print a list of all UPS devices
|
||||||
println!("Connected UPS devices:");
|
println!("Connected UPS devices:");
|
||||||
|
@ -78,3 +79,12 @@ fn main() -> nut_client::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## SSL
|
||||||
|
|
||||||
|
You can turn on SSL support by adding `.with_ssl(true)` in the `ConfigBuilder`.
|
||||||
|
This requires the `ssl` feature, which uses `rustls` under the hood.
|
||||||
|
|
||||||
|
Note that this crate turns off all certificate validation at the moment, effectively
|
||||||
|
giving a false sense of security. If you'd like to contribute to this, see issue #8.
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,8 @@ license = "MIT"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
shell-words = "1.0.0"
|
shell-words = "1.0.0"
|
||||||
dotenv = { version = "0.15.0", optional = true }
|
rustls = { version = "0.19", optional = true, features = ["dangerous_configuration"] }
|
||||||
|
webpki = { version = "0.21", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
env-file = ["dotenv"]
|
ssl = ["rustls", "webpki"]
|
||||||
|
|
|
@ -22,7 +22,7 @@ fn main() -> nut_client::Result<()> {
|
||||||
.with_debug(false) // Turn this on for debugging network chatter
|
.with_debug(false) // Turn this on for debugging network chatter
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let mut conn = Connection::new(config)?;
|
let mut conn = Connection::new(&config)?;
|
||||||
|
|
||||||
// Print a list of all UPS devices
|
// Print a list of all UPS devices
|
||||||
println!("Connected UPS devices:");
|
println!("Connected UPS devices:");
|
||||||
|
|
47
nut-client/src/blocking/filter.rs
Normal file
47
nut-client/src/blocking/filter.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::net::TcpStream;
|
||||||
|
|
||||||
|
#[allow(clippy::large_enum_variant)]
|
||||||
|
pub enum ConnectionPipeline {
|
||||||
|
Tcp(TcpStream),
|
||||||
|
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
Ssl(rustls::StreamOwned<rustls::ClientSession, TcpStream>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnectionPipeline {
|
||||||
|
pub fn tcp(&self) -> Option<TcpStream> {
|
||||||
|
match self {
|
||||||
|
Self::Tcp(stream) => Some(stream.try_clone().ok()).flatten(),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Read for ConnectionPipeline {
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||||
|
match self {
|
||||||
|
Self::Tcp(stream) => stream.read(buf),
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
Self::Ssl(stream) => stream.read(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for ConnectionPipeline {
|
||||||
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
|
match self {
|
||||||
|
Self::Tcp(stream) => stream.write(buf),
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
Self::Ssl(stream) => stream.write(buf),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
|
match self {
|
||||||
|
Self::Tcp(stream) => stream.flush(),
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
Self::Ssl(stream) => stream.flush(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
use std::io;
|
|
||||||
use std::io::{BufRead, BufReader, Write};
|
use std::io::{BufRead, BufReader, Write};
|
||||||
use std::net::{SocketAddr, TcpStream};
|
use std::net::{SocketAddr, TcpStream};
|
||||||
|
|
||||||
|
use crate::blocking::filter::ConnectionPipeline;
|
||||||
use crate::cmd::{Command, Response};
|
use crate::cmd::{Command, Response};
|
||||||
use crate::{Config, Host, NutError, Variable};
|
use crate::{ClientError, Config, Host, NutError, Variable};
|
||||||
|
|
||||||
|
mod filter;
|
||||||
|
|
||||||
/// A blocking NUT client connection.
|
/// A blocking NUT client connection.
|
||||||
pub enum Connection {
|
pub enum Connection {
|
||||||
|
@ -13,7 +15,7 @@ pub enum Connection {
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
/// Initializes a connection to a NUT server (upsd).
|
/// Initializes a connection to a NUT server (upsd).
|
||||||
pub fn new(config: Config) -> crate::Result<Self> {
|
pub fn new(config: &Config) -> crate::Result<Self> {
|
||||||
match &config.host {
|
match &config.host {
|
||||||
Host::Tcp(socket_addr) => {
|
Host::Tcp(socket_addr) => {
|
||||||
Ok(Self::Tcp(TcpConnection::new(config.clone(), socket_addr)?))
|
Ok(Self::Tcp(TcpConnection::new(config.clone(), socket_addr)?))
|
||||||
|
@ -58,17 +60,22 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A blocking TCP NUT client connection.
|
/// A blocking TCP NUT client connection.
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct TcpConnection {
|
pub struct TcpConnection {
|
||||||
config: Config,
|
config: Config,
|
||||||
tcp_stream: TcpStream,
|
pipeline: ConnectionPipeline,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TcpConnection {
|
impl TcpConnection {
|
||||||
fn new(config: Config, socket_addr: &SocketAddr) -> crate::Result<Self> {
|
fn new(config: Config, socket_addr: &SocketAddr) -> crate::Result<Self> {
|
||||||
// Create the TCP connection
|
// Create the TCP connection
|
||||||
let tcp_stream = TcpStream::connect_timeout(socket_addr, config.timeout)?;
|
let tcp_stream = TcpStream::connect_timeout(socket_addr, config.timeout)?;
|
||||||
let mut connection = Self { config, tcp_stream };
|
let mut connection = Self {
|
||||||
|
config,
|
||||||
|
pipeline: ConnectionPipeline::Tcp(tcp_stream),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initialize SSL connection
|
||||||
|
connection.enable_ssl()?;
|
||||||
|
|
||||||
// Attempt login using `config.auth`
|
// Attempt login using `config.auth`
|
||||||
connection.login()?;
|
connection.login()?;
|
||||||
|
@ -76,84 +83,114 @@ impl TcpConnection {
|
||||||
Ok(connection)
|
Ok(connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
fn enable_ssl(&mut self) -> crate::Result<()> {
|
||||||
|
if self.config.ssl {
|
||||||
|
// Send TLS request and check for 'OK'
|
||||||
|
self.write_cmd(Command::StartTLS)?;
|
||||||
|
self.read_response()
|
||||||
|
.map_err(|e| {
|
||||||
|
if let ClientError::Nut(NutError::FeatureNotConfigured) = e {
|
||||||
|
ClientError::Nut(NutError::SslNotSupported)
|
||||||
|
} else {
|
||||||
|
e
|
||||||
|
}
|
||||||
|
})?
|
||||||
|
.expect_ok()?;
|
||||||
|
|
||||||
|
let mut config = rustls::ClientConfig::new();
|
||||||
|
config
|
||||||
|
.dangerous()
|
||||||
|
.set_certificate_verifier(std::sync::Arc::new(
|
||||||
|
crate::ssl::NutCertificateValidator::new(&self.config),
|
||||||
|
));
|
||||||
|
|
||||||
|
// todo: this DNS name is temporary; should get from connection hostname? (#8)
|
||||||
|
let dns_name = webpki::DNSNameRef::try_from_ascii_str("www.google.com").unwrap();
|
||||||
|
let sess = rustls::ClientSession::new(&std::sync::Arc::new(config), dns_name);
|
||||||
|
|
||||||
|
// Wrap and override the TCP stream
|
||||||
|
let tcp = self
|
||||||
|
.pipeline
|
||||||
|
.tcp()
|
||||||
|
.ok_or_else(|| ClientError::from(NutError::SslNotSupported))?;
|
||||||
|
let tls = rustls::StreamOwned::new(sess, tcp);
|
||||||
|
self.pipeline = ConnectionPipeline::Ssl(tls);
|
||||||
|
|
||||||
|
// Send a test command
|
||||||
|
self.get_network_version()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "ssl"))]
|
||||||
|
fn enable_ssl(&mut self) -> crate::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn login(&mut self) -> crate::Result<()> {
|
fn login(&mut self) -> crate::Result<()> {
|
||||||
if let Some(auth) = &self.config.auth {
|
if let Some(auth) = self.config.auth.clone() {
|
||||||
// Pass username and check for 'OK'
|
// Pass username and check for 'OK'
|
||||||
Self::write_cmd(
|
self.write_cmd(Command::SetUsername(&auth.username))?;
|
||||||
&mut self.tcp_stream,
|
self.read_response()?.expect_ok()?;
|
||||||
Command::SetUsername(&auth.username),
|
|
||||||
self.config.debug,
|
|
||||||
)?;
|
|
||||||
Self::read_response(&mut self.tcp_stream, self.config.debug)?.expect_ok()?;
|
|
||||||
|
|
||||||
// Pass password and check for 'OK'
|
// Pass password and check for 'OK'
|
||||||
if let Some(password) = &auth.password {
|
if let Some(password) = &auth.password {
|
||||||
Self::write_cmd(
|
self.write_cmd(Command::SetPassword(password))?;
|
||||||
&mut self.tcp_stream,
|
self.read_response()?.expect_ok()?;
|
||||||
Command::SetPassword(password),
|
|
||||||
self.config.debug,
|
|
||||||
)?;
|
|
||||||
Self::read_response(&mut self.tcp_stream, self.config.debug)?.expect_ok()?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
|
fn list_ups(&mut self) -> crate::Result<Vec<(String, String)>> {
|
||||||
Self::write_cmd(
|
let query = &["UPS"];
|
||||||
&mut self.tcp_stream,
|
self.write_cmd(Command::List(query))?;
|
||||||
Command::List(&["UPS"]),
|
|
||||||
self.config.debug,
|
|
||||||
)?;
|
|
||||||
let list = Self::read_list(&mut self.tcp_stream, &["UPS"], self.config.debug)?;
|
|
||||||
|
|
||||||
|
let list = self.read_list(query)?;
|
||||||
list.into_iter().map(|row| row.expect_ups()).collect()
|
list.into_iter().map(|row| row.expect_ups()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_clients(&mut self, ups_name: &str) -> crate::Result<Vec<String>> {
|
fn list_clients(&mut self, ups_name: &str) -> crate::Result<Vec<String>> {
|
||||||
let query = &["CLIENT", ups_name];
|
let query = &["CLIENT", ups_name];
|
||||||
Self::write_cmd(
|
self.write_cmd(Command::List(query))?;
|
||||||
&mut self.tcp_stream,
|
|
||||||
Command::List(query),
|
|
||||||
self.config.debug,
|
|
||||||
)?;
|
|
||||||
let list = Self::read_list(&mut self.tcp_stream, query, self.config.debug)?;
|
|
||||||
|
|
||||||
|
let list = self.read_list(query)?;
|
||||||
list.into_iter().map(|row| row.expect_client()).collect()
|
list.into_iter().map(|row| row.expect_client()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
|
fn list_vars(&mut self, ups_name: &str) -> crate::Result<Vec<(String, String)>> {
|
||||||
let query = &["VAR", ups_name];
|
let query = &["VAR", ups_name];
|
||||||
Self::write_cmd(
|
self.write_cmd(Command::List(query))?;
|
||||||
&mut self.tcp_stream,
|
|
||||||
Command::List(query),
|
|
||||||
self.config.debug,
|
|
||||||
)?;
|
|
||||||
let list = Self::read_list(&mut self.tcp_stream, query, self.config.debug)?;
|
|
||||||
|
|
||||||
|
let list = self.read_list(query)?;
|
||||||
list.into_iter().map(|row| row.expect_var()).collect()
|
list.into_iter().map(|row| row.expect_var()).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> {
|
fn get_var(&mut self, ups_name: &str, variable: &str) -> crate::Result<(String, String)> {
|
||||||
let query = &["VAR", ups_name, variable];
|
let query = &["VAR", ups_name, variable];
|
||||||
Self::write_cmd(&mut self.tcp_stream, Command::Get(query), self.config.debug)?;
|
self.write_cmd(Command::Get(query))?;
|
||||||
|
|
||||||
let resp = Self::read_response(&mut self.tcp_stream, self.config.debug)?;
|
self.read_response()?.expect_var()
|
||||||
resp.expect_var()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_cmd(stream: &mut TcpStream, line: Command, debug: bool) -> crate::Result<()> {
|
fn get_network_version(&mut self) -> crate::Result<String> {
|
||||||
|
self.write_cmd(Command::NetworkVersion)?;
|
||||||
|
self.read_plain_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_cmd(&mut self, line: Command) -> crate::Result<()> {
|
||||||
let line = format!("{}\n", line);
|
let line = format!("{}\n", line);
|
||||||
if debug {
|
if self.config.debug {
|
||||||
eprint!("DEBUG -> {}", line);
|
eprint!("DEBUG -> {}", line);
|
||||||
}
|
}
|
||||||
stream.write_all(line.as_bytes())?;
|
self.pipeline.write_all(line.as_bytes())?;
|
||||||
stream.flush()?;
|
self.pipeline.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_line(
|
fn parse_line(
|
||||||
reader: &mut BufReader<&mut TcpStream>,
|
reader: &mut BufReader<&mut ConnectionPipeline>,
|
||||||
debug: bool,
|
debug: bool,
|
||||||
) -> crate::Result<Vec<String>> {
|
) -> crate::Result<Vec<String>> {
|
||||||
let mut raw = String::new();
|
let mut raw = String::new();
|
||||||
|
@ -170,25 +207,27 @@ impl TcpConnection {
|
||||||
Ok(args)
|
Ok(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_response(stream: &mut TcpStream, debug: bool) -> crate::Result<Response> {
|
fn read_response(&mut self) -> crate::Result<Response> {
|
||||||
let mut reader = io::BufReader::new(stream);
|
let mut reader = BufReader::new(&mut self.pipeline);
|
||||||
let args = Self::parse_line(&mut reader, debug)?;
|
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||||
Response::from_args(args)
|
Response::from_args(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_list(
|
fn read_plain_response(&mut self) -> crate::Result<String> {
|
||||||
stream: &mut TcpStream,
|
let mut reader = BufReader::new(&mut self.pipeline);
|
||||||
query: &[&str],
|
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||||
debug: bool,
|
Ok(args.join(" "))
|
||||||
) -> crate::Result<Vec<Response>> {
|
}
|
||||||
let mut reader = io::BufReader::new(stream);
|
|
||||||
let args = Self::parse_line(&mut reader, debug)?;
|
fn read_list(&mut self, query: &[&str]) -> crate::Result<Vec<Response>> {
|
||||||
|
let mut reader = BufReader::new(&mut self.pipeline);
|
||||||
|
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||||
|
|
||||||
Response::from_args(args)?.expect_begin_list(query)?;
|
Response::from_args(args)?.expect_begin_list(query)?;
|
||||||
let mut lines: Vec<Response> = Vec::new();
|
let mut lines: Vec<Response> = Vec::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let args = Self::parse_line(&mut reader, debug)?;
|
let args = Self::parse_line(&mut reader, self.config.debug)?;
|
||||||
let resp = Response::from_args(args)?;
|
let resp = Response::from_args(args)?;
|
||||||
|
|
||||||
match resp {
|
match resp {
|
||||||
|
|
|
@ -11,6 +11,10 @@ pub enum Command<'a> {
|
||||||
SetPassword(&'a str),
|
SetPassword(&'a str),
|
||||||
/// Queries for a list. Allows for any number of arguments, which forms a single query.
|
/// Queries for a list. Allows for any number of arguments, which forms a single query.
|
||||||
List(&'a [&'a str]),
|
List(&'a [&'a str]),
|
||||||
|
/// Tells upsd to switch to TLS, so all future communications will be encrypted.
|
||||||
|
StartTLS,
|
||||||
|
/// Queries the network version.
|
||||||
|
NetworkVersion,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Command<'a> {
|
impl<'a> Command<'a> {
|
||||||
|
@ -21,6 +25,8 @@ impl<'a> Command<'a> {
|
||||||
Self::SetUsername(_) => "USERNAME",
|
Self::SetUsername(_) => "USERNAME",
|
||||||
Self::SetPassword(_) => "PASSWORD",
|
Self::SetPassword(_) => "PASSWORD",
|
||||||
Self::List(_) => "LIST",
|
Self::List(_) => "LIST",
|
||||||
|
Self::StartTLS => "STARTTLS",
|
||||||
|
Self::NetworkVersion => "NETVER",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,6 +37,7 @@ impl<'a> Command<'a> {
|
||||||
Self::SetUsername(username) => vec![username],
|
Self::SetUsername(username) => vec![username],
|
||||||
Self::SetPassword(password) => vec![password],
|
Self::SetPassword(password) => vec![password],
|
||||||
Self::List(query) => query.to_vec(),
|
Self::List(query) => query.to_vec(),
|
||||||
|
_ => Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,6 +90,7 @@ impl Response {
|
||||||
match err_type.as_str() {
|
match err_type.as_str() {
|
||||||
"ACCESS-DENIED" => Err(NutError::AccessDenied.into()),
|
"ACCESS-DENIED" => Err(NutError::AccessDenied.into()),
|
||||||
"UNKNOWN-UPS" => Err(NutError::UnknownUps.into()),
|
"UNKNOWN-UPS" => Err(NutError::UnknownUps.into()),
|
||||||
|
"FEATURE-NOT-CONFIGURED" => Err(NutError::FeatureNotConfigured.into()),
|
||||||
_ => Err(NutError::Generic(format!(
|
_ => Err(NutError::Generic(format!(
|
||||||
"Server error: {} {}",
|
"Server error: {} {}",
|
||||||
err_type,
|
err_type,
|
||||||
|
|
|
@ -58,16 +58,18 @@ pub struct Config {
|
||||||
pub(crate) host: Host,
|
pub(crate) host: Host,
|
||||||
pub(crate) auth: Option<Auth>,
|
pub(crate) auth: Option<Auth>,
|
||||||
pub(crate) timeout: Duration,
|
pub(crate) timeout: Duration,
|
||||||
|
pub(crate) ssl: bool,
|
||||||
pub(crate) debug: bool,
|
pub(crate) debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Creates a connection configuration.
|
/// Creates a connection configuration.
|
||||||
pub fn new(host: Host, auth: Option<Auth>, timeout: Duration, debug: bool) -> Self {
|
pub fn new(host: Host, auth: Option<Auth>, timeout: Duration, ssl: bool, debug: bool) -> Self {
|
||||||
Config {
|
Config {
|
||||||
host,
|
host,
|
||||||
auth,
|
auth,
|
||||||
timeout,
|
timeout,
|
||||||
|
ssl,
|
||||||
debug,
|
debug,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,6 +81,7 @@ pub struct ConfigBuilder {
|
||||||
host: Option<Host>,
|
host: Option<Host>,
|
||||||
auth: Option<Auth>,
|
auth: Option<Auth>,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
|
ssl: Option<bool>,
|
||||||
debug: Option<bool>,
|
debug: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,6 +110,13 @@ impl ConfigBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enables SSL on the connection.
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
pub fn with_ssl(mut self, ssl: bool) -> Self {
|
||||||
|
self.ssl = Some(ssl);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Enables debugging network calls by printing to stderr.
|
/// Enables debugging network calls by printing to stderr.
|
||||||
pub fn with_debug(mut self, debug: bool) -> Self {
|
pub fn with_debug(mut self, debug: bool) -> Self {
|
||||||
self.debug = Some(debug);
|
self.debug = Some(debug);
|
||||||
|
@ -119,6 +129,7 @@ impl ConfigBuilder {
|
||||||
self.host.unwrap_or_default(),
|
self.host.unwrap_or_default(),
|
||||||
self.auth,
|
self.auth,
|
||||||
self.timeout.unwrap_or_else(|| Duration::from_secs(5)),
|
self.timeout.unwrap_or_else(|| Duration::from_secs(5)),
|
||||||
|
self.ssl.unwrap_or(false),
|
||||||
self.debug.unwrap_or(false),
|
self.debug.unwrap_or(false),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,11 @@ pub enum NutError {
|
||||||
UnexpectedResponse,
|
UnexpectedResponse,
|
||||||
/// Occurs when the response type is not recognized by the client.
|
/// Occurs when the response type is not recognized by the client.
|
||||||
UnknownResponseType(String),
|
UnknownResponseType(String),
|
||||||
|
/// Occurs when attempting to use SSL in a transport that doesn't support it, or
|
||||||
|
/// if the server is not configured for it.
|
||||||
|
SslNotSupported,
|
||||||
|
/// Occurs when the client used a feature that is disabled by the server.
|
||||||
|
FeatureNotConfigured,
|
||||||
/// Generic (usually internal) client error.
|
/// Generic (usually internal) client error.
|
||||||
Generic(String),
|
Generic(String),
|
||||||
}
|
}
|
||||||
|
@ -23,6 +28,8 @@ impl fmt::Display for NutError {
|
||||||
Self::UnknownUps => write!(f, "Unknown UPS device name"),
|
Self::UnknownUps => write!(f, "Unknown UPS device name"),
|
||||||
Self::UnexpectedResponse => write!(f, "Unexpected server response content"),
|
Self::UnexpectedResponse => write!(f, "Unexpected server response content"),
|
||||||
Self::UnknownResponseType(ty) => write!(f, "Unknown response type: {}", ty),
|
Self::UnknownResponseType(ty) => write!(f, "Unknown response type: {}", ty),
|
||||||
|
Self::SslNotSupported => write!(f, "SSL not supported by server or transport"),
|
||||||
|
Self::FeatureNotConfigured => write!(f, "Feature not configured by server"),
|
||||||
Self::Generic(msg) => write!(f, "Internal client error: {}", msg),
|
Self::Generic(msg) => write!(f, "Internal client error: {}", msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,4 +15,6 @@ pub mod blocking;
|
||||||
mod cmd;
|
mod cmd;
|
||||||
mod config;
|
mod config;
|
||||||
mod error;
|
mod error;
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
mod ssl;
|
||||||
mod var;
|
mod var;
|
||||||
|
|
40
nut-client/src/ssl/mod.rs
Normal file
40
nut-client/src/ssl/mod.rs
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
use crate::Config;
|
||||||
|
|
||||||
|
/// The certificate validation mechanism for NUT.
|
||||||
|
pub struct NutCertificateValidator {
|
||||||
|
debug: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NutCertificateValidator {
|
||||||
|
/// Initialize a new instance.
|
||||||
|
pub fn new(config: &Config) -> Self {
|
||||||
|
NutCertificateValidator {
|
||||||
|
debug: config.debug,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl rustls::ServerCertVerifier for NutCertificateValidator {
|
||||||
|
fn verify_server_cert(
|
||||||
|
&self,
|
||||||
|
_roots: &rustls::RootCertStore,
|
||||||
|
presented_certs: &[rustls::Certificate],
|
||||||
|
_dns_name: webpki::DNSNameRef<'_>,
|
||||||
|
_ocsp: &[u8],
|
||||||
|
) -> Result<rustls::ServerCertVerified, rustls::TLSError> {
|
||||||
|
// todo: verify certificates, but not hostnames
|
||||||
|
|
||||||
|
if self.debug {
|
||||||
|
let parsed = webpki::EndEntityCert::from(presented_certs[0].0.as_slice()).ok();
|
||||||
|
if let Some(_parsed) = parsed {
|
||||||
|
eprintln!("DEBUG <- Certificate received and parsed");
|
||||||
|
// todo: reading values here... https://github.com/briansmith/webpki/pull/103
|
||||||
|
} else {
|
||||||
|
eprintln!("DEBUG <- Certificate not-parseable");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// trust everything for now
|
||||||
|
Ok(rustls::ServerCertVerified::assertion())
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,3 +19,4 @@ anyhow = "1"
|
||||||
[dependencies.nut-client]
|
[dependencies.nut-client]
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
path = "../nut-client"
|
path = "../nut-client"
|
||||||
|
features = ["ssl"]
|
||||||
|
|
|
@ -15,6 +15,7 @@ Written using the [nut-client](https://github.com/aramperes/nut-client-rs) crate
|
||||||
- List variables for a UPS device
|
- List variables for a UPS device
|
||||||
- Get variable value of a UPS device
|
- Get variable value of a UPS device
|
||||||
- List clients connected to a UPS device
|
- List clients connected to a UPS device
|
||||||
|
- Connect securely with SSL
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
@ -58,6 +59,9 @@ However, there are also some additions:
|
||||||
```bash
|
```bash
|
||||||
# Enable network debugging (global flag).
|
# Enable network debugging (global flag).
|
||||||
ruspc -D
|
ruspc -D
|
||||||
|
|
||||||
|
# Enable SSL
|
||||||
|
rupsc -S
|
||||||
```
|
```
|
||||||
|
|
||||||
## Pronunciation
|
## Pronunciation
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
use crate::parser::UpsdName;
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use core::convert::TryInto;
|
|
||||||
use nut_client::blocking::Connection;
|
use nut_client::blocking::Connection;
|
||||||
|
use nut_client::Config;
|
||||||
|
|
||||||
/// Lists each UPS on the upsd server, one per line.
|
/// Lists each UPS on the upsd server, one per line.
|
||||||
pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> anyhow::Result<()> {
|
pub fn list_devices(config: Config, with_description: bool) -> anyhow::Result<()> {
|
||||||
let mut conn = connect(server, debug)?;
|
let mut conn = connect(config)?;
|
||||||
|
|
||||||
for (name, description) in conn.list_ups()? {
|
for (name, description) in conn.list_ups()? {
|
||||||
if with_description {
|
if with_description {
|
||||||
|
@ -18,11 +18,8 @@ pub fn list_devices(server: UpsdName, with_description: bool, debug: bool) -> an
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_variable(server: UpsdName, variable: &str, debug: bool) -> anyhow::Result<()> {
|
pub fn print_variable(config: Config, ups_name: &str, variable: &str) -> anyhow::Result<()> {
|
||||||
let ups_name = server
|
let mut conn = connect(config)?;
|
||||||
.upsname
|
|
||||||
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
|
|
||||||
let mut conn = connect(server, debug)?;
|
|
||||||
|
|
||||||
let variable = conn.get_var(ups_name, variable)?;
|
let variable = conn.get_var(ups_name, variable)?;
|
||||||
println!("{}", variable.value());
|
println!("{}", variable.value());
|
||||||
|
@ -30,11 +27,8 @@ pub fn print_variable(server: UpsdName, variable: &str, debug: bool) -> anyhow::
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
|
pub fn list_variables(config: Config, ups_name: &str) -> anyhow::Result<()> {
|
||||||
let ups_name = server
|
let mut conn = connect(config)?;
|
||||||
.upsname
|
|
||||||
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
|
|
||||||
let mut conn = connect(server, debug)?;
|
|
||||||
|
|
||||||
for var in conn.list_vars(ups_name)? {
|
for var in conn.list_vars(ups_name)? {
|
||||||
println!("{}", var);
|
println!("{}", var);
|
||||||
|
@ -43,11 +37,8 @@ pub fn list_variables(server: UpsdName, debug: bool) -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn list_clients(server: UpsdName, debug: bool) -> anyhow::Result<()> {
|
pub fn list_clients(config: Config, ups_name: &str) -> anyhow::Result<()> {
|
||||||
let ups_name = server
|
let mut conn = connect(config)?;
|
||||||
.upsname
|
|
||||||
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")?;
|
|
||||||
let mut conn = connect(server, debug)?;
|
|
||||||
|
|
||||||
for client_ip in conn.list_clients(ups_name)? {
|
for client_ip in conn.list_clients(ups_name)? {
|
||||||
println!("{}", client_ip);
|
println!("{}", client_ip);
|
||||||
|
@ -56,11 +47,6 @@ pub fn list_clients(server: UpsdName, debug: bool) -> anyhow::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn connect(server: UpsdName, debug: bool) -> anyhow::Result<Connection> {
|
fn connect(config: Config) -> anyhow::Result<Connection> {
|
||||||
let host = server.try_into()?;
|
Connection::new(&config).with_context(|| format!("Failed to connect to upsd: {:?}", &config))
|
||||||
let config = nut_client::ConfigBuilder::new()
|
|
||||||
.with_host(host)
|
|
||||||
.with_debug(debug)
|
|
||||||
.build();
|
|
||||||
Connection::new(config).with_context(|| format!("Failed to connect to upsd: {}", server))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,14 @@
|
||||||
///! This a Rust clone of [upsc](https://github.com/networkupstools/nut/blob/master/clients/upsc.c).
|
///! This a Rust clone of [upsc](https://github.com/networkupstools/nut/blob/master/clients/upsc.c).
|
||||||
///!
|
///!
|
||||||
///! P.S.: pronounced "r-oopsie".
|
///! P.S.: pronounced "r-oopsie".
|
||||||
mod cmd;
|
use core::convert::TryInto;
|
||||||
mod parser;
|
|
||||||
|
|
||||||
use crate::parser::UpsdName;
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use core::convert::TryInto;
|
|
||||||
|
use crate::parser::UpsdName;
|
||||||
|
mod cmd;
|
||||||
|
mod parser;
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let args = App::new(clap::crate_name!())
|
let args = App::new(clap::crate_name!())
|
||||||
|
@ -43,6 +44,12 @@ fn main() -> anyhow::Result<()> {
|
||||||
.takes_value(false)
|
.takes_value(false)
|
||||||
.help("Enables debug mode (logs network commands to stderr)."),
|
.help("Enables debug mode (logs network commands to stderr)."),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("ssl")
|
||||||
|
.short("S")
|
||||||
|
.takes_value(false)
|
||||||
|
.help("Enables SSL on the connection with upsd."),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("upsd-server")
|
Arg::with_name("upsd-server")
|
||||||
.required(false)
|
.required(false)
|
||||||
|
@ -63,23 +70,37 @@ fn main() -> anyhow::Result<()> {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let debug = args.is_present("debug");
|
let debug = args.is_present("debug");
|
||||||
|
let ssl = args.is_present("ssl");
|
||||||
|
|
||||||
|
let host = server.try_into()?;
|
||||||
|
let config = nut_client::ConfigBuilder::new()
|
||||||
|
.with_host(host)
|
||||||
|
.with_debug(debug)
|
||||||
|
.with_ssl(ssl)
|
||||||
|
.build();
|
||||||
|
|
||||||
if args.is_present("list") {
|
if args.is_present("list") {
|
||||||
return cmd::list_devices(server, false, debug);
|
return cmd::list_devices(config, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.is_present("list-full") {
|
if args.is_present("list-full") {
|
||||||
return cmd::list_devices(server, true, debug);
|
return cmd::list_devices(config, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.is_present("clients") {
|
if args.is_present("clients") {
|
||||||
return cmd::list_clients(server, debug);
|
return cmd::list_clients(config, get_ups_name(&server)?);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback: prints one variable (or all of them)
|
// Fallback: prints one variable (or all of them)
|
||||||
if let Some(variable) = args.value_of("variable") {
|
if let Some(variable) = args.value_of("variable") {
|
||||||
cmd::print_variable(server, variable, debug)
|
cmd::print_variable(config, get_ups_name(&server)?, variable)
|
||||||
} else {
|
} else {
|
||||||
cmd::list_variables(server, debug)
|
cmd::list_variables(config, get_ups_name(&server)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_ups_name<'a>(server: &'a UpsdName) -> anyhow::Result<&'a str> {
|
||||||
|
server
|
||||||
|
.upsname
|
||||||
|
.with_context(|| "ups name must be specified: <upsname>[@<hostname>[:<port>]]")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue