Use TMPDIR with macOS, and set socket SO_SNDBUF and SO_RCVBUF
This commit is contained in:
+3
-2
@@ -1,4 +1,5 @@
|
||||
extern crate byteorder;
|
||||
extern crate nix;
|
||||
#[cfg(windows)]
|
||||
extern crate named_pipe;
|
||||
|
||||
@@ -10,7 +11,7 @@ mod proxy_socket;
|
||||
|
||||
use proxy_socket::ProxySocket;
|
||||
|
||||
const BUFFER_SIZE: u32 = 1024 * 16; // 1024 ^ 2 is the maximum
|
||||
const BUFFER_SIZE: u32 = 1024 ^ 2; // 1024 ^ 2 is the maximum
|
||||
|
||||
fn valid_length(length: u32) -> bool {
|
||||
return length > 0 && length <= BUFFER_SIZE;
|
||||
@@ -56,7 +57,7 @@ fn write_response(buf: &[u8]) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut socket = proxy_socket::connect().unwrap();
|
||||
let mut socket = proxy_socket::connect(BUFFER_SIZE).unwrap();
|
||||
|
||||
// Start thread for user input reading
|
||||
let ui = thread::spawn(move || {
|
||||
|
||||
+10
-4
@@ -2,7 +2,11 @@ use std::env;
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use nix::sys::socket;
|
||||
use nix::sys::socket::sockopt::SndBuf;
|
||||
use nix::sys::socket::sockopt::RcvBuf;
|
||||
|
||||
#[cfg(windows)]
|
||||
use named_pipe::PipeClient;
|
||||
@@ -28,7 +32,7 @@ impl<W: Write> Write for ProxySocket<W> {
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn connect() -> io::Result<ProxySocket<PipeClient>> {
|
||||
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<PipeClient>> {
|
||||
let username = env::var("USERNAME").unwrap();
|
||||
let pipe_name = format!("\\\\.\\pipe\\keepassxc\\{}\\kpxc_server", username);
|
||||
let client = PipeClient::connect(pipe_name)?;
|
||||
@@ -36,17 +40,19 @@ pub fn connect() -> io::Result<ProxySocket<PipeClient>> {
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
pub fn connect() -> io::Result<ProxySocket<UnixStream>> {
|
||||
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<UnixStream>> {
|
||||
use std::time::Duration;
|
||||
|
||||
let socket_name = "kpxc_server";
|
||||
let socket: String;
|
||||
if let Ok(xdg) = env::var("XDG_RUNTIME_DIR") {
|
||||
socket = format!("{}/{}", xdg, socket_name);
|
||||
if let Ok(dir) = if cfg!(target_os = "macos") {env::var("TMPDIR") } else { env::var("XDG_RUNTIME_DIR") } {
|
||||
socket = format!("{}/{}", dir, socket_name);
|
||||
} else {
|
||||
socket = format!("/tmp/{}", socket_name);
|
||||
}
|
||||
let s = UnixStream::connect(socket)?;
|
||||
socket::setsockopt(s.as_raw_fd(), SndBuf, &(buffer_size as usize)).expect("setsockopt for SndBuf failed");
|
||||
socket::setsockopt(s.as_raw_fd(), RcvBuf, &(buffer_size as usize)).expect("setsockopt for RcvBuf failed");
|
||||
let timeout: Option<Duration> = Some(Duration::from_secs(1));
|
||||
s.set_read_timeout(timeout)?;
|
||||
Ok(ProxySocket { inner: s })
|
||||
|
||||
Reference in New Issue
Block a user