add windows named pipe support
This commit is contained in:
+42
-40
@@ -1,83 +1,85 @@
|
||||
extern crate byteorder;
|
||||
#[cfg(windows)]
|
||||
extern crate named_pipe;
|
||||
|
||||
use std::io::{self, Read, Write};
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::str;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use byteorder::{ByteOrder, NativeEndian, WriteBytesExt};
|
||||
|
||||
#[cfg(not(windows))]
|
||||
#[path = "platform_unix.rs"]
|
||||
mod platform;
|
||||
|
||||
#[cfg(windows)]
|
||||
#[path = "platform_windows.rs"]
|
||||
mod platform;
|
||||
|
||||
use platform::ProxySocket;
|
||||
|
||||
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();
|
||||
|
||||
handle.read_exact(&mut buf).unwrap();
|
||||
let length: u32 = NativeEndian::read_u32(&buf);
|
||||
return length;
|
||||
NativeEndian::read_u32(&buf)
|
||||
}
|
||||
|
||||
fn read_body(length: u32, mut socket: &UnixStream)
|
||||
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();
|
||||
|
||||
match handle.read_exact(&mut buffer) {
|
||||
Ok(_v) => {
|
||||
if valid_length(length) {
|
||||
socket.write(&buffer).unwrap();
|
||||
socket.flush().unwrap();
|
||||
read_unix_response(length, &socket);
|
||||
}
|
||||
},
|
||||
Err(_e) => {}
|
||||
if let Ok(_) = handle.read_exact(&mut buffer) {
|
||||
if valid_length(length) {
|
||||
socket.write_u32::<NativeEndian>(length).unwrap();
|
||||
socket.write(&buffer).unwrap();
|
||||
socket.flush().unwrap();
|
||||
read_response(socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_unix_response(length: u32, mut socket: &UnixStream)
|
||||
fn read_response(socket: &mut ProxySocket)
|
||||
{
|
||||
let mut buf = vec![0; length as usize];
|
||||
let mut len_buf = vec![0; 4];
|
||||
|
||||
match socket.read(&mut buf) {
|
||||
Ok(_length) => {
|
||||
let text = str::from_utf8(&buf).unwrap();
|
||||
write_output(text);
|
||||
},
|
||||
Err(_e) => {}
|
||||
}
|
||||
if let Ok(_) = socket.read_exact(&mut len_buf) {
|
||||
let len = NativeEndian::read_u32(&len_buf);
|
||||
let mut buf = vec![0; len as usize];
|
||||
if let Ok(_) = socket.read_exact(&mut buf) {
|
||||
write_response(&buf, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_output(text: &str)
|
||||
{
|
||||
let textlen = text.len();
|
||||
fn write_response(buf: &[u8], len: u32) {
|
||||
let stdout = io::stdout();
|
||||
let mut handle = stdout.lock();
|
||||
let mut out = stdout.lock();
|
||||
|
||||
handle.write_u32::<NativeEndian>(textlen as u32).unwrap();
|
||||
handle.write(text.as_bytes()).unwrap();
|
||||
out.write_u32::<NativeEndian>(len).unwrap();
|
||||
out.write(buf).unwrap();
|
||||
out.flush().unwrap();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let socket = UnixStream::connect("/tmp/kpxc_server").unwrap();
|
||||
let timeout: Option<Duration> = Some(Duration::from_secs(1));
|
||||
socket.set_read_timeout(timeout).unwrap();
|
||||
let mut socket = ProxySocket::connect().unwrap();
|
||||
|
||||
// Start thread for user input reading
|
||||
let send_socket = socket.try_clone().expect("Cannot clone socket");
|
||||
//let mut send_socket = socket.try_clone().expect("Cannot clone socket");
|
||||
let ui = thread::spawn(move || {
|
||||
loop {
|
||||
let length = read_header();
|
||||
read_body(length, &send_socket);
|
||||
}
|
||||
});
|
||||
read_body(length, &mut socket);
|
||||
}
|
||||
});
|
||||
|
||||
let _ui_res = ui.join();
|
||||
let _ui_res = ui.join();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
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 user = env::var("USER").unwrap();
|
||||
let s = UnixStream::connect(format!("/tmp/keepassxc-{}.socket", user))?;
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user