fix: clean up

This commit is contained in:
Myzel394 2024-02-19 20:58:58 +01:00
parent 6c0751740c
commit 2ef3eea524
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
2 changed files with 18 additions and 206 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
build build
./tailwindcss/node_modules/* ./tailwindcss/node_modules/*
tailwindcss/node_modules
tailwindcss/node_modules/*

View File

@ -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 crate::engines::engine_base::engine_base::EngineBase;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::TcpStream;
use std::str; use std::str;
use std::sync::Arc; use std::sync::Arc;
use std::time::Instant;
use engines::brave::brave::Brave; use engines::brave::brave::Brave;
use futures::lock::Mutex; use futures::lock::Mutex;
@ -35,7 +11,6 @@ use rocket::response::{
content::{RawCss, RawHtml}, content::{RawCss, RawHtml},
stream::TextStream, stream::TextStream,
}; };
use rustls::RootCertStore;
use crate::static_files::static_files::read_file_contents; 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(); static ref TAILWIND_CSS: String = read_file_contents("./tailwindcss/output.css").unwrap();
} }
#[get("/")] 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";
fn index() -> &'static str {
"Hello, world!"
}
#[get("/search")] #[get("/search")]
fn search_get() -> &'static str { fn search_get() -> &'static str {
@ -76,193 +48,35 @@ fn get_tailwindcss() -> RawCss<&'static str> {
RawCss(&TAILWIND_CSS) 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?<query>")] #[get("/searchquery?<query>")]
async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> { async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
let query_box = query.to_string(); 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 = Arc::new(Mutex::new(false));
let completed_ref_writer = completed_ref.clone(); let completed_ref_writer = completed_ref.clone();
let ddg_ref = Arc::new(Mutex::new(Brave::new())); let brave_ref = Arc::new(Mutex::new(Brave::new()));
let ddg_ref_writer = ddg_ref.clone(); let brave_ref_writer = brave_ref.clone();
let now_ref = now.clone();
tokio::spawn(async move { tokio::spawn(async move {
// let root_store = RootCertStore { let client = ClientBuilder::new().user_agent(USER_AGENT).build().unwrap();
// roots: webpki_roots::TLS_SERVER_ROOTS.into(), let response = client
// }; .get(format!("https://search.brave.com/search?q={}", query_box))
//
// 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")
.send() .send()
.await .await
.unwrap(); .unwrap();
dbg!(nw.elapsed()); let mut stream = response.bytes_stream();
let mut stream = resposne.bytes_stream();
while let Some(chunk) = stream.next().await { while let Some(chunk) = stream.next().await {
let chunk = chunk.unwrap(); let buffer = chunk.unwrap();
println!("{}", "========"); let mut brave = brave_ref_writer.lock().await;
dbg!(nw.elapsed()); if let Some(result) = brave.parse_packet(buffer.iter()) {
println!("{}", String::from_utf8_lossy(&chunk)); 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; let mut completed = completed_ref_writer.lock().await;
*completed = true; *completed = true;
@ -274,7 +88,7 @@ async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
yield HTML_BEGINNING.to_string(); yield HTML_BEGINNING.to_string();
loop { loop {
let ddg = ddg_ref.lock().await; let ddg = brave_ref.lock().await;
let len = ddg.results.len(); let len = ddg.results.len();
@ -293,7 +107,6 @@ async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
for ii in (current_index + 1)..len { for ii in (current_index + 1)..len {
let result = ddg.results.get(ii).unwrap(); let result = ddg.results.get(ii).unwrap();
println!("Yield: {}", now.elapsed().as_millis());
let text = format!("<li><h1>{}</h1><p>{}</p></li>", &result.title, &result.description); let text = format!("<li><h1>{}</h1><p>{}</p></li>", &result.title, &result.description);
yield text.to_string(); yield text.to_string();
@ -313,10 +126,7 @@ async fn hello<'a>(query: &str) -> RawHtml<TextStream![String]> {
#[launch] #[launch]
async fn rocket() -> _ { async fn rocket() -> _ {
rocket::build() rocket::build()
.mount("/", routes![index])
.mount("/", routes![hello]) .mount("/", routes![hello])
.mount("/", routes![search_get]) .mount("/", routes![search_get])
.mount("/", routes![get_tailwindcss]) .mount("/", routes![get_tailwindcss])
.mount("/", routes![slow])
.mount("/", routes![slowresponse])
} }