set up IP addr nicknames for eventual IP allow-listing

main
(on company time) 2022-12-16 16:05:29 -06:00
parent 86e5305630
commit 0fc99e7c26
3 changed files with 48 additions and 5 deletions

1
Cargo.lock generated
View File

@ -1367,6 +1367,7 @@ dependencies = [
"rustls", "rustls",
"rusty_ulid 1.0.0", "rusty_ulid 1.0.0",
"serde", "serde",
"serde_json",
"structopt", "structopt",
"tokio", "tokio",
"tracing", "tracing",

View File

@ -21,6 +21,7 @@ rmp-serde = "1.1.1"
rustls = "0.20.7" rustls = "0.20.7"
rusty_ulid = "1.0.0" rusty_ulid = "1.0.0"
serde = "1.0.151" serde = "1.0.151"
serde_json = "1.0.89"
structopt = "0.3.26" structopt = "0.3.26"
tokio = { version = "1.23.0", features = ["full"] } tokio = { version = "1.23.0", features = ["full"] }
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"

View File

@ -35,7 +35,11 @@ pub async fn main (opt: Opt) -> anyhow::Result <()>
tokio::fs::create_dir_all ("ptth_quic_output").await?; tokio::fs::create_dir_all ("ptth_quic_output").await?;
tokio::fs::write ("ptth_quic_output/quic_server.crt", &server_cert).await?; tokio::fs::write ("ptth_quic_output/quic_server.crt", &server_cert).await?;
let relay_state = Arc::new (RelayState::default ()); let relay_state = RelayState::default ();
if let Err (e) = relay_state.reload_config ().await {
error! ("{:?}", e);
}
let relay_state = Arc::new (relay_state);
let make_svc = { let make_svc = {
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
@ -55,6 +59,20 @@ pub async fn main (opt: Opt) -> anyhow::Result <()>
let http_addr = SocketAddr::from (([0, 0, 0, 0], 4004)); let http_addr = SocketAddr::from (([0, 0, 0, 0], 4004));
let http_server = Server::bind (&http_addr); let http_server = Server::bind (&http_addr);
let _task_reload_config = {
let relay_state = Arc::clone (&relay_state);
tokio::spawn (async move {
let mut interval = tokio::time::interval (std::time::Duration::from_secs (60));
interval.set_missed_tick_behavior (tokio::time::MissedTickBehavior::Skip);
loop {
interval.tick ().await;
relay_state.reload_config ().await.ok ();
}
})
};
let task_quic_server = { let task_quic_server = {
let relay_state = Arc::clone (&relay_state); let relay_state = Arc::clone (&relay_state);
tokio::spawn (async move { tokio::spawn (async move {
@ -172,12 +190,20 @@ struct RelayState {
#[derive (Default)] #[derive (Default)]
struct Config { struct Config {
ip_nicknames: HashMap <String, [u8; 4]>, ip_nicknames: BTreeMap <[u8; 4], String>,
}
impl From <ConfigFile> for Config {
fn from (x: ConfigFile) -> Self {
Self {
ip_nicknames: x.ip_nicknames.into_iter ().collect (),
}
}
} }
#[derive (Deserialize)] #[derive (Deserialize)]
struct ConfigFile { struct ConfigFile {
ip_nicknames: HashMap <String, String>, ip_nicknames: Vec <([u8; 4], String)>,
} }
struct DirecState { struct DirecState {
@ -223,7 +249,15 @@ struct P4State {
} }
impl RelayState { impl RelayState {
async fn reload_config (&self) -> anyhow::Result <()> {
let s = tokio::fs::read_to_string ("config/ptth_quic_relay_server.json").await?;
let config: ConfigFile = serde_json::from_str (&s)?;
let config = Arc::new (Config::from (config));
self.config.store (config);
Ok (())
}
} }
struct RequestP2ToP4 { struct RequestP2ToP4 {
@ -300,11 +334,18 @@ async fn handle_quic_connection (
{ {
let id = Ulid::generate (); let id = Ulid::generate ();
let config = relay_state.config.load ();
let remote_addr = conn.remote_address (); let remote_addr = conn.remote_address ();
let ip_nickname = match remote_addr { let ip_nickname = match remote_addr {
SocketAddr::V4 (x) => { SocketAddr::V4 (x) => {
let ip = x.ip ().octets (); let ip = x.ip ().octets ();
"Unknown" debug! ("{:?}", ip);
match config.ip_nicknames.get (&ip) {
Some (nick) => nick.as_str (),
_ => "Unknown",
}
}, },
_ => "Unknown, not IPv4", _ => "Unknown, not IPv4",
}; };