From 7538041b670c4d414ed8d6d10e822b85aaeef40b Mon Sep 17 00:00:00 2001 From: asonix Date: Thu, 19 Mar 2020 23:06:16 -0500 Subject: [PATCH] Add basic info page --- src/config.rs | 4 ++++ src/main.rs | 26 +++++++++++++++++++++++-- src/state.rs | 53 +++++++++++++++++---------------------------------- 3 files changed, 46 insertions(+), 37 deletions(-) diff --git a/src/config.rs b/src/config.rs index 04a4b99..69e2a15 100644 --- a/src/config.rs +++ b/src/config.rs @@ -96,6 +96,10 @@ impl Config { "v0.1.0-master".to_owned() } + pub fn source_code(&self) -> String { + "https://git.asonix.dog/asonix/ap-relay".to_owned() + } + pub fn generate_url(&self, kind: UrlKind) -> String { let scheme = if self.https { "https" } else { "http" }; diff --git a/src/main.rs b/src/main.rs index 58f308b..ad1ac1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,8 +18,30 @@ mod webfinger; use self::{args::Args, config::Config, db::Db, state::State, webfinger::RelayResolver}; -async fn index() -> impl Responder { - "hewwo, mr obama" +async fn index(state: web::Data, config: web::Data) -> impl Responder { + let mut s = String::new(); + s.push_str(&format!("Welcome to the relay on {}\n", config.hostname())); + + let listeners = state.listeners().await; + if listeners.is_empty() { + s.push_str("There are no currently connected servers\n"); + } else { + s.push_str("Here are the currently connected servers:\n"); + s.push_str("\n"); + } + + for listener in listeners { + if let Some(domain) = listener.as_url().domain() { + s.push_str(&format!("{}\n", domain)); + } + } + s.push_str("\n"); + s.push_str(&format!( + "The source code for this project can be found at {}\n", + config.source_code() + )); + + s } #[actix_rt::main] diff --git a/src/state.rs b/src/state.rs index ad83e2c..7f985e0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -45,36 +45,33 @@ impl State { } pub async fn bust_whitelist(&self, whitelist: &str) { - let hs = self.whitelists.clone(); - - let mut write_guard = hs.write().await; + let mut write_guard = self.whitelists.write().await; write_guard.remove(whitelist); } pub async fn bust_block(&self, block: &str) { - let hs = self.blocks.clone(); - - let mut write_guard = hs.write().await; + let mut write_guard = self.blocks.write().await; write_guard.remove(block); } pub async fn bust_listener(&self, inbox: &XsdAnyUri) { - let hs = self.listeners.clone(); - - let mut write_guard = hs.write().await; + let mut write_guard = self.listeners.write().await; write_guard.remove(inbox); } - pub async fn listeners_without(&self, inbox: &XsdAnyUri, domain: &str) -> Vec { - let hs = self.listeners.clone(); + pub async fn listeners(&self) -> Vec { + let read_guard = self.listeners.read().await; + read_guard.iter().cloned().collect() + } - let read_guard = hs.read().await; + pub async fn listeners_without(&self, inbox: &XsdAnyUri, domain: &str) -> Vec { + let read_guard = self.listeners.read().await; read_guard .iter() .filter_map(|listener| { - if let Some(host) = listener.as_url().host() { - if listener != inbox && host.to_string() != domain { + if let Some(dom) = listener.as_url().domain() { + if listener != inbox && dom != domain { return Some(listener.clone()); } } @@ -89,10 +86,8 @@ impl State { return true; } - let hs = self.whitelists.clone(); - if let Some(host) = actor_id.as_url().host() { - let read_guard = hs.read().await; + let read_guard = self.whitelists.read().await; return read_guard.contains(&host.to_string()); } @@ -100,10 +95,8 @@ impl State { } pub async fn is_blocked(&self, actor_id: &XsdAnyUri) -> bool { - let hs = self.blocks.clone(); - if let Some(host) = actor_id.as_url().host() { - let read_guard = hs.read().await; + let read_guard = self.blocks.read().await; return read_guard.contains(&host.to_string()); } @@ -111,9 +104,7 @@ impl State { } pub async fn is_listener(&self, actor_id: &XsdAnyUri) -> bool { - let hs = self.listeners.clone(); - - let read_guard = hs.read().await; + let read_guard = self.listeners.read().await; read_guard.contains(actor_id) } @@ -125,30 +116,22 @@ impl State { } pub async fn cache(&self, object_id: XsdAnyUri, actor_id: XsdAnyUri) { - let cache = self.actor_id_cache.clone(); - - let mut write_guard = cache.write().await; + let mut write_guard = self.actor_id_cache.write().await; write_guard.put(object_id, actor_id); } pub async fn cache_block(&self, host: String) { - let blocks = self.blocks.clone(); - - let mut write_guard = blocks.write().await; + let mut write_guard = self.blocks.write().await; write_guard.insert(host); } pub async fn cache_whitelist(&self, host: String) { - let whitelists = self.whitelists.clone(); - - let mut write_guard = whitelists.write().await; + let mut write_guard = self.whitelists.write().await; write_guard.insert(host); } pub async fn cache_listener(&self, listener: XsdAnyUri) { - let listeners = self.listeners.clone(); - - let mut write_guard = listeners.write().await; + let mut write_guard = self.listeners.write().await; write_guard.insert(listener); }