diff --git a/src/main.rs b/src/main.rs index db4b98f..bd73f4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,23 +6,15 @@ use std::io::{self, Read, Write}; use std::thread; use byteorder::{ByteOrder, NativeEndian, WriteBytesExt}; -#[cfg(not(windows))] -#[path = "platform_unix.rs"] -mod platform; +mod proxy_socket; -#[cfg(windows)] -#[path = "platform_windows.rs"] -mod platform; +use proxy_socket::ProxySocket; -use platform::ProxySocket; - -fn valid_length(length: u32) -> bool -{ - return length > 0 && length <= 4096; // 1024 ^ 2 is the maximum +fn valid_length(length: u32) -> bool { + return length > 0 && length <= 4096; // 1024 ^ 2 is the maximum } -fn read_header() -> u32 -{ +fn read_header() -> u32 { let stdin = io::stdin(); let mut buf = vec![0; 4]; let mut handle = stdin.lock(); @@ -31,8 +23,7 @@ fn read_header() -> u32 NativeEndian::read_u32(&buf) } -fn read_body(length: u32, socket: &mut ProxySocket) -{ +fn read_body(length: u32, socket: &mut ProxySocket) { let mut buffer = vec![0; length as usize]; let stdin = io::stdin(); let mut handle = stdin.lock(); @@ -46,8 +37,7 @@ fn read_body(length: u32, socket: &mut ProxySocket) } } -fn read_response(socket: &mut ProxySocket) -{ +fn read_response(socket: &mut ProxySocket) { let mut buf = vec![0; 1024 * 1024]; if let Ok(len) = socket.read(&mut buf) { // for some reason the length is 1 byte too long in linux? @@ -69,7 +59,7 @@ fn write_response(buf: &[u8]) { } fn main() { - let mut socket = ProxySocket::connect().unwrap(); + let mut socket = proxy_socket::connect().unwrap(); // Start thread for user input reading let ui = thread::spawn(move || { @@ -79,5 +69,5 @@ fn main() { } }); - let _ui_res = ui.join(); + let _ui_res = ui.join().unwrap(); } diff --git a/src/platform_unix.rs b/src/platform_unix.rs deleted file mode 100644 index 9eb79cd..0000000 --- a/src/platform_unix.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::env; -use std::io::{Read, Result, Write}; -use std::os::unix::net::UnixStream; -use std::time::Duration; - -pub struct ProxySocket(UnixStream); - -impl ProxySocket { - pub fn connect() -> Result { - let socket_name = "kpxc_server"; - let socket: String; - if let Ok(xdg) = env::var("XDG_RUNTIME_DIR") { - socket = format!("{}/{}", xdg, socket_name); - } else { - socket = format!("/tmp/{}", socket_name); - } - let msg = format!("Can't connect to socket: {}", socket); - let s = UnixStream::connect(socket).expect(&msg); - let timeout: Option = Some(Duration::from_secs(1)); - s.set_read_timeout(timeout)?; - Ok(ProxySocket(s)) - } -} - -impl Read for ProxySocket { - fn read(&mut self, buf: &mut [u8]) -> Result { - self.0.read(buf) - } -} - -impl Write for ProxySocket { - fn write(&mut self, buf: &[u8]) -> Result { - self.0.write(buf) - } - - fn flush(&mut self) -> Result<()> { - self.0.flush() - } -} diff --git a/src/platform_windows.rs b/src/platform_windows.rs deleted file mode 100644 index 8a10321..0000000 --- a/src/platform_windows.rs +++ /dev/null @@ -1,28 +0,0 @@ - -use std::io::{Result, Read, Write}; -use named_pipe::PipeClient; - -pub struct ProxySocket(PipeClient); - -impl ProxySocket { - pub fn connect() -> Result { - let client = PipeClient::connect("\\\\.\\pipe\\KeePassHttp").unwrap(); - Ok(ProxySocket(client)) - } -} - -impl Read for ProxySocket { - fn read(&mut self, buf: &mut [u8]) -> Result { - self.0.read(buf) - } -} - -impl Write for ProxySocket { - fn write(&mut self, buf: &[u8]) -> Result { - self.0.write(buf) - } - - fn flush(&mut self) -> Result<()> { - self.0.flush() - } -} diff --git a/src/proxy_socket.rs b/src/proxy_socket.rs new file mode 100644 index 0000000..e7899ab --- /dev/null +++ b/src/proxy_socket.rs @@ -0,0 +1,51 @@ +use std::io::{self, Read, Write}; + +#[cfg(not(windows))] +use std::os::unix::net::UnixStream; + +#[cfg(windows)] +use named_pipe::PipeClient; + +pub struct ProxySocket { + inner: T, +} + +impl Read for ProxySocket { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + self.inner.read(buf) + } +} + +impl Write for ProxySocket { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +#[cfg(windows)] +pub fn connect() -> io::Result> { + let client = PipeClient::connect("\\\\.\\pipe\\KeePassHttp")?; + Ok(ProxySocket { inner: client }) +} + +#[cfg(not(windows))] +pub fn connect() -> io::Result> { + use std::env; + 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); + } else { + socket = format!("/tmp/{}", socket_name); + } + let s = UnixStream::connect(socket)?; + let timeout: Option = Some(Duration::from_secs(1)); + s.set_read_timeout(timeout)?; + Ok(ProxySocket { inner: s }) +}