Skip to content

Commit

Permalink
vpn work on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nullchinchilla committed Jun 14, 2024
1 parent 671edf8 commit 74ed194
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion binaries/geph5-client-gui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate winresource;

fn main() {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
let mut res = winresource::WindowsResource::new();
let mut res = winresource::WindowsResource::new();
res.set_icon("icon.ico");
res.compile().unwrap();
}
Expand Down
25 changes: 21 additions & 4 deletions binaries/geph5-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyctx::AnyCtx;
use anyhow::Context as _;
use clone_macro::clone;
use futures_util::{
future::Shared,
Expand Down Expand Up @@ -139,7 +140,11 @@ async fn client_main(ctx: AnyCtx<Config>) -> anyhow::Result<()> {
if ctx.init().vpn {
let vpn = VpnCapture::new(ctx.clone());
loop {
let captured = vpn.ipstack().accept().await?;
let captured = vpn
.ipstack()
.accept()
.await
.context("could not accept from ipstack")?;
match captured {
ipstack_geph::stream::IpStackStream::Tcp(captured) => {
let peer_addr = captured.peer_addr();
Expand Down Expand Up @@ -172,6 +177,11 @@ async fn client_main(ctx: AnyCtx<Config>) -> anyhow::Result<()> {
peer_addr = display(peer_addr),
"captured a UDP"
);
let peer_addr = if captured.peer_addr().port() == 53 {
"1.1.1.1:53".parse()?
} else {
peer_addr
};
let ctx = ctx.clone();
smolscale::spawn::<anyhow::Result<()>>(async move {
let tunneled = open_conn(&ctx, &format!("udp${peer_addr}")).await?;
Expand Down Expand Up @@ -222,9 +232,16 @@ async fn client_main(ctx: AnyCtx<Config>) -> anyhow::Result<()> {
)),
);
socks5_loop(&ctx)
.race(vpn_loop)
.race(run_http_proxy(&ctx))
.race(auth_loop(&ctx))
.inspect_err(|e| tracing::error!(err = debug(e), "socks5 loop stopped"))
.race(vpn_loop.inspect_err(|e| tracing::error!(err = debug(e), "vpn loop stopped")))
.race(
run_http_proxy(&ctx)
.inspect_err(|e| tracing::error!(err = debug(e), "http proxy stopped")),
)
.race(
auth_loop(&ctx)
.inspect_err(|e| tracing::error!(err = debug(e), "auth loop stopped")),
)
.await
}
}
5 changes: 4 additions & 1 deletion binaries/geph5-client/src/vpn/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ipstack_geph::{IpStack, IpStackConfig};
use once_cell::sync::Lazy;
use smol::channel::{Receiver, Sender};

use crate::Config;
use crate::{client_inner::open_conn, Config};

pub struct VpnCapture {
ipstack: IpStack,
Expand All @@ -35,13 +35,15 @@ impl VpnCapture {
}

fn up_shuffle(ctx: AnyCtx<Config>, send_captured: Sender<Bytes>) -> anyhow::Result<()> {
smol::future::block_on(open_conn(&ctx, ""))?;
let handle = windivert::PacketHandle::open("outbound and not loopback", -100)?;
loop {
let fallible = || {
let raw_pkt = handle.receive()?;
let ip_pkt = pnet_packet::ipv4::Ipv4Packet::new(&raw_pkt)
.context("cannot parse packet as IPv4")?;
if WHITELIST.contains(&IpAddr::V4(ip_pkt.get_destination())) {
tracing::debug!(ip = debug(ip_pkt.get_destination()), "windivert whitelist");
handle.inject(&raw_pkt, true)?;
anyhow::Ok(None)
} else {
Expand All @@ -60,6 +62,7 @@ fn up_shuffle(ctx: AnyCtx<Config>, send_captured: Sender<Bytes>) -> anyhow::Resu
}

fn dn_shuffle(ctx: AnyCtx<Config>, recv_injected: Receiver<Bytes>) -> anyhow::Result<()> {
smol::future::block_on(open_conn(&ctx, ""))?;
let handle = windivert::PacketHandle::open("false", -200)?;
loop {
let pkt = recv_injected.recv_blocking()?;
Expand Down

0 comments on commit 74ed194

Please sign in to comment.