Skip to content

Commit

Permalink
Update version in Cargo.toml to 4.99.7, add geph5-broker-protocol dep…
Browse files Browse the repository at this point in the history
…endency, and implement stdio VPN loop with async read/write operations
  • Loading branch information
nullchinchilla committed Sep 19, 2024
1 parent a771bb6 commit dd4d78e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
52 changes: 26 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "geph4-client"
version = "4.99.6"
version = "4.99.7"
authors = ["nullchinchilla <[email protected]>"]
edition = "2021"
description = "Geph client"
Expand All @@ -15,6 +15,7 @@ repository = "https://github.com/geph-official/geph4-client"

#geph5-client={path="../geph5/binaries/geph5-client"}
#sillad={path="../geph5/libraries/sillad"}
#geph5-broker-protocol={path="../geph5/libraries/geph5-broker-protocol"}

[lib]
name = "geph4client"
Expand Down
44 changes: 43 additions & 1 deletion src/connect/vpn.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use anyhow::Context;
use futures_util::{
io::{BufReader, BufWriter},
AsyncReadExt, AsyncWriteExt,
};
use smol::future::FutureExt;

use super::ConnectContext;
use crate::config::VpnMode;


#[cfg(unix)]
use std::os::unix::prelude::FromRawFd;

Expand All @@ -18,6 +21,9 @@ pub(super) async fn vpn_loop(ctx: ConnectContext) -> anyhow::Result<()> {
return unsafe { fd_vpn_loop(ctx, fd_num).await };
}

if ctx.opt.vpn_mode == Some(VpnMode::Stdio) {
return stdio_vpn_loop(ctx).await;
}

smol::future::pending().await
}
Expand Down Expand Up @@ -50,3 +56,39 @@ async unsafe fn fd_vpn_loop(ctx: ConnectContext, fd_num: i32) -> anyhow::Result<
};
up_loop.race(dn_loop).await
}

async fn stdio_vpn_loop(ctx: ConnectContext) -> anyhow::Result<()> {
let (stdin, stdout) = (
smol::Unblock::new(std::io::stdin()),
smol::Unblock::new(std::io::stdout()),
);
let mut stdin = BufReader::new(stdin);
let mut stdout = BufWriter::new(stdout);

// The upload task
let tunnel = ctx.tunnel.clone();
let upload_task = async {
loop {
let mut len_bytes = [0u8; 2];
stdin.read_exact(&mut len_bytes).await?;
let len = u16::from_le_bytes(len_bytes) as usize;

let mut buffer = vec![0u8; len];
stdin.read_exact(&mut buffer).await?;
tunnel.send_vpn(&buffer).await?;
}
};

// Download task
let download_task = async {
loop {
let down_pkt = ctx.tunnel.recv_vpn().await?;
let len_bytes = (down_pkt.len() as u16).to_le_bytes();
stdout.write_all(&len_bytes).await?;
stdout.write_all(&down_pkt).await?;
stdout.flush().await?;
}
};

download_task.race(upload_task).await
}

0 comments on commit dd4d78e

Please sign in to comment.