Skip to content

Commit

Permalink
Reduce GUI repaint interval, display MB used in dashboard, optimize i…
Browse files Browse the repository at this point in the history
…mports, add byte tracking to socks5 loop, introduce stats module
  • Loading branch information
nullchinchilla committed Apr 19, 2024
1 parent b56c041 commit 67281d3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion binaries/geph5-client-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl App {

impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
ctx.request_repaint_after(Duration::from_millis(1000));
ctx.request_repaint_after(Duration::from_millis(100));

egui::TopBottomPanel::top("top").show(ctx, |ui| {
ui.horizontal(|ui| {
Expand Down
4 changes: 3 additions & 1 deletion binaries/geph5-client-gui/src/tabs/dashboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::time::{Duration, Instant};
use std::time::Duration;

use crate::{daemon::DAEMON, l10n::l10n, settings::get_config};

Expand All @@ -19,6 +19,8 @@ impl Dashboard {
let start_time = daemon.start_time().elapsed().as_secs() + 1;
let start_time = Duration::from_secs(1) * start_time as _;
columns[1].label(format!("{:?}", start_time));
let mb_used = (daemon.bytes_used() as f64) / 1_000_000.0;
columns[1].label(format!("{:.2} MB", mb_used));
}
None => {
columns[1].colored_label(egui::Color32::DARK_RED, l10n("disconnected"));
Expand Down
19 changes: 11 additions & 8 deletions binaries/geph5-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use anyctx::AnyCtx;
use clone_macro::clone;
use futures_util::{
future::{FusedFuture, Shared},
task::noop_waker,
FutureExt, TryFutureExt,
};
use futures_util::{future::Shared, task::noop_waker, FutureExt, TryFutureExt};
use geph5_broker_protocol::{Credential, ExitList};
use smol::future::FutureExt as _;
use std::{
net::SocketAddr,
path::PathBuf,
sync::Arc,
sync::{atomic::Ordering, Arc},
task::Context,
time::{Duration, Instant},
};
Expand All @@ -25,6 +21,7 @@ use crate::{
database::db_read_or_wait,
route::ExitConstraint,
socks5::socks5_loop,
stats::STAT_TOTAL_BYTES,
};

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -41,17 +38,18 @@ pub struct Config {

pub struct Client {
task: Shared<smol::Task<Result<(), Arc<anyhow::Error>>>>,
ctx: AnyCtx<Config>,
start_time: Instant,
}

impl Client {
/// Starts the client logic in the loop, returnign the handle.
pub fn start(cfg: Config) -> Self {
let ctx = AnyCtx::new(cfg);
let task = smolscale::spawn(client_main(ctx).map_err(Arc::new));
let task = smolscale::spawn(client_main(ctx.clone()).map_err(Arc::new));
Client {
task: task.shared(),

ctx,
start_time: Instant::now(),
}
}
Expand Down Expand Up @@ -79,6 +77,11 @@ impl Client {
pub fn start_time(&self) -> Instant {
self.start_time
}

/// Returns the count of all bytes used.
pub fn bytes_used(&self) -> u64 {
self.ctx.get(STAT_TOTAL_BYTES).load(Ordering::Relaxed)
}
}

pub type CtxField<T> = fn(&AnyCtx<Config>) -> T;
Expand Down
1 change: 1 addition & 0 deletions binaries/geph5-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mod client_inner;
mod database;
mod route;
mod socks5;
mod stats;
38 changes: 33 additions & 5 deletions binaries/geph5-client/src/socks5.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::client_inner::open_conn;
use crate::stats::STAT_TOTAL_BYTES;
use anyctx::AnyCtx;
use futures_util::AsyncReadExt as _;
use futures_util::AsyncWriteExt;
use futures_util::{AsyncRead, AsyncReadExt as _, AsyncWrite};
use nursery_macro::nursery;
use sillad::listener::Listener as _;
use smol::future::FutureExt as _;
use socksv5::v5::{
read_handshake, read_request, write_auth_method, write_request_status, SocksV5AuthMethod,
SocksV5Host, SocksV5RequestStatus,
};
use std::net::Ipv4Addr;
use std::{net::Ipv4Addr, sync::atomic::Ordering};

use super::Config;

Expand Down Expand Up @@ -48,12 +50,38 @@ pub async fn socks5_loop(ctx: &AnyCtx<Config>) -> anyhow::Result<()> {
.await?;
tracing::trace!(remote_addr = display(&remote_addr), "connection opened");
let (read_stream, write_stream) = stream.split();
smol::io::copy(read_stream, write_client)
.race(smol::io::copy(read_client, write_stream))
.await?;
io_copy_with(read_stream, write_client, |n| {
ctx.get(STAT_TOTAL_BYTES)
.fetch_add(n as u64, Ordering::Relaxed);
})
.race(io_copy_with(read_client, write_stream, |n| {
ctx.get(STAT_TOTAL_BYTES)
.fetch_add(n as u64, Ordering::Relaxed);
}))
.await?;
anyhow::Ok(())
})
.detach();
}
})
}

async fn io_copy_with(
mut read: impl AsyncRead + Unpin,
mut write: impl AsyncWrite + Unpin,
mut on_copy: impl FnMut(usize),
) -> anyhow::Result<()> {
let mut buffer = vec![0; 8192]; // Buffer size can be adjusted based on expected data sizes or performance testing

loop {
let bytes_read = read.read(&mut buffer).await?;

if bytes_read == 0 {
break; // End of input stream
}

write.write_all(&buffer[..bytes_read]).await?;
on_copy(bytes_read); // Invoke the callback
}
Ok(())
}
5 changes: 5 additions & 0 deletions binaries/geph5-client/src/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::sync::atomic::AtomicU64;

use crate::client::CtxField;

pub static STAT_TOTAL_BYTES: CtxField<AtomicU64> = |_| AtomicU64::new(0);

0 comments on commit 67281d3

Please sign in to comment.