Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjanowsk committed Dec 2, 2024
1 parent 7baabdc commit 9e8e367
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions neptun/src/device/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub fn api_exec<R: Read, W: Write>(
};
// The protocol requires to return an error code as the response, or zero on success
writeln!(writer, "errno={}\n", status).ok();
_ = writer.flush();
cmd.clear();
}
}
Expand Down
20 changes: 13 additions & 7 deletions neptun/src/device/integration_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,26 +398,32 @@ mod tests {
}
}

fn read_until_double_new_line(&self, socket: &mut UnixStream) -> String {
let mut reader = BufReader::new(socket);
let mut ret = Vec::new();
loop {
if reader.read_until(b'\n', &mut ret).is_ok_and(|n| n == 1) {
break
}
}
String::from_utf8(ret).unwrap()
}

/// Issue a get command on the interface
fn wg_get(&self) -> String {
let path = format!("/var/run/wireguard/{}.sock", self.name);

let mut socket = UnixStream::connect(path).unwrap();
write!(socket, "get=1\n\n").unwrap();

let mut ret = String::new();
socket.read_to_string(&mut ret).unwrap();
ret
self.read_until_double_new_line(&mut socket)
}

/// Issue a set command on the interface
fn wg_set(&self, setting: &str) -> String {
let path = format!("/var/run/wireguard/{}.sock", self.name);
let mut socket = UnixStream::connect(path).unwrap();
write!(socket, "set=1\n{}\n\n", setting).unwrap();

let mut ret = String::new();
socket.read_to_string(&mut ret).unwrap();
let ret = self.read_until_double_new_line(&mut socket);
ret
}

Expand Down

0 comments on commit 9e8e367

Please sign in to comment.