correction + regexp
This commit is contained in:
@ -7,7 +7,10 @@ use std::io::{Read, Write};
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
const IPC_READ_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
const IPC_MAX_REQUEST_BYTES: usize = 4 * 1024 * 1024;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct HistoryItem {
|
||||
@ -75,16 +78,50 @@ pub fn start_server(db: Arc<Mutex<Database>>, crypto: Arc<Crypto>, socket_path:
|
||||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
Ok(mut stream) => {
|
||||
if let Err(e) = stream.set_read_timeout(Some(IPC_READ_TIMEOUT)) {
|
||||
eprintln!("Impossible de définir le timeout IPC : {e}");
|
||||
}
|
||||
|
||||
let db_clone = Arc::clone(&db);
|
||||
let crypto_clone = Arc::clone(&crypto);
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let mut buf = String::new();
|
||||
if stream.read_to_string(&mut buf).is_err() {
|
||||
return;
|
||||
let mut buf = Vec::new();
|
||||
let mut tmp = [0u8; 4096];
|
||||
|
||||
loop {
|
||||
match stream.read(&mut tmp) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
buf.extend_from_slice(&tmp[..n]);
|
||||
if buf.len() > IPC_MAX_REQUEST_BYTES {
|
||||
eprintln!("IPC : requête trop grande, abandon");
|
||||
return;
|
||||
}
|
||||
}
|
||||
Err(e)
|
||||
if e.kind() == std::io::ErrorKind::WouldBlock
|
||||
|| e.kind() == std::io::ErrorKind::TimedOut =>
|
||||
{
|
||||
eprintln!("IPC : timeout de lecture");
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("IPC read error : {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let req = match serde_json::from_str::<IpcRequest>(&buf) {
|
||||
let buf_str = match String::from_utf8(buf) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
eprintln!("IPC : requête non-UTF8 : {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let req = match serde_json::from_str::<IpcRequest>(&buf_str) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
eprintln!("IPC parse error : {e}");
|
||||
@ -94,6 +131,8 @@ pub fn start_server(db: Arc<Mutex<Database>>, crypto: Arc<Crypto>, socket_path:
|
||||
|
||||
match req {
|
||||
IpcRequest::GetHistory { limit } => {
|
||||
// Limite à 1000 pour éviter les requêtes abusives
|
||||
let limit = limit.min(1000);
|
||||
let lock = db_clone.lock().unwrap();
|
||||
let history = lock.read_history(limit).unwrap_or_default();
|
||||
let items: Vec<HistoryItem> = history
|
||||
@ -126,12 +165,12 @@ pub fn start_server(db: Arc<Mutex<Database>>, crypto: Arc<Crypto>, socket_path:
|
||||
})
|
||||
} else if Crypto::is_password_encrypted(&content) {
|
||||
reply(
|
||||
&mut stream,
|
||||
IpcResponse::Error(
|
||||
"Entrée chiffrée par mot de passe : déchiffrez-la côté client avant de coller"
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
&mut stream,
|
||||
IpcResponse::Error(
|
||||
"Entrée chiffrée par mot de passe : déchiffrez-la côté client avant de coller"
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
return;
|
||||
} else {
|
||||
content
|
||||
@ -153,6 +192,7 @@ pub fn start_server(db: Arc<Mutex<Database>>, crypto: Arc<Crypto>, socket_path:
|
||||
height: h,
|
||||
bytes: std::borrow::Cow::Owned(rgba.into_raw()),
|
||||
});
|
||||
reply(&mut stream, IpcResponse::Ok);
|
||||
} else {
|
||||
reply(
|
||||
&mut stream,
|
||||
@ -160,13 +200,12 @@ pub fn start_server(db: Arc<Mutex<Database>>, crypto: Arc<Crypto>, socket_path:
|
||||
"Image introuvable : {actual}"
|
||||
)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let _ = cb.set_text(actual);
|
||||
reply(&mut stream, IpcResponse::Ok);
|
||||
}
|
||||
reply(&mut stream, IpcResponse::Ok);
|
||||
}
|
||||
Err(e) => reply(&mut stream, IpcResponse::Error(e.to_string())),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user