commit f156801f2153258f518959e55c9ab5f490d4d780
parent 0c92c7416e7666969f94b825c494274ca786b7e3
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 29 Mar 2023 19:17:28 -0700
cleanup errors
Diffstat:
| M | src/main.rs | | | 113 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 76 insertions(+), 37 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -31,6 +31,7 @@ use select::predicate::{And, Attr, Name};
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error;
+use std::fmt::{Display, Formatter};
use std::fs;
use std::io::Cursor;
use std::io::{self, Write};
@@ -39,6 +40,7 @@ use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use std::time::Instant;
+use reqwest::Error;
use termage;
use textwrap;
use tui::layout::Rect;
@@ -214,6 +216,46 @@ impl LeChatPHPConfig {
}
}
+#[derive(Debug)]
+enum LoginErr {
+ ServerDownErr,
+ ServerDown500Err,
+ CaptchaFailedSolveErr,
+ CaptchaUsedErr,
+ CaptchaWgErr,
+ RegErr,
+ NicknameErr,
+ KickedErr,
+ UnknownErr,
+ Reqwest(reqwest::Error),
+}
+
+impl From<reqwest::Error> for LoginErr {
+ fn from(value: Error) -> Self {
+ LoginErr::Reqwest(value)
+ }
+}
+
+impl Display for LoginErr {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ let s = match self {
+ LoginErr::ServerDownErr => SERVER_DOWN_ERR.to_owned(),
+ LoginErr::ServerDown500Err => SERVER_DOWN_500_ERR.to_owned(),
+ LoginErr::CaptchaFailedSolveErr => CAPTCHA_FAILED_SOLVE_ERR.to_owned(),
+ LoginErr::CaptchaUsedErr => CAPTCHA_USED_ERR.to_owned(),
+ LoginErr::CaptchaWgErr => CAPTCHA_WG_ERR.to_owned(),
+ LoginErr::RegErr => REG_ERR.to_owned(),
+ LoginErr::NicknameErr => NICKNAME_ERR.to_owned(),
+ LoginErr::KickedErr => KICKED_ERR.to_owned(),
+ LoginErr::UnknownErr => UNKNOWN_ERR.to_owned(),
+ LoginErr::Reqwest(e) => e.to_string(),
+ };
+ write!(f, "{}", s)
+ }
+}
+
+impl error::Error for LoginErr {}
+
struct BaseClient {
username: String,
password: String,
@@ -246,31 +288,29 @@ impl LeChatPHPClient {
let mut attempt = 0;
loop {
if let Err(e) = self.login() {
- if e.to_string() == KICKED_ERR
- || e.to_string() == REG_ERR
- || e.to_string() == NICKNAME_ERR
- || e.to_string() == UNKNOWN_ERR
- {
- eprintln!("{:?}", e.to_string());
- break;
- } else if e.to_string() == CAPTCHA_FAILED_SOLVE_ERR {
- // If we fail to solve the captcha, retry right away.
- eprintln!("{}", e.to_string());
- continue;
- } else if e.to_string() == CAPTCHA_WG_ERR || e.to_string() == CAPTCHA_USED_ERR {
- } else if e.to_string() == SERVER_DOWN_ERR || e.to_string() == SERVER_DOWN_500_ERR {
- eprintln!("{}", e.to_string());
- } else if let Some(err) = e.downcast_ref::<reqwest::Error>() {
- if err.is_connect() {
- eprintln!("{:?}\nIs tor proxy enabled ?", err.to_string());
+ match e {
+ LoginErr::KickedErr | LoginErr::RegErr | LoginErr::NicknameErr | LoginErr::UnknownErr => {
+ eprintln!("{}", e);
break;
- } else if err.is_timeout() {
- eprintln!("timeout: {:?}", err.to_string());
- } else {
- eprintln!("{:?}", err.to_string());
- }
- } else {
- eprintln!("unknown error: {:?}", e.to_string());
+ },
+ LoginErr::CaptchaFailedSolveErr => {
+ eprintln!("{}", e);
+ continue;
+ },
+ LoginErr::CaptchaWgErr | LoginErr::CaptchaUsedErr => {},
+ LoginErr::ServerDownErr | LoginErr::ServerDown500Err => {
+ eprintln!("{}", e);
+ },
+ LoginErr::Reqwest(err) => {
+ if err.is_connect() {
+ eprintln!("{}\nIs tor proxy enabled ?", err);
+ break;
+ } else if err.is_timeout() {
+ eprintln!("timeout: {}", err);
+ } else {
+ eprintln!("{}", err);
+ }
+ },
}
} else {
attempt = 0;
@@ -782,7 +822,7 @@ impl LeChatPHPClient {
Ok(())
}
- fn login(&mut self) -> Result<()> {
+ fn login(&mut self) -> std::result::Result<(), LoginErr> {
// If we provided a session, skip login process
if self.session != "" {
return Ok(());
@@ -792,7 +832,7 @@ impl LeChatPHPClient {
let login_url = format!("{}/{}", &self.config.url, &self.config.page_php);
let resp = self.client.get(&login_url).send()?;
if resp.status() == StatusCode::BAD_GATEWAY {
- return Err(SERVER_DOWN_ERR.into());
+ return Err(LoginErr::ServerDownErr);
}
let resp = resp.text()?;
let doc = Document::from(resp.as_str());
@@ -814,8 +854,7 @@ impl LeChatPHPClient {
let mut captcha_input = String::new();
if self.manual_captcha {
// Otherwise, save the captcha on disk and prompt user for answer
- let img_decoded =
- general_purpose::STANDARD.decode(captcha_img.strip_prefix("data:image/gif;base64,").unwrap())?;
+ let img_decoded = general_purpose::STANDARD.decode(captcha_img.strip_prefix("data:image/gif;base64,").unwrap()).unwrap();
let img = image::load_from_memory(&img_decoded).unwrap();
let img_buf = image::imageops::resize(
&img,
@@ -836,7 +875,7 @@ impl LeChatPHPClient {
} else {
match lechatphp::solve_b64(captcha_img) {
Some(answer) => captcha_input = answer,
- None => return Err(CAPTCHA_FAILED_SOLVE_ERR.into()),
+ None => return Err(LoginErr::CaptchaFailedSolveErr),
}
}
@@ -848,8 +887,8 @@ impl LeChatPHPClient {
let mut resp = self.client.post(&login_url).form(¶ms).send()?;
match resp.status() {
- StatusCode::BAD_GATEWAY => return Err(SERVER_DOWN_ERR.into()),
- StatusCode::INTERNAL_SERVER_ERROR => return Err(SERVER_DOWN_500_ERR.into()),
+ StatusCode::BAD_GATEWAY => return Err(LoginErr::ServerDownErr),
+ StatusCode::INTERNAL_SERVER_ERROR => return Err(LoginErr::ServerDown500Err),
_ => {}
}
@@ -865,15 +904,15 @@ impl LeChatPHPClient {
let mut resp = resp.text()?;
if resp.contains(CAPTCHA_USED_ERR) {
- return Err(CAPTCHA_USED_ERR.into());
+ return Err(LoginErr::CaptchaUsedErr);
} else if resp.contains(CAPTCHA_WG_ERR) {
- return Err(CAPTCHA_WG_ERR.into());
+ return Err(LoginErr::CaptchaWgErr);
} else if resp.contains(REG_ERR) {
- return Err(REG_ERR.into());
+ return Err(LoginErr::RegErr);
} else if resp.contains(NICKNAME_ERR) {
- return Err(NICKNAME_ERR.into());
+ return Err(LoginErr::NicknameErr);
} else if resp.contains(KICKED_ERR) {
- return Err(KICKED_ERR.into());
+ return Err(LoginErr::KickedErr);
}
let mut doc = Document::from(resp.as_str());
@@ -883,7 +922,7 @@ impl LeChatPHPClient {
if let Some(h2) = doc.find(Name("h2")).next() {
eprintln!("{}", h2.text());
}
- return Err(UNKNOWN_ERR.into());
+ return Err(LoginErr::UnknownErr);
} else if body_class == "failednotice" {
eprintln!("failed logins: {}", body.text());
let nc = doc.find(Attr("name", "nc")).next().unwrap();