Unix socket version

This commit is contained in:
varjolintu
2017-11-16 09:31:47 +02:00
parent f364455c10
commit 6c5f96f407
2 changed files with 33 additions and 47 deletions
+1 -1
View File
@@ -4,4 +4,4 @@ Application that works as a proxy between Native Messaging browser extension and
This is still under development. Installing the proxy needs manual changes to JSON scripts installed for Native Messaging. This is still under development. Installing the proxy needs manual changes to JSON scripts installed for Native Messaging.
See [this page](https://developer.chrome.com/extensions/nativeMessaging) for further information. See [this page](https://developer.chrome.com/extensions/nativeMessaging) for further information.
keepassxc-proxy listens stdin from keepassxc-browser extension and transfers the data to UDP port 19700 which KeePassXC listens. keepassxc-proxy listens stdin from keepassxc-browser extension and transfers the data to Unix domain socket `/tmp/kpxc_server` which KeePassXC listens.
+29 -43
View File
@@ -1,7 +1,8 @@
extern crate byteorder; extern crate byteorder;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::net::UdpSocket; use std::os::unix::net::UnixStream;
use std::os::unix::net::UnixListener;
use std::str; use std::str;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@@ -9,7 +10,7 @@ use byteorder::{ByteOrder, NativeEndian, WriteBytesExt};
fn valid_length(length: u32) -> bool fn valid_length(length: u32) -> bool
{ {
return length > 0 && length <= 16384; // 1024 ^ 2 is the maximum return length > 0 && length <= 4096; // 1024 ^ 2 is the maximum
} }
fn read_header() -> (u32) fn read_header() -> (u32)
@@ -18,44 +19,39 @@ fn read_header() -> (u32)
let mut buf = vec![0; 4]; let mut buf = vec![0; 4];
let mut handle = stdin.lock(); let mut handle = stdin.lock();
handle.read_exact(&mut buf); handle.read_exact(&mut buf).unwrap();
let length: u32 = NativeEndian::read_u32(&buf); let length: u32 = NativeEndian::read_u32(&buf);
return length; return length;
} }
fn read_body(length: u32, socket: &UdpSocket) fn read_body(length: u32, mut socket: &UnixStream)
{ {
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();
match handle.read_exact(&mut buffer) { match handle.read_exact(&mut buffer) {
Ok(v) => { Ok(_v) => {
if valid_length(length) { if valid_length(length) {
socket.send_to(&buffer, "127.0.0.1:19700").expect("Cannot send data"); socket.write(&buffer).unwrap();
socket.flush().unwrap();
read_unix_response(length, &socket);
} }
}, },
Err(e) => panic!("Read error: {}", e) Err(_e) => {}
//Err(e) => {}
} }
} }
fn read_udp_response(socket: &UdpSocket) fn read_unix_response(length: u32, mut socket: &UnixStream)
{ {
let mut buf = [0; 4069]; let mut buf = vec![0; length as usize];
match socket.recv_from(&mut buf) { match socket.read(&mut buf) {
Ok((length, src)) => { Ok(_length) => {
if valid_length(length as u32) { let text = str::from_utf8(&buf).unwrap();
thread::spawn(move || { write_output(text);
let buf = &mut buf[..length]; },
let text = str::from_utf8(&buf).unwrap(); Err(_e) => {}
write_output(text);
});
}
},
//Err(e) => panic!("Read error: {}", e)
Err(e) => {}
} }
} }
@@ -66,32 +62,22 @@ fn write_output(text: &str)
let mut handle = stdout.lock(); let mut handle = stdout.lock();
handle.write_u32::<NativeEndian>(textlen as u32).unwrap(); handle.write_u32::<NativeEndian>(textlen as u32).unwrap();
handle.write(text.as_bytes()); handle.write(text.as_bytes()).unwrap();
} }
fn main() { 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 socket = UdpSocket::bind("127.0.0.1:0").expect("Couldn't bind to address"); // Start thread for user input reading
let timeout: Option<Duration> = Some(Duration::from_secs(1)); let send_socket = socket.try_clone().expect("Cannot clone socket");
socket.set_read_timeout(timeout); let ui = thread::spawn(move || {
loop {
// Start thread for user input reading let length = read_header();
let send_socket = socket.try_clone().expect("Cannot clone socket"); read_body(length, &send_socket);
let ui = thread::spawn(move || {
loop {
let length = read_header();
read_body(length, &send_socket);
} }
}); });
// Start thread for UDP packet receiving let _ui_res = ui.join();
let recv_socket = socket.try_clone().expect("Cannot clone socket");
let pr = thread::spawn(move || {
loop {
read_udp_response(&recv_socket);
}
});
let ui_res = ui.join();
let pr_res = pr.join();
} }