this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: implement server

dusk a48b86a0

+118
+1
.gitignore
··· 1 + /target
+62
Cargo.lock
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "ascii" 7 + version = "1.1.0" 8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" 10 + 11 + [[package]] 12 + name = "chunked_transfer" 13 + version = "1.5.0" 14 + source = "registry+https://github.com/rust-lang/crates.io-index" 15 + checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" 16 + 17 + [[package]] 18 + name = "clickee-proxy" 19 + version = "0.1.0" 20 + dependencies = [ 21 + "tiny_http", 22 + "tinyget", 23 + ] 24 + 25 + [[package]] 26 + name = "httpdate" 27 + version = "1.0.3" 28 + source = "registry+https://github.com/rust-lang/crates.io-index" 29 + checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" 30 + 31 + [[package]] 32 + name = "log" 33 + version = "0.4.28" 34 + source = "registry+https://github.com/rust-lang/crates.io-index" 35 + checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" 36 + 37 + [[package]] 38 + name = "tiny_http" 39 + version = "0.12.0" 40 + source = "registry+https://github.com/rust-lang/crates.io-index" 41 + checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" 42 + dependencies = [ 43 + "ascii", 44 + "chunked_transfer", 45 + "httpdate", 46 + "log", 47 + ] 48 + 49 + [[package]] 50 + name = "tinyget" 51 + version = "1.1.2" 52 + source = "registry+https://github.com/rust-lang/crates.io-index" 53 + checksum = "5205be93938f3ae916c773ffa3361eaf3b0ee4d40f3752da46d8b1d5311e7285" 54 + dependencies = [ 55 + "urlencoding", 56 + ] 57 + 58 + [[package]] 59 + name = "urlencoding" 60 + version = "2.1.3" 61 + source = "registry+https://github.com/rust-lang/crates.io-index" 62 + checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
+8
Cargo.toml
··· 1 + [package] 2 + name = "clickee-proxy" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies] 7 + tiny_http = "0.12.0" 8 + tinyget = "1.1.2"
+47
src/main.rs
··· 1 + use tiny_http::{Response, Server}; 2 + 3 + fn main() { 4 + let clickee_urls_raw = std::env::var("CLICKEE_URLS").expect("CLICKEE_URLS must be set"); 5 + let clickee_urls = clickee_urls_raw.split(",").collect::<Vec<_>>(); 6 + let port = std::env::var("PORT") 7 + .unwrap_or_else(|_| "8000".to_string()) 8 + .parse::<u16>() 9 + .expect("PORT must be a number"); 10 + let server = Server::http(("0.0.0.0", port)).unwrap(); 11 + 12 + for request in server.incoming_requests() { 13 + let get_header = |name| { 14 + request 15 + .headers() 16 + .iter() 17 + .find(|h| h.field.equiv(name)) 18 + .map(|h| h.value.to_string()) 19 + }; 20 + let ip = get_header("x-real-ip") 21 + .or_else(|| request.remote_addr().map(|a| a.to_string())) 22 + .unwrap_or_else(|| "unknown".to_string()); 23 + let ua = get_header("user-agent").unwrap_or_else(|| "unknown".to_string()); 24 + 25 + let mut sent_click = false; 26 + for url in clickee_urls.iter() { 27 + match tinyget::get(*url).send_lazy() { 28 + Ok(_) => { 29 + sent_click = true; 30 + break; 31 + } 32 + Err(err) => println!("clickee ({url}) not online (error: {err}), using next one"), 33 + } 34 + } 35 + 36 + if sent_click { 37 + println!("clicker clicked: {ua} from {ip}"); 38 + } else { 39 + println!("no clickee available"); 40 + } 41 + 42 + let resp = Response::empty(sent_click.then_some(200).unwrap_or(502)); 43 + if let Err(err) = request.respond(resp) { 44 + println!("error responding to clicker: {err}"); 45 + } 46 + } 47 + }