Merged all workspace projects into a single binary project
This commit is contained in:
100
src/tcp_relay_client/client_config.rs
Normal file
100
src/tcp_relay_client/client_config.rs
Normal file
@@ -0,0 +1,100 @@
|
||||
use bytes::BufMut;
|
||||
use clap::Parser;
|
||||
|
||||
/// TCP relay client
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
pub struct ClientConfig {
|
||||
/// Access token
|
||||
#[clap(short, long)]
|
||||
pub token: Option<String>,
|
||||
|
||||
/// Relay server
|
||||
#[clap(short, long, default_value = "http://127.0.0.1:8000")]
|
||||
pub relay_url: String,
|
||||
|
||||
/// Listen address
|
||||
#[clap(short, long, default_value = "127.0.0.1")]
|
||||
pub listen_address: String,
|
||||
|
||||
/// Alternative root certificate to use for server authentication
|
||||
#[clap(short = 'c', long)]
|
||||
pub root_certificate: Option<String>,
|
||||
|
||||
#[clap(skip)]
|
||||
_root_certificate_cache: Option<Vec<u8>>,
|
||||
|
||||
/// TLS certificate for TLS authentication.
|
||||
#[clap(long)]
|
||||
pub tls_cert: Option<String>,
|
||||
|
||||
#[clap(skip)]
|
||||
_tls_cert_cache: Option<Vec<u8>>,
|
||||
|
||||
/// TLS key for TLS authentication.
|
||||
#[clap(long)]
|
||||
pub tls_key: Option<String>,
|
||||
|
||||
#[clap(skip)]
|
||||
_tls_key_cache: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl ClientConfig {
|
||||
/// Load certificates and put them in cache
|
||||
pub fn load_certificates(&mut self) {
|
||||
self._root_certificate_cache = self
|
||||
.root_certificate
|
||||
.as_ref()
|
||||
.map(|c| std::fs::read(c).expect("Failed to read root certificate!"));
|
||||
|
||||
self._tls_cert_cache = self
|
||||
.tls_cert
|
||||
.as_ref()
|
||||
.map(|c| std::fs::read(c).expect("Failed to read client certificate!"));
|
||||
|
||||
self._tls_key_cache = self
|
||||
.tls_key
|
||||
.as_ref()
|
||||
.map(|c| std::fs::read(c).expect("Failed to read client key!"));
|
||||
}
|
||||
|
||||
/// Get client token, returning a dummy token if none was specified
|
||||
pub fn get_auth_token(&self) -> &str {
|
||||
self.token.as_deref().unwrap_or("none")
|
||||
}
|
||||
|
||||
/// Get root certificate content
|
||||
pub fn get_root_certificate(&self) -> Option<Vec<u8>> {
|
||||
self._root_certificate_cache.clone()
|
||||
}
|
||||
|
||||
/// Get client certificate & key pair, if available
|
||||
pub fn get_client_keypair(&self) -> Option<(&Vec<u8>, &Vec<u8>)> {
|
||||
if let (Some(cert), Some(key)) = (&self._tls_cert_cache, &self._tls_key_cache) {
|
||||
Some((cert, key))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Get client certificate & key pair, in a single memory buffer
|
||||
pub fn get_merged_client_keypair(&self) -> Option<Vec<u8>> {
|
||||
self.get_client_keypair().map(|(c, k)| {
|
||||
let mut out = k.to_vec();
|
||||
out.put_slice("\n".as_bytes());
|
||||
out.put_slice(c);
|
||||
out
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::client_config::ClientConfig;
|
||||
|
||||
#[test]
|
||||
fn verify_cli() {
|
||||
use clap::CommandFactory;
|
||||
ClientConfig::command().debug_assert()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user