refactor with generics

This commit is contained in:
Andy Brandt
2017-11-30 16:03:28 -06:00
parent 14c705359e
commit fcfc22296a
4 changed files with 60 additions and 86 deletions
+8 -18
View File
@@ -6,23 +6,15 @@ use std::io::{self, Read, Write};
use std::thread; use std::thread;
use byteorder::{ByteOrder, NativeEndian, WriteBytesExt}; use byteorder::{ByteOrder, NativeEndian, WriteBytesExt};
#[cfg(not(windows))] mod proxy_socket;
#[path = "platform_unix.rs"]
mod platform;
#[cfg(windows)] use proxy_socket::ProxySocket;
#[path = "platform_windows.rs"]
mod platform;
use platform::ProxySocket; fn valid_length(length: u32) -> bool {
fn valid_length(length: u32) -> bool
{
return length > 0 && length <= 4096; // 1024 ^ 2 is the maximum return length > 0 && length <= 4096; // 1024 ^ 2 is the maximum
} }
fn read_header() -> u32 fn read_header() -> u32 {
{
let stdin = io::stdin(); let stdin = io::stdin();
let mut buf = vec![0; 4]; let mut buf = vec![0; 4];
let mut handle = stdin.lock(); let mut handle = stdin.lock();
@@ -31,8 +23,7 @@ fn read_header() -> u32
NativeEndian::read_u32(&buf) NativeEndian::read_u32(&buf)
} }
fn read_body(length: u32, socket: &mut ProxySocket) fn read_body<T: Read + Write>(length: u32, socket: &mut ProxySocket<T>) {
{
let mut buffer = vec![0; length as usize]; let mut buffer = vec![0; length as usize];
let stdin = io::stdin(); let stdin = io::stdin();
let mut handle = stdin.lock(); 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<T: Read>(socket: &mut ProxySocket<T>) {
{
let mut buf = vec![0; 1024 * 1024]; let mut buf = vec![0; 1024 * 1024];
if let Ok(len) = socket.read(&mut buf) { if let Ok(len) = socket.read(&mut buf) {
// for some reason the length is 1 byte too long in linux? // for some reason the length is 1 byte too long in linux?
@@ -69,7 +59,7 @@ fn write_response(buf: &[u8]) {
} }
fn main() { fn main() {
let mut socket = ProxySocket::connect().unwrap(); let mut socket = proxy_socket::connect().unwrap();
// Start thread for user input reading // Start thread for user input reading
let ui = thread::spawn(move || { let ui = thread::spawn(move || {
@@ -79,5 +69,5 @@ fn main() {
} }
}); });
let _ui_res = ui.join(); let _ui_res = ui.join().unwrap();
} }
-39
View File
@@ -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<ProxySocket> {
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<Duration> = 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<usize> {
self.0.read(buf)
}
}
impl Write for ProxySocket {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> Result<()> {
self.0.flush()
}
}
-28
View File
@@ -1,28 +0,0 @@
use std::io::{Result, Read, Write};
use named_pipe::PipeClient;
pub struct ProxySocket(PipeClient);
impl ProxySocket {
pub fn connect() -> Result<ProxySocket> {
let client = PipeClient::connect("\\\\.\\pipe\\KeePassHttp").unwrap();
Ok(ProxySocket(client))
}
}
impl Read for ProxySocket {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.0.read(buf)
}
}
impl Write for ProxySocket {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> Result<()> {
self.0.flush()
}
}
+51
View File
@@ -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<T> {
inner: T,
}
impl<R: Read> Read for ProxySocket<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.read(buf)
}
}
impl<W: Write> Write for ProxySocket<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}
#[cfg(windows)]
pub fn connect() -> io::Result<ProxySocket<PipeClient>> {
let client = PipeClient::connect("\\\\.\\pipe\\KeePassHttp")?;
Ok(ProxySocket { inner: client })
}
#[cfg(not(windows))]
pub fn connect() -> io::Result<ProxySocket<UnixStream>> {
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<Duration> = Some(Duration::from_secs(1));
s.set_read_timeout(timeout)?;
Ok(ProxySocket { inner: s })
}