1. 完善msg的处理工作。到了可以调试的步骤了

2. 准备增加屏幕和鼠标的获取相关代码,尚不能编译 通过
This commit is contained in:
Ekko.bao 2024-08-09 08:38:51 +08:00
parent ef5ba10d0e
commit 5d99d5bf46
3 changed files with 64 additions and 50 deletions

View File

@ -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"
winapi = { version = "0.3.9", features = ["winnt"] }

View File

@ -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<Mutex<ClipboardSync>>) -> Result<task::JoinH
enum Msg <'a>{
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("<title>Copy & Paste</title>") {
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::<Vec<_>>();
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::<Vec<_>>();
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::<Vec<&str>>();
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::<Vec<_>>().concat();
let text = element.text().collect::<String>();
@ -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::<String>();
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;
}
}
}

17
src/sys_res/mod.rs Executable file
View File

@ -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)
}