diff --git a/.gitignore b/.gitignore index 42fcfb8..8a86a75 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ build ./tailwindcss/node_modules/* +tailwindcss/node_modules +tailwindcss/node_modules/* diff --git a/src/main.rs b/src/main.rs index da00ece..427ea83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,6 @@ -// use engines::{ -// duckduckgo::duckduckgo::DuckDuckGo, -// engine_base::engine_base::{EngineBase, SearchResult}, -// }; -// -// pub mod client; -// pub mod engines; -// pub mod utils; -// -// #[tokio::main] -// async fn main() { -// let callback = Box::new(|result: SearchResult| { -// dbg!(&result); -// }); -// let mut ddg = DuckDuckGo::new(callback); -// ddg.search(&"test").await; -// -// println!("done"); -// } - -// Found no other way to make this work use crate::engines::engine_base::engine_base::EngineBase; -use std::io::{BufRead, BufReader, Read, Write}; -use std::net::TcpStream; use std::str; use std::sync::Arc; -use std::time::Instant; use engines::brave::brave::Brave; use futures::lock::Mutex; @@ -35,7 +11,6 @@ use rocket::response::{ content::{RawCss, RawHtml}, stream::TextStream, }; -use rustls::RootCertStore; use crate::static_files::static_files::read_file_contents; @@ -54,10 +29,7 @@ lazy_static! { static ref TAILWIND_CSS: String = read_file_contents("./tailwindcss/output.css").unwrap(); } -#[get("/")] -fn index() -> &'static str { - "Hello, world!" -} +const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3"; #[get("/search")] fn search_get() -> &'static str { @@ -76,194 +48,36 @@ fn get_tailwindcss() -> RawCss<&'static str> { RawCss(&TAILWIND_CSS) } -#[get("/slow")] -async fn slow() -> &'static str { - tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; - - "Slow" -} - -#[get("/slowresponse")] -async fn slowresponse() -> TextStream![String] { - TextStream! { - yield "First".to_owned(); - - tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; - - yield "second".to_owned(); - - tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; - - yield "third".to_owned(); - } -} - #[get("/searchquery?")] async fn hello<'a>(query: &str) -> RawHtml { let query_box = query.to_string(); - let now = Arc::new(Box::new(Instant::now())); let completed_ref = Arc::new(Mutex::new(false)); let completed_ref_writer = completed_ref.clone(); - let ddg_ref = Arc::new(Mutex::new(Brave::new())); - let ddg_ref_writer = ddg_ref.clone(); - - let now_ref = now.clone(); + let brave_ref = Arc::new(Mutex::new(Brave::new())); + let brave_ref_writer = brave_ref.clone(); tokio::spawn(async move { - // let root_store = RootCertStore { - // roots: webpki_roots::TLS_SERVER_ROOTS.into(), - // }; - // - // let mut config = rustls::ClientConfig::builder() - // .with_root_certificates(root_store) - // .with_no_client_auth(); - // - // // Allow using SSLKEYLOGFILE. - // config.key_log = Arc::new(rustls::KeyLogFile::new()); - // - // let now = Instant::now(); - // let server_name = "html.duckduckgo.com".try_into().unwrap(); - // let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); - // - // let mut sock = TcpStream::connect("html.duckduckgo.com:443".to_socket_addrs()).unwrap(); - // let mut tls = rustls::Stream::new(&mut conn, &mut sock); - // tls.write_all( - // concat!( - // "POST /html/ HTTP/1.1\r\n", - // "Host: html.duckduckgo.com\r\n", - // "Connection: close\r\n", - // "Accept-Encoding: identity\r\n", - // "Content-Length: 6\r\n", - // // form data - // "Content-Type: application/x-www-form-urlencoded\r\n", - // "\r\n", - // "q=test", - // ) - // .as_bytes(), - // ) - // .unwrap(); - - // dbg!("Connected to DuckDuckGo"); - // dbg!(now.elapsed()); - // - // // Iterate over the stream to read the response in real time - // - // loop { - // if conn.wants_read() { - // conn.read_tls(&mut sock).unwrap(); - // conn.process_new_packets().unwrap(); - // - // let mut plaintext = Vec::new(); - // conn.reader().read_to_end(&mut plaintext).unwrap(); - // } - // - // if conn.wants_write() { - // conn.write_tls(&mut sock).unwrap(); - // } - // sock.wa - // } - - // loop { - // dbg!(now.elapsed()); - // let mut buf = [0u8; 1024]; - // let n = tls.read(&mut buf).unwrap(); - // if n == 0 { - // break; - // } - // - // dbg!(now.elapsed()); - // - // let mut ddg = ddg_ref_writer.lock().await; - // if let Some(result) = ddg.parse_packet(buf.iter()) { - // ddg.add_result(result); - // } - // - // // Release - // drop(ddg); - // tokio::task::yield_now().await; - // } - - // dbg!("done with content"); - // dbg!(now.elapsed()); - // - // let mut ddg = ddg_ref_writer.lock().await; - // while let Some(result) = ddg.parse_next() { - // ddg.add_result(result); - // } - - let root_store = RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); - let mut config = rustls::ClientConfig::builder() - .with_root_certificates(root_store) - .with_no_client_auth(); - - // Allow using SSLKEYLOGFILE. - config.key_log = Arc::new(rustls::KeyLogFile::new()); - - let server_name = "search.brave.com".try_into().unwrap(); - let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); - let mut sock = TcpStream::connect("search.brave.com:443").unwrap(); - let mut tls = rustls::Stream::new(&mut conn, &mut sock); - let now_sock = Instant::now(); - tls.write_all( - format!( - "GET /search?q={}&show_local=0&source=unlocalise HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\nAccept-Encoding: identity\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.3\r\n\r\n", - query_box, - ) - .as_bytes(), - ) - .unwrap(); - - let nw = Instant::now(); - - let client = ClientBuilder::new().build().unwrap(); - let resposne = client - .get("https://www.bing.com/search?q=test") + let client = ClientBuilder::new().user_agent(USER_AGENT).build().unwrap(); + let response = client + .get(format!("https://search.brave.com/search?q={}", query_box)) .send() .await .unwrap(); - dbg!(nw.elapsed()); - - let mut stream = resposne.bytes_stream(); + let mut stream = response.bytes_stream(); while let Some(chunk) = stream.next().await { - let chunk = chunk.unwrap(); + let buffer = chunk.unwrap(); - println!("{}", "========"); - dbg!(nw.elapsed()); - println!("{}", String::from_utf8_lossy(&chunk)); + let mut brave = brave_ref_writer.lock().await; + if let Some(result) = brave.parse_packet(buffer.iter()) { + brave.add_result(result); + + drop(brave); + tokio::task::yield_now().await; + } } - dbg!(nw.elapsed()); - - // loop { - // let mut buf = [0; 16384]; - // let n = tls.conn.reader().read(&mut buf); - // - // // dbg!(&n); - // - // if n.is_ok() { - // let n = n.unwrap(); - // if n == 0 { - // break; - // } - // // println!("{}", String::from_utf8_lossy(&buf)); - // // let mut brave = ddg_ref_writer.lock().await; - // - // dbg!(nw.elapsed()); - // - // // if let Some(result) = brave.parse_packet(buf.iter()) { - // // // println!("Brave: {}", now_ref.elapsed().as_millis()); - // // brave.add_result(result); - // // - // // drop(brave); - // // tokio::task::yield_now().await; - // // } - // } else { - // tls.conn.complete_io(tls.sock); - // } - // } - let mut completed = completed_ref_writer.lock().await; *completed = true; }); @@ -274,7 +88,7 @@ async fn hello<'a>(query: &str) -> RawHtml { yield HTML_BEGINNING.to_string(); loop { - let ddg = ddg_ref.lock().await; + let ddg = brave_ref.lock().await; let len = ddg.results.len(); @@ -293,7 +107,6 @@ async fn hello<'a>(query: &str) -> RawHtml { for ii in (current_index + 1)..len { let result = ddg.results.get(ii).unwrap(); - println!("Yield: {}", now.elapsed().as_millis()); let text = format!("
  • {}

    {}

  • ", &result.title, &result.description); yield text.to_string(); @@ -313,10 +126,7 @@ async fn hello<'a>(query: &str) -> RawHtml { #[launch] async fn rocket() -> _ { rocket::build() - .mount("/", routes![index]) .mount("/", routes![hello]) .mount("/", routes![search_get]) .mount("/", routes![get_tailwindcss]) - .mount("/", routes![slow]) - .mount("/", routes![slowresponse]) }