Skip to content

Commit

Permalink
Vmess WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Aug 17, 2023
1 parent 367c540 commit d8b75bc
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 20 deletions.
10 changes: 5 additions & 5 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "f267c28d6e0cbe45608f60b117a704c18e3155b38981d0866edb7f1ea5e9fa9b",
"checksum": "3e9cefe14ba34202af1b06aa8dc813b02fe4ac0fa0a3022682061695eb3ec7e8",
"crates": {
"addr2line 0.20.0": {
"name": "addr2line",
Expand Down Expand Up @@ -2044,7 +2044,7 @@
"Git": {
"remote": "https://github.com/Watfaq/boring.git",
"commitish": {
"Branch": "bazel"
"Rev": "a5c2442"
},
"strip_prefix": "boring"
}
Expand Down Expand Up @@ -2102,7 +2102,7 @@
"Git": {
"remote": "https://github.com/Watfaq/boring.git",
"commitish": {
"Branch": "bazel"
"Rev": "a5c2442"
},
"strip_prefix": "boring-sys"
}
Expand Down Expand Up @@ -7141,7 +7141,7 @@
"Git": {
"remote": "https://github.com/Watfaq/boring.git",
"commitish": {
"Branch": "bazel"
"Rev": "a5c2442"
},
"strip_prefix": "hyper-boring"
}
Expand Down Expand Up @@ -14488,7 +14488,7 @@
"Git": {
"remote": "https://github.com/Watfaq/boring.git",
"commitish": {
"Branch": "bazel"
"Rev": "a5c2442"
},
"strip_prefix": "tokio-boring"
}
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ foreign-types-shared = "0.3.1"
network-interface = "1.0.0"
base64 = "0.21"
uuid = { version = "1.2.1", features = ["v4", "fast-rng", "macro-diagnostics"] }
boring = { git = "https://github.com/Watfaq/boring.git", branch = "bazel" }
boring-sys = { git = "https://github.com/Watfaq/boring.git", branch = "bazel" }
hyper-boring = { git = "https://github.com/Watfaq/boring.git", branch = "bazel" }
tokio-boring = { git = "https://github.com/Watfaq/boring.git", branch = "bazel" }
boring = { git = "https://github.com/Watfaq/boring.git", rev = "a5c2442" }
boring-sys = { git = "https://github.com/Watfaq/boring.git", rev = "a5c2442" }
hyper-boring = { git = "https://github.com/Watfaq/boring.git", rev = "a5c2442" }
tokio-boring = { git = "https://github.com/Watfaq/boring.git", rev = "a5c2442" }
crc32fast = "1.3.2"
brotli = "3.3.4"
hmac = "0.12.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc, time::Duration};

use async_trait::async_trait;
use futures::{future, TryFutureExt};
use futures::TryFutureExt;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -82,6 +82,7 @@ impl ProxySetProvider {
OutboundProxyProtocol::Ss(s) => s.try_into(),
OutboundProxyProtocol::Socks5(_) => todo!(),
OutboundProxyProtocol::Trojan(_) => todo!(),
OutboundProxyProtocol::Vmess(_) => todo!(),
})
.collect::<Result<Vec<_>, _>>();
Ok(proxies?)
Expand Down
20 changes: 20 additions & 0 deletions clash_lib/src/config/internal/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum OutboundProxyProtocol {
Socks5(OutboundSocks5),
#[serde(rename = "trojan")]
Trojan(OutboundTrojan),
#[serde(rename = "vmess")]
Vmess(OutboundVmess),
}

impl OutboundProxyProtocol {
Expand All @@ -60,6 +62,7 @@ impl OutboundProxyProtocol {
OutboundProxyProtocol::Ss(ss) => &ss.name,
OutboundProxyProtocol::Socks5(socks5) => &socks5.name,
OutboundProxyProtocol::Trojan(trojan) => &trojan.name,
OutboundProxyProtocol::Vmess(vmess) => &vmess.name,
}
}
}
Expand All @@ -81,6 +84,7 @@ impl Display for OutboundProxyProtocol {
OutboundProxyProtocol::Direct => write!(f, "{}", PROXY_DIRECT),
OutboundProxyProtocol::Reject => write!(f, "{}", PROXY_REJECT),
OutboundProxyProtocol::Trojan(_) => write!(f, "{}", "Trojan"),
OutboundProxyProtocol::Vmess(_) => write!(f, "{}", "Vmess"),
}
}
}
Expand Down Expand Up @@ -137,6 +141,22 @@ pub struct OutboundTrojan {
pub ws_opts: Option<WsOpt>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Default)]
pub struct OutboundVmess {
pub name: String,
pub server: String,
pub port: u16,
pub uuid: String,
pub alter_id: u16,
pub cipher: String,
pub udp: bool,
pub tls: bool,
pub skip_cert_verify: bool,
pub server_name: Option<String>,
pub network: Option<String>,
pub ws_opts: Option<WsOpt>,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
#[serde(tag = "type")]
pub enum OutboundGroupProtocol {
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod shadowsocks;
pub mod socks;
//pub mod trojan;
pub mod utils;
//pub mod vmess;
pub mod vmess;

pub mod converters;

Expand Down
1 change: 1 addition & 0 deletions clash_lib/src/proxy/shadowsocks/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl Sink<UdpPacket> for OutboundDatagramShadowsocks {
Poll::Ready(Ok(()))
}
}

impl Stream for OutboundDatagramShadowsocks {
type Item = UdpPacket;

Expand Down
39 changes: 35 additions & 4 deletions clash_lib/src/proxy/vmess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ use http::Uri;

mod vmess_impl;

use crate::{app::ThreadSafeDNSResolver, session::Session};
use crate::{
app::ThreadSafeDNSResolver,
config::internal::proxy::{OutboundProxy, OutboundProxyProtocol},
session::{Session, SocksAddr},
};

use super::{
transport::{self, Http2Config},
Expand Down Expand Up @@ -77,13 +81,29 @@ impl OutboundHandler for Handler {
&self.opts.name
}

/// The protocol of the outbound handler
/// only contains Type information, do not rely on the underlying value
fn proto(&self) -> OutboundProxy {
OutboundProxy::ProxyServer(OutboundProxyProtocol::Vmess(Default::default()))
}

/// The proxy remote address
async fn remote_addr(&self) -> Option<SocksAddr> {
Some(SocksAddr::Domain(self.opts.server.clone(), self.opts.port))
}

/// whether the outbound handler support UDP
async fn support_udp(&self) -> bool {
self.opts.udp
}

async fn connect_stream(
&self,
sess: &Session,
resolver: ThreadSafeDNSResolver,
) -> io::Result<AnyStream> {
let mut stream = new_tcp_stream(
resolver,
let stream = new_tcp_stream(
resolver.clone(),
self.opts.server.as_str(),
self.opts.port,
self.opts.common_opts.iface.as_ref(),
Expand All @@ -99,6 +119,18 @@ impl OutboundHandler for Handler {
})
.await?;

self.proxy_stream(stream, sess, resolver).await
}

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

let underlying = match self.opts.transport {
Some(VmessTransport::Ws(ref opt)) => {
let uri = format!("ws://{}:{}{}", self.opts.server, self.opts.port, opt.path)
Expand Down Expand Up @@ -169,7 +201,6 @@ impl OutboundHandler for Handler {

vmess_builder.proxy_stream(underlying).await
}

async fn connect_datagram(
&self,
sess: &Session,
Expand Down
Loading

0 comments on commit d8b75bc

Please sign in to comment.