Skip to content

Commit

Permalink
Use bool in callback instead since it's clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Nov 8, 2024
1 parent 75f4ed0 commit 1d115c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
29 changes: 13 additions & 16 deletions crates/shared/src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub mod commands;
use std::{
convert::Infallible,
io::{self, ErrorKind},
ops::ControlFlow,
os::windows::prelude::{AsHandle, AsRawHandle as _},
sync::LazyLock,
};
Expand Down Expand Up @@ -129,7 +128,7 @@ impl Server {
pub fn recv_all(
&mut self,
cb: impl Fn(Receive),
mut auth: impl FnMut(Pid, Auth) -> ControlFlow<()>,
mut auth: impl FnMut(Pid, Auth) -> bool,
) -> io::Result<Infallible> {
let span = trace_span!("pipe");
let _guard = span.enter();
Expand Down Expand Up @@ -163,10 +162,11 @@ impl Server {
let fut = async {
loop {
unsafe {
self.recv_one(&mut sa, &cb, &mut auth).await?;
self.buf.clear();
self.msg_len = None;
self.connect(&mut sa, &cb, &mut auth).await?;
}

self.buf.clear();
self.msg_len = None;
}
};

Expand All @@ -175,11 +175,11 @@ impl Server {

/// # Safety:
/// sa must be valid
async unsafe fn recv_one(
async unsafe fn connect(
&mut self,
sa: *mut SECURITY_ATTRIBUTES,
cb: &impl Fn(Receive),
auth: &mut impl FnMut(Pid, Auth) -> ControlFlow<()>,
auth: &mut impl FnMut(Pid, Auth) -> bool,
) -> Result<(), io::Error> {
let server = unsafe {
ServerOptions::new()
Expand All @@ -204,7 +204,7 @@ impl Server {
return Ok(());
}

let mut authed = false;
let mut first = true;
loop {
if server.readable().await.is_err() {
break;
Expand Down Expand Up @@ -241,7 +241,7 @@ impl Server {

let data = &self.buf[size_of::<usize>()..len + size_of::<usize>()];

if !authed {
if first {
let span = trace_span!("auth");
let _guard = span.enter();

Expand All @@ -266,15 +266,12 @@ impl Server {
return Ok(());
}

match auth(pid, auth_code) {
ControlFlow::Continue(_) => (),
ControlFlow::Break(_) => {
_ = server.disconnect();
return Ok(());
}
if !auth(pid, auth_code) {
_ = server.disconnect();
return Ok(());
}

authed = true;
first = false;
} else {
_ = server.disconnect();
return Ok(());
Expand Down
15 changes: 2 additions & 13 deletions crates/yabg3nml/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
convert::Infallible,
io,
ops::ControlFlow,
sync::atomic::{AtomicU32, AtomicU64, Ordering},
};

Expand Down Expand Up @@ -61,22 +60,12 @@ pub fn server() -> io::Result<Infallible> {
let auth = |pid, code| {
let ppid = PID.load(Ordering::Relaxed);

trace!(pid, ppid, "verifying pid");

if ppid != pid {
return ControlFlow::Break(());
}

// load current auth code; also change auth code to always keep it randomized
let passcode = AUTH.swap(rand::random::<u64>(), Ordering::Relaxed);

trace!(code, passcode, "verifying auth code");
trace!(pid, ppid, code, passcode, "verifying pid and auth code");

if passcode == code {
ControlFlow::Continue(())
} else {
ControlFlow::Break(())
}
ppid == pid && passcode == code
};

server.recv_all(cb, auth)
Expand Down

0 comments on commit 1d115c8

Please sign in to comment.