diff options
| author | Fuwn <[email protected]> | 2024-06-24 21:38:39 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-24 21:38:39 -0700 |
| commit | d0d03cb07101291b4a49c322318b97c1c10521f5 (patch) | |
| tree | ca624f965fd066295110bd61b1df037f188e6e21 /src/modules | |
| parent | chore(modules): add back removed comments (diff) | |
| download | locus-d0d03cb07101291b4a49c322318b97c1c10521f5.tar.xz locus-d0d03cb07101291b4a49c322318b97c1c10521f5.zip | |
feat(modules): finger gateway
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/finger.rs | 69 | ||||
| -rw-r--r-- | src/modules/web.rs | 6 |
2 files changed, 72 insertions, 3 deletions
diff --git a/src/modules/finger.rs b/src/modules/finger.rs new file mode 100644 index 0000000..c61cfbe --- /dev/null +++ b/src/modules/finger.rs @@ -0,0 +1,69 @@ +use { + crate::{response::success, route::track_mount}, + std::io::{Read, Write}, + windmark::response::Response, +}; + +pub fn module(router: &mut windmark::router::Router) { + track_mount(router, "/finger", "Finger-to-Gemini Gateway", |context| { + success( + &format!( + r"# Finger-to-Gemini Gateway + +To use this gateway, simply append the address of your target resource to the end of the current route, minus the protocol. Routed paths are supported! + +To visit my personal Finger server, <finger://fuwn.me>, you would visit <gemini://fuwn.me/finger/fuwn.me>. + +=> /finger/fuwn.me Try it!" + ), + &context, + ) + }); + track_mount( + router, + "/finger/*uri", + "-Finger-to-Gemini Gateway Router", + |context| { + if let Some(uri) = context.parameters.get("uri") { + let path; + let url = url::Url::parse({ + let mut parts = uri.split("/"); + let host = parts.next().unwrap(); + + path = parts.collect::<Vec<&str>>().join("/"); + + &if !host.contains(":") { + format!("{}:79", host) + } else { + host.to_string() + } + }) + .unwrap(); + + let mut stream = std::net::TcpStream::connect(url.to_string()).unwrap(); + let mut buffer = [0; 1024]; + + stream.write_all(format!("{path}\n").as_bytes()).unwrap(); + + let mut response = String::new(); + + loop { + let bytes_read = stream.read(&mut buffer).unwrap(); + + if bytes_read == 0 { + break; + } + + #[allow(unsafe_code)] + response.push_str(unsafe { + std::str::from_utf8_unchecked(&buffer[..bytes_read]) + }); + } + + Response::success(response) + } else { + Response::bad_request("Invalid URI") + } + }, + ); +} diff --git a/src/modules/web.rs b/src/modules/web.rs index 1be6f48..31b0eec 100644 --- a/src/modules/web.rs +++ b/src/modules/web.rs @@ -10,14 +10,14 @@ use { fn error(message: &str, context: &windmark::context::RouteContext) -> Response { success( &format!( - "# World Wide Web to Gemini Gateway\n\n{message}\n\n=> /web Go back" + "# World Wide Web-to-Gemini Gateway\n\n{message}\n\n=> /web Go back" ), context, ) } pub fn module(router: &mut windmark::router::Router) { - track_mount(router, "/web", "World Wide Web to Gemini Gateway", |context| { + track_mount(router, "/web", "-World-Wide-Web to Gemini Gateway", |context| { success( &format!( r"# World Wide Web to Gemini Gateway @@ -34,7 +34,7 @@ To visit the web version of this exact page, <{ROOT_HTTPS_URL}/web>, you would v track_mount( router, "/web/*url", - "World Wide Web to Gemini Gateway Visitor", + "-World-Wide-Web to Gemini Gateway Router", async move |context: RouteContext| { let queries = windmark::utilities::queries_from_url(&context.url); |