From 5d99d5bf46c1c03dcade1e7296b6c62bb3d1b649 Mon Sep 17 00:00:00 2001 From: Begild Date: Fri, 9 Aug 2024 08:38:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E5=96=84msg=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=B7=A5=E4=BD=9C=E3=80=82=E5=88=B0=E4=BA=86=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=B0=83=E8=AF=95=E7=9A=84=E6=AD=A5=E9=AA=A4=E4=BA=86?= =?UTF-8?q?=202.=20=E5=87=86=E5=A4=87=E5=A2=9E=E5=8A=A0=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E5=92=8C=E9=BC=A0=E6=A0=87=E7=9A=84=E8=8E=B7=E5=8F=96=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=B0=9A=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E7=BC=96=E8=AF=91=20=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 10 +++--- src/lib.rs | 87 ++++++++++++++++++++++------------------------ src/sys_res/mod.rs | 17 +++++++++ 3 files changed, 64 insertions(+), 50 deletions(-) create mode 100755 src/sys_res/mod.rs diff --git a/Cargo.toml b/Cargo.toml index e70259a..ee8408f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,14 @@ edition = "2021" [dependencies] tokio = { version = "1.39.2", features = ["macros", "rt-multi-thread"] } tokio-macros = "2.4.0" -reqwest="0.12.5" -log="0.4.22" -env_logger="0.8" +reqwest = "0.12.5" +log = "0.4.22" +env_logger = "0.8" url = "2.2" percent-encoding = "2.1" trust-dns-resolver = "0.20" select = "0.5" scraper = "0.12" -arboard = "3.4.0" \ No newline at end of file +arboard = "3.4.0" + +winapi = { version = "0.3.9", features = ["winnt"] } diff --git a/src/lib.rs b/src/lib.rs index 7cff664..ce715ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ use std::collections::{BTreeMap, VecDeque}; extern crate log; pub mod wclip; +pub mod sys_res; pub struct ClipboardSync { user_name: String, password: String, @@ -20,6 +21,8 @@ pub struct ClipboardSync { ip: String, cookies: String, clip: wclip::Wclip, + //last message magic + cache_magic: u8, } const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.42"; @@ -46,7 +49,7 @@ pub async fn start_msg_sync(ctx:Arc>) -> Result{ Msg(&'a str), - Sub((i8, i8, i8, &'a str)), + Sub((u8, u8, u8, &'a str)), } impl ClipboardSync { @@ -58,6 +61,7 @@ impl ClipboardSync { ip: ip.to_string(), cookies: "".to_string(), clip: wclip::Wclip::new(), + cache_magic: 0xff, } } @@ -132,7 +136,6 @@ impl ClipboardSync { log::trace!("verify Response: {}", resp); } if resp.contains("Copy & Paste") { - self.msg2clip(&resp).await; Ok(()) } else { Err(Box::new(io::Error::new(io::ErrorKind::Other, "登录失败"))) @@ -164,46 +167,28 @@ impl ClipboardSync { //x|x|x| -> index|total|magic| let head = &msg[0..7]; let body = &msg[7..]; - let text = head.split('|').collect::>(); - if text.len() == 3 && text[0].len() == 1 && text[1].len() == 1 && text[2].len() == 1{ + let check = head.split('|').filter(|text| { + let text = text.as_bytes(); + if text.len() == 1 { + if text[0] >= b'!' && text[0] < b'|' {true} else {false} + } else { + false + } + }).map(|x|x.as_bytes()[0] - b'!').collect::>(); - let check = head.split('|').filter(|text| { - let text = text.as_bytes(); - if text.len() == 1 { - let text = text[0] as i8; - if text < '!' as i8 && text >= '|' as i8 { - true - } else { - false - } - } else { - false - } - }).collect::>(); - - let index = text[0].as_bytes()[0] as i8; - if index < '!' as i8 && index >= '|' as i8 { - return Msg::Msg(msg) - } - let total = text[1].as_bytes()[1] as i8; - if total < '!' as i8 && total >= '|' as i8 { - return Msg::Msg(msg) - } - let magic = text[2].as_bytes()[1] as i8; - if magic < '!' as i8 && magic >= '|' as i8 { - return Msg::Msg(msg) - } - return Msg::Sub((index, total, magic, body)); + if check.len() != 3 { + return Msg::Msg(msg) } - return Msg::Msg(msg) + + return Msg::Sub((check[0], check[1], check[2], body)); } pub async fn msg2clip(&mut self, html: &str) { // log::info!("html: {}", html); let document = Html::parse_document(html); let selector = Selector::parse("code").unwrap(); - let mut clip_text = String::new(); let mut msg_map = BTreeMap::new(); - let mut magic_ref = 0; + let mut magic_ref = 0xff; + let mut is_first = true; for element in document.select(&selector).into_iter() { //let text = element.text().collect::>().concat(); let text = element.text().collect::(); @@ -211,27 +196,37 @@ impl ClipboardSync { let msg = ClipboardSync::parse_msg(text.as_str()); match msg { Msg::Msg(msg) => { - if magic_ref == 0 { + if magic_ref == 0xff { self.clip.set(&msg); break; } }, - Msg::Sub((index, total, magic, body)) => { - if magic_ref == 0{ - magic_ref = magic; - } else if magic_ref == magic { - msg_map.insert(index, element); - if msg_map.len() == (total - 'a' as i8) as usize { + Msg::Sub((index, total, magic, _body)) => { + magic_ref = if magic_ref == 0xff {magic} else {magic_ref}; + if magic_ref != magic { // other message ignore + if is_first { + break; } - // clip_text.push_str(body); - } else { + is_first = false; + continue + } + if msg_map.len() == 0 && ( + self.cache_magic == magic_ref // message is repeated ignore it + || !is_first // we only process the newest msg + ) { + break; + } + // same msg pack, append to list + msg_map.insert(index, text); + if msg_map.len() as u8 == total { + let msg = msg_map.into_iter().map(|(_key, v)| v).collect::(); + self.cache_magic = magic_ref; // save for next msg + self.clip.set(&msg); break; } } } - } - if clip_text.len() != 0 { - self.clip.set(&clip_text); + is_first = false; } } } diff --git a/src/sys_res/mod.rs b/src/sys_res/mod.rs new file mode 100755 index 0000000..f9ee372 --- /dev/null +++ b/src/sys_res/mod.rs @@ -0,0 +1,17 @@ +extern crate winapi; + +use winapi::um::winuser::{GetCursorPos, GetSystemMetrics, SM_CXSCREEN, SM_CYSCREEN}; +use winapi::shared::windef::POINT; + + +pub fn get_screen_size() -> (i32, i32) { + let mut point = POINT { x: 0, y: 0 }; + unsafe { + GetCursorPos(&mut point); + } + let x = point.x; + let y = point.y; + let width = GetSystemMetrics(SM_CXSCREEN); + let height = GetSystemMetrics(SM_CYSCREEN); + (x, y, width, height) +} \ No newline at end of file