Add client TLS auth on server side

This commit is contained in:
2022-08-31 12:24:54 +02:00
parent 1b95b10553
commit 27b52dfcb7
9 changed files with 144 additions and 34 deletions

View File

@ -8,7 +8,7 @@ static mut ROOT_CERT: Option<Vec<u8>> = None;
pub struct ClientConfig {
/// Access token
#[clap(short, long)]
pub token: String,
pub token: Option<String>,
/// Relay server
#[clap(short, long, default_value = "http://127.0.0.1:8000")]
@ -24,6 +24,11 @@ pub struct ClientConfig {
}
impl ClientConfig {
/// 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")
}
/// Load root certificate
pub fn get_root_certificate(&self) -> Option<Vec<u8>> {
self.root_certificate.as_ref()?;

View File

@ -23,7 +23,7 @@ async fn get_server_config(config: &ClientConfig) -> Result<RemoteConfig, Box<dy
let client = client.build().expect("Failed to build reqwest client");
let req = client.get(url)
.header("Authorization", format!("Bearer {}", config.token))
.header("Authorization", format!("Bearer {}", config.get_auth_token()))
.send()
.await?;
if req.status().as_u16() != 200 {
@ -51,7 +51,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let h = tokio::spawn(relay_client(
format!("{}/ws?id={}&token={}",
args.relay_url, port.id, urlencoding::encode(&args.token))
args.relay_url, port.id, urlencoding::encode(args.get_auth_token()))
.replace("http", "ws"),
listen_address,
args.clone(),