Skip to content

Commit

Permalink
add boringtun
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Sep 18, 2023
1 parent f89493c commit 5d049b7
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 4 deletions.
156 changes: 153 additions & 3 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ filetime = "0.2"
axum = { version = "0.6.20", features = ["ws"] }
tower-http = { version = "0.4.0", features = ["fs", "trace", "cors"] }
chrono = { version = "0.4.26", features = ["serde"] }

tun = { git = "https://github.com/Watfaq/rust-tun.git", rev = "5c0702b", features = ["async"] }
netstack-lwip = { git = "https://github.com/Watfaq/netstack-lwip.git", rev = "8c8c0b0" }
boringtun = { version = "0.6.0", features = ["device"] }



serde = { version = "1.0", features=["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/app/dns/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Resolver {
async move {
c.exchange(message)
.inspect_err(|x| {
warn!("DNS client {} resolve error: {}", c.id(), x.to_string())
debug!("DNS client {} resolve error: {}", c.id(), x.to_string())
})
.await
}
Expand Down
2 changes: 2 additions & 0 deletions clash_lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod trojan;
pub mod tun;
pub mod utils;
pub mod vmess;
//pub mod wg;

pub mod converters;

Expand Down Expand Up @@ -129,6 +130,7 @@ pub enum OutboundType {
Shadowsocks,
Vmess,
Trojan,
WireGuard,

#[serde(rename = "URLTest")]
UrlTest,
Expand Down
94 changes: 94 additions & 0 deletions clash_lib/src/proxy/wg/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{
io,
net::{Ipv4Addr, Ipv6Addr},
sync::Arc,
};

use crate::{
app::{dispatcher::BoxedChainedStream, dns::ThreadSafeDNSResolver},
session::{Session, SocksAddr},
};

use super::{
AnyOutboundDatagram, AnyOutboundHandler, AnyStream, CommonOption, OutboundHandler, OutboundType,
};

use async_trait::async_trait;
pub use netstack_lwip as netstack;

pub struct Opts {
pub name: String,
pub common_opts: CommonOption,
pub server: String,
pub port: u16,
pub ip: Ipv4Addr,
pub ipv6: Option<Ipv6Addr>,
pub private_key: String,
pub public_key: String,
pub preshared_key: Option<String>,
pub remote_dns_resolve: bool,
pub dns: Option<Vec<String>>,
pub mtu: Option<u16>,
pub udp: bool,
}

pub struct Handler {
opts: Opts,

device: boringtun::device::Device,
}

impl Handler {
pub fn new(opts: Opts) -> AnyOutboundHandler {
let device_cfg = boringtun::device::DeviceConfig::default();
let device = boringtun::device::Device::new("utun", device_cfg).unwrap();
Arc::new(Self { opts, device })
}
}

#[async_trait]
impl OutboundHandler for Handler {
fn name(&self) -> &str {
&self.opts.name
}

fn proto(&self) -> OutboundType {
OutboundType::WireGuard
}

async fn remote_addr(&self) -> Option<SocksAddr> {
None
}

async fn support_udp(&self) -> bool {
self.opts.udp
}

/// connect to remote target via TCP
async fn connect_stream(
&self,
sess: &Session,
resolver: ThreadSafeDNSResolver,
) -> io::Result<BoxedChainedStream> {
todo!()
}

/// wraps a stream with outbound handler
async fn proxy_stream(
&self,
s: AnyStream,
sess: &Session,
resolver: ThreadSafeDNSResolver,
) -> io::Result<AnyStream> {
todo!()
}

/// connect to remote target via UDP
async fn connect_datagram(
&self,
sess: &Session,
resolver: ThreadSafeDNSResolver,
) -> io::Result<AnyOutboundDatagram> {
todo!()
}
}

0 comments on commit 5d049b7

Please sign in to comment.