diff --git a/Cargo.lock b/Cargo.lock index ceeca24..3098fc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,14 +1,30 @@ +[[package]] +name = "bitflags" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cc" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "keepassxc-proxy" version = "0.1.0" dependencies = [ "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "named_pipe 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", + "nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -20,6 +36,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "libc" +version = "0.2.43" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "named_pipe" version = "0.2.4" @@ -29,6 +50,23 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "nix" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" @@ -40,8 +78,14 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d0c54bb8f454c567f21197eefcdbf5679d0bd99f2ddbe52e84c77061952e6789" "checksum byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff81738b726f5d099632ceaffe7fb65b90212e8dce59d518729e7e8634032d3d" +"checksum cc 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "2119ea4867bd2b8ed3aecab467709720b2d55b1bcfe09f772fd68066eaf15275" +"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum named_pipe 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "516fe2b5b1131f912a3d4e0cbfec369ca9a65f89d477c15d86ef0fc376faaa55" +"checksum nix 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d37e713a259ff641624b6cb20e3b12b2952313ba36b6823c0f16e6cfd9e5de17" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index 635468a..71eae19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ repository = "https://github.com/varjolintu/keepassxc-proxy-rust" [dependencies] byteorder = "1.1.0" +nix = "0.11.0" [target.'cfg(windows)'.dependencies] named_pipe = "0.2" diff --git a/src/main.rs b/src/main.rs index 51b9e63..351599b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 || { diff --git a/src/proxy_socket.rs b/src/proxy_socket.rs index 40f11ef..db04d2d 100644 --- a/src/proxy_socket.rs +++ b/src/proxy_socket.rs @@ -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 Write for ProxySocket { } #[cfg(windows)] -pub fn connect() -> io::Result> { +pub fn connect(buffer_size: u32) -> io::Result> { 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> { } #[cfg(not(windows))] -pub fn connect() -> io::Result> { +pub fn connect(buffer_size: u32) -> io::Result> { 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 = Some(Duration::from_secs(1)); s.set_read_timeout(timeout)?; Ok(ProxySocket { inner: s })