got it working in both windows & linux

This commit is contained in:
Andy Brandt
2017-11-30 13:57:32 -06:00
parent 5b9d6eac06
commit 14c705359e
2 changed files with 18 additions and 13 deletions
+9 -11
View File
@@ -39,7 +39,6 @@ fn read_body(length: u32, socket: &mut ProxySocket)
if let Ok(_) = handle.read_exact(&mut buffer) { if let Ok(_) = handle.read_exact(&mut buffer) {
if valid_length(length) { if valid_length(length) {
socket.write_u32::<NativeEndian>(length).unwrap();
socket.write(&buffer).unwrap(); socket.write(&buffer).unwrap();
socket.flush().unwrap(); socket.flush().unwrap();
read_response(socket); read_response(socket);
@@ -49,22 +48,22 @@ fn read_body(length: u32, socket: &mut ProxySocket)
fn read_response(socket: &mut ProxySocket) fn read_response(socket: &mut ProxySocket)
{ {
let mut len_buf = vec![0; 4]; let mut buf = vec![0; 1024 * 1024];
if let Ok(len) = socket.read(&mut buf) {
if let Ok(_) = socket.read_exact(&mut len_buf) { // for some reason the length is 1 byte too long in linux?
let len = NativeEndian::read_u32(&len_buf); let mut adjust = 0;
let mut buf = vec![0; len as usize]; if cfg!(not(windows)) {
if let Ok(_) = socket.read_exact(&mut buf) { adjust = 1;
write_response(&buf, len);
} }
write_response(&buf[0..len - adjust]);
} }
} }
fn write_response(buf: &[u8], len: u32) { fn write_response(buf: &[u8]) {
let stdout = io::stdout(); let stdout = io::stdout();
let mut out = stdout.lock(); let mut out = stdout.lock();
out.write_u32::<NativeEndian>(len).unwrap(); out.write_u32::<NativeEndian>(buf.len() as u32).unwrap();
out.write(buf).unwrap(); out.write(buf).unwrap();
out.flush().unwrap(); out.flush().unwrap();
} }
@@ -73,7 +72,6 @@ fn main() {
let mut socket = ProxySocket::connect().unwrap(); let mut socket = ProxySocket::connect().unwrap();
// Start thread for user input reading // Start thread for user input reading
//let mut send_socket = socket.try_clone().expect("Cannot clone socket");
let ui = thread::spawn(move || { let ui = thread::spawn(move || {
loop { loop {
let length = read_header(); let length = read_header();
+9 -2
View File
@@ -7,8 +7,15 @@ pub struct ProxySocket(UnixStream);
impl ProxySocket { impl ProxySocket {
pub fn connect() -> Result<ProxySocket> { pub fn connect() -> Result<ProxySocket> {
let user = env::var("USER").unwrap(); let socket_name = "kpxc_server";
let s = UnixStream::connect(format!("/tmp/keepassxc-{}.socket", user))?; 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)); let timeout: Option<Duration> = Some(Duration::from_secs(1));
s.set_read_timeout(timeout)?; s.set_read_timeout(timeout)?;
Ok(ProxySocket(s)) Ok(ProxySocket(s))