From aae811b2c7edd3f8d2e52652e0cedab9098bb608 Mon Sep 17 00:00:00 2001 From: dumbasPL <29180158+dumbasPL@users.noreply.github.com> Date: Fri, 24 Nov 2023 00:06:25 +0100 Subject: [PATCH] convert to async --- Cargo.lock | 23 +++++++++++++---------- Cargo.toml | 5 +++-- examples/get-test-data.rs | 13 +++++++------ examples/test.rs | 13 +++++++------ src/main.rs | 25 ++++++++++++++++--------- src/portal.rs | 26 ++++++++++++++++---------- 6 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e980e0..fc759d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,7 @@ dependencies = [ "image", "reqwest", "tl", + "tokio", ] [[package]] @@ -439,12 +440,6 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" -[[package]] -name = "futures-io" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" - [[package]] name = "futures-sink" version = "0.3.28" @@ -464,12 +459,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-core", - "futures-io", "futures-task", - "memchr", "pin-project-lite", "pin-utils", - "slab", ] [[package]] @@ -1219,12 +1211,23 @@ dependencies = [ "bytes", "libc", "mio", - "num_cpus", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys", ] +[[package]] +name = "tokio-macros" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.22", +] + [[package]] name = "tokio-util" version = "0.7.8" diff --git a/Cargo.toml b/Cargo.toml index 26e6f25..448098a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ edition = "2021" [dependencies] darknet = "0.4" anyhow = "1.0" -reqwest = { version = "0.11", default-features = false, features = ["blocking"] } +reqwest = { version = "0.11", default-features = false } tl = "0.7" image = "0.24" -clap = { version = "4.4.7", features = ["derive", "string", "env"] } \ No newline at end of file +clap = { version = "4.4.7", features = ["derive", "string", "env"] } +tokio = { version = "1", features = ["macros"]} \ No newline at end of file diff --git a/examples/get-test-data.rs b/examples/get-test-data.rs index f39c487..aca7743 100644 --- a/examples/get-test-data.rs +++ b/examples/get-test-data.rs @@ -10,16 +10,16 @@ use aero2solver::{ }; use anyhow::{anyhow, Result}; -fn run(solver: &mut Aero2Solver) -> Result<()> { +async fn run(solver: &mut Aero2Solver) -> Result<()> { let client = PortalClient::new(BASE_URL, USER_AGENT)?; - let state = client.get_state()?; + let state = client.get_state().await?; let mut tries = 0; let (solution, captcha) = loop { tries += 1; println!("Trying to solve captcha (try {})", tries); - let captcha = client.get_captcha(&state.session_id)?; + let captcha = client.get_captcha(&state.session_id).await?; match solver.solve(&captcha, 0.9, 8) { Ok(solution) => { println!("Captcha solved as {} after {}", solution, tries); @@ -29,7 +29,7 @@ fn run(solver: &mut Aero2Solver) -> Result<()> { } }; - let state = client.submit_captcha(&state.session_id, &solution)?; + let state = client.submit_captcha(&state.session_id, &solution).await?; match state.message { Some(ref message) if message.eq("Rozłącz i ponownie połącz się z Internetem.") => { println!("Captcha solved, code: {}", solution); @@ -46,7 +46,8 @@ fn run(solver: &mut Aero2Solver) -> Result<()> { Ok(()) } -fn main() -> Result<()> { +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { let mut solver = Aero2Solver::new( "./model/captcha.names", "./model/captcha.cfg", @@ -62,7 +63,7 @@ fn main() -> Result<()> { break; } - match run(&mut solver) { + match run(&mut solver).await { Ok(_) => success_count += 1, Err(e) => println!("Error: {}", e), } diff --git a/examples/test.rs b/examples/test.rs index e1b2dec..6de7908 100644 --- a/examples/test.rs +++ b/examples/test.rs @@ -5,16 +5,16 @@ use aero2solver::{ }; use anyhow::Result; -fn run(solver: &mut Aero2Solver) -> Result<()> { +async fn run(solver: &mut Aero2Solver) -> Result<()> { let client = PortalClient::new(BASE_URL, USER_AGENT)?; - let state = client.get_state()?; + let state = client.get_state().await?; let mut tries = 0; let solution: String = loop { tries += 1; println!("Trying to solve captcha (try {})", tries); - let captcha = client.get_captcha(&state.session_id)?; + let captcha = client.get_captcha(&state.session_id).await?; match solver.solve(&captcha, 0.8, 8) { Ok(solution) => { println!("Captcha solved as {} after {}", solution, tries); @@ -24,7 +24,7 @@ fn run(solver: &mut Aero2Solver) -> Result<()> { } }; - let state = client.submit_captcha(&state.session_id, &solution)?; + let state = client.submit_captcha(&state.session_id, &solution).await?; println!( "Captcha solved with message: {}", state.message.unwrap_or_default() @@ -33,7 +33,8 @@ fn run(solver: &mut Aero2Solver) -> Result<()> { Ok(()) } -fn main() -> Result<()> { +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { let mut solver = Aero2Solver::new( "./model/captcha.names", "./model/captcha.cfg", @@ -41,6 +42,6 @@ fn main() -> Result<()> { )?; loop { - run(&mut solver)?; + run(&mut solver).await?; } } diff --git a/src/main.rs b/src/main.rs index 914ecc6..43688d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,8 @@ struct Args { solved_delay: f32, } -fn main() -> Result<()> { +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { let Args { model_cfg, weights, @@ -68,10 +69,12 @@ fn main() -> Result<()> { let solved_sleep_time = Duration::from_secs_f32(solved_delay); loop { - let was_solved = run(&mut solver, threshold, error_sleep_time).unwrap_or_else(|x| { - println!("Error: {}", x); - false - }); + let was_solved = run(&mut solver, threshold, error_sleep_time) + .await + .unwrap_or_else(|x| { + println!("Error: {}", x); + false + }); let sleep_time = match was_solved { true => { @@ -85,12 +88,16 @@ fn main() -> Result<()> { } } -fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration) -> Result { +async fn run( + solver: &mut Aero2Solver, + min_threshold: f32, + fail_sleep_time: Duration, +) -> Result { let client = PortalClient::new(BASE_URL, USER_AGENT)?; let mut was_required = false; - let mut state = client.get_state()?; + let mut state = client.get_state().await?; loop { if !state.captcha_present { let status = match was_required { @@ -125,7 +132,7 @@ fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration) break Err(anyhow!("Too many tries")); } println!("Trying to solve captcha (try {})", tries); - let captcha = client.get_captcha(&state.session_id)?; + let captcha = client.get_captcha(&state.session_id).await?; match solver.solve(&captcha, min_threshold, 8) { Ok(solution) => { println!("Captcha solved as {} after {}", solution, tries); @@ -135,6 +142,6 @@ fn run(solver: &mut Aero2Solver, min_threshold: f32, fail_sleep_time: Duration) } }?; - state = client.submit_captcha(&state.session_id, &solution)?; + state = client.submit_captcha(&state.session_id, &solution).await?; } } diff --git a/src/portal.rs b/src/portal.rs index ed905d7..77e466d 100644 --- a/src/portal.rs +++ b/src/portal.rs @@ -2,7 +2,7 @@ use std::time::Duration; use ::reqwest::redirect::Policy; use anyhow::{anyhow, Ok, Result}; -use reqwest::blocking as reqwest; +use reqwest; pub struct CaptchaState { pub captcha_present: bool, @@ -62,31 +62,35 @@ impl PortalClient { }) } - pub fn get_state(&self) -> Result { + pub async fn get_state(&self) -> Result { let page = self .client .post(self.base_url) .form(&[("viewForm", "true")]) - .send()? - .text()?; + .send() + .await? + .text() + .await?; self.parse_page(&page) } - pub fn get_captcha(&self, session_id: &str) -> Result> { + pub async fn get_captcha(&self, session_id: &str) -> Result> { let bytes = self .client .get(format!( "{}getCaptcha.html?PHPSESSID={}", self.base_url, session_id )) - .send()? - .bytes()?; + .send() + .await? + .bytes() + .await?; Ok(bytes.to_vec()) } - pub fn submit_captcha(&self, session_id: &str, solution: &str) -> Result { + pub async fn submit_captcha(&self, session_id: &str, solution: &str) -> Result { let page = self .client .post(self.base_url) @@ -95,8 +99,10 @@ impl PortalClient { ("viewForm", "true"), ("captcha", solution), ]) - .send()? - .text()?; + .send() + .await? + .text() + .await?; self.parse_page(&page) }