Get server configuration from client

This commit is contained in:
2022-08-30 10:38:26 +02:00
parent 31752bad80
commit 5d4db12bf6
4 changed files with 495 additions and 3 deletions

View File

@ -1,6 +1,12 @@
[package]
name = "tcp_realy_client"
name = "tcp_relay_client"
version = "0.1.0"
edition = "2021"
[dependencies]
base = { path = "../base" }
clap = { version = "3.2.18", features = ["derive", "env"] }
log = "0.4.17"
env_logger = "0.9.0"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }

View File

@ -0,0 +1,47 @@
use std::sync::Arc;
use clap::Parser;
use base::RemoteConfig;
/// TCP relay client
#[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
/// Access token
#[clap(short, long)]
pub token: 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,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
let args: Args = Args::parse();
let args = Arc::new(args);
// Get server relay configuration (fetch the list of port to forward)
let url = format!("{}/config", args.relay_url);
log::info!("Retrieving configuration on {}", url);
let req = reqwest::Client::new().get(url)
.header("Authorization", format!("Bearer {}", args.token))
.send()
.await?;
if req.status().as_u16() != 200 {
log::error!("Could not retrieve configuration! (got status {})", req.status());
std::process::exit(2);
}
let conf = req.json::<RemoteConfig>()
.await?;
println!("{:#?}", conf);
Ok(())
}