~shockham/gemo

b2e48e2210f6a23422dd668fec7ecdbbb7070541 — shockham 3 years ago 5ca1d4e main
Take in url as last cli arg
1 files changed, 21 insertions(+), 7 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +21 -7
@@ 2,19 2,25 @@ use native_tls::TlsConnector;
use std::io::{Read, Write};
use std::net::TcpStream;

fn client() -> Result<String, String> {
fn client(url: String) -> Result<String, String> {
    let mut builder = TlsConnector::builder();
    builder.danger_accept_invalid_hostnames(true);
    builder.danger_accept_invalid_certs(true);
    let connector = builder.build().expect("Connection Failed");

    let stream = TcpStream::connect("gemini.circumlunar.space:1965").expect("Connection Failed");
    let mut stream = connector
        .connect("gemini.circumlunar.space", stream)
        .expect("Connection Failed");
    let host = url
        .as_str()
        .strip_prefix("gemini://")
        .expect("Failed to parse url")
        .split('/')
        .next()
        .expect("Failed to parse url");

    let stream = TcpStream::connect(format!("{}:1965", host)).expect("Connection Failed");
    let mut stream = connector.connect(host, stream).expect("Connection Failed");

    stream
        .write_all(b"gemini://gemini.circumlunar.space/users/hundredrabbits/\r\n")
        .write_all(format!("{}\r\n", url).as_bytes())
        .expect("Connection Failed");
    let mut res = vec![];
    stream.read_to_end(&mut res).expect("Connection Failed");


@@ 23,7 29,15 @@ fn client() -> Result<String, String> {
}

fn main() -> Result<(), String> {
    let body = client()?;
    let url = match std::env::args().last() {
        Some(url) => url,
        None => {
            println!("Usage: gemo GEMINI_URL");
            return Ok(());
        }
    };

    let body = client(url)?;
    println!("{}", body);

    Ok(())