refactor with generics
This commit is contained in:
+8
-18
@@ -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
|
||||
{
|
||||
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<T: Read + Write>(length: u32, socket: &mut ProxySocket<T>) {
|
||||
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<T: Read>(socket: &mut ProxySocket<T>) {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
Reference in New Issue
Block a user