Skip to content

Commit 211ae91

Browse files
author
varjolintu
committed
Use TMPDIR with macOS, and set socket SO_SNDBUF and SO_RCVBUF
1 parent 759507e commit 211ae91

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ repository = "https://github.com/varjolintu/keepassxc-proxy-rust"
88

99
[dependencies]
1010
byteorder = "1.1.0"
11+
nix = "0.11.0"
1112

1213
[target.'cfg(windows)'.dependencies]
1314
named_pipe = "0.2"

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate byteorder;
2+
extern crate nix;
23
#[cfg(windows)]
34
extern crate named_pipe;
45

@@ -10,7 +11,7 @@ mod proxy_socket;
1011

1112
use proxy_socket::ProxySocket;
1213

13-
const BUFFER_SIZE: u32 = 1024 * 16; // 1024 ^ 2 is the maximum
14+
const BUFFER_SIZE: u32 = 1024 ^ 2; // 1024 ^ 2 is the maximum
1415

1516
fn valid_length(length: u32) -> bool {
1617
return length > 0 && length <= BUFFER_SIZE;
@@ -56,7 +57,7 @@ fn write_response(buf: &[u8]) {
5657
}
5758

5859
fn main() {
59-
let mut socket = proxy_socket::connect().unwrap();
60+
let mut socket = proxy_socket::connect(BUFFER_SIZE).unwrap();
6061

6162
// Start thread for user input reading
6263
let ui = thread::spawn(move || {

src/proxy_socket.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use std::env;
22
use std::io::{self, Read, Write};
33

44
#[cfg(not(windows))]
5+
use std::os::unix::io::AsRawFd;
56
use std::os::unix::net::UnixStream;
7+
use nix::sys::socket;
8+
use nix::sys::socket::sockopt::SndBuf;
9+
use nix::sys::socket::sockopt::RcvBuf;
610

711
#[cfg(windows)]
812
use named_pipe::PipeClient;
@@ -28,25 +32,27 @@ impl<W: Write> Write for ProxySocket<W> {
2832
}
2933

3034
#[cfg(windows)]
31-
pub fn connect() -> io::Result<ProxySocket<PipeClient>> {
35+
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<PipeClient>> {
3236
let username = env::var("USERNAME").unwrap();
3337
let pipe_name = format!("\\\\.\\pipe\\keepassxc\\{}\\kpxc_server", username);
3438
let client = PipeClient::connect(pipe_name)?;
3539
Ok(ProxySocket { inner: client })
3640
}
3741

3842
#[cfg(not(windows))]
39-
pub fn connect() -> io::Result<ProxySocket<UnixStream>> {
43+
pub fn connect(buffer_size: u32) -> io::Result<ProxySocket<UnixStream>> {
4044
use std::time::Duration;
4145

4246
let socket_name = "kpxc_server";
4347
let socket: String;
44-
if let Ok(xdg) = env::var("XDG_RUNTIME_DIR") {
45-
socket = format!("{}/{}", xdg, socket_name);
48+
if let Ok(dir) = if cfg!(target_os = "macos") {env::var("TMPDIR") } else { env::var("XDG_RUNTIME_DIR") } {
49+
socket = format!("{}/{}", dir, socket_name);
4650
} else {
4751
socket = format!("/tmp/{}", socket_name);
4852
}
4953
let s = UnixStream::connect(socket)?;
54+
socket::setsockopt(s.as_raw_fd(), SndBuf, &(buffer_size as usize)).expect("setsockopt for SndBuf failed");
55+
socket::setsockopt(s.as_raw_fd(), RcvBuf, &(buffer_size as usize)).expect("setsockopt for RcvBuf failed");
5056
let timeout: Option<Duration> = Some(Duration::from_secs(1));
5157
s.set_read_timeout(timeout)?;
5258
Ok(ProxySocket { inner: s })

0 commit comments

Comments
 (0)