Skip to content

Commit

Permalink
fix vmess host
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Aug 27, 2023
1 parent d154e54 commit 1da074a
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ venv/

# don't check in this real config
ignore.yaml
cache.db
Binary file removed clash/tests/data/config/cache.db
Binary file not shown.
12 changes: 8 additions & 4 deletions clash/tests/data/config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,17 @@ proxies:
Host: www.amazon.com
- name: vmess-altid
type: vmess
server: 10.0.0.13
port: 16823
uuid: b831381d-6324-4d53-ad4f-8cda48b30811
server: tw-1.ac.laowanxiang.com
port: 153
uuid: 46dd0dd3-2cc0-3f55-907c-d94e54877687
alterId: 64
cipher: auto
udp: true
skip-cert-verify: true
network: ws
ws-opts:
path: /api/v3/download.getFile
headers:
Host: 5607b9d187e655736f563fee87d7283994721.laowanxiang.com

rules:
- DOMAIN,ipinfo.io,relay
Expand Down
11 changes: 10 additions & 1 deletion clash_lib/src/app/dns/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Resolver {
) -> anyhow::Result<Vec<net::IpAddr>> {
let mut m = op::Message::new();
let mut q = op::Query::new();
let name = rr::Name::from_str(host)
let name = rr::Name::from_str_relaxed(host)
.map_err(|_x| anyhow!("invalid domain: {}", host))?
.append_domain(&rr::Name::root())?; // makes it FQDN
q.set_name(name);
Expand Down Expand Up @@ -429,6 +429,15 @@ mod tests {
use trust_dns_client::op;
use trust_dns_proto::rr;

#[test]
fn test_bad_labels() {
let name = rr::Name::from_str_relaxed("some_domain.understore")
.unwrap()
.append_domain(&rr::Name::root())
.unwrap();
assert_eq!(name.to_string(), "some_domain.understore");
}

#[tokio::test]
async fn test_udp_resolve() {
let c = DnsClient::new(Opts {
Expand Down
7 changes: 7 additions & 0 deletions clash_lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ mod transport;
#[cfg(test)]
pub mod mocks;

#[macro_export]
macro_rules! p_debug {
($($arg:tt)*) => {
debug!(target: "proxy", $($arg)*)
};
}

#[derive(thiserror::Error, Debug)]
pub enum ProxyError {
#[error(transparent)]
Expand Down
3 changes: 2 additions & 1 deletion clash_lib/src/proxy/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
app::{
proxy_manager::providers::proxy_provider::ThreadSafeProxyProvider, ThreadSafeDNSResolver,
},
p_debug,
session::{Session, SocksAddr},
Error,
};
Expand Down Expand Up @@ -69,7 +70,7 @@ impl Handler {
let proxies = get_proxies_from_providers(&self.providers, touch).await;
for proxy in proxies {
if proxy.name() == self.inner.lock().await.current {
debug!("{} selected {}", self.name(), proxy.name());
p_debug!("{} selected {}", self.name(), proxy.name());
return proxy;
}
}
Expand Down
26 changes: 12 additions & 14 deletions clash_lib/src/proxy/transport/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ macro_rules! ws_debug {
}

pub struct WebsocketStreamBuilder {
uri: Uri,
server: String,
port: u16,
path: String,
headers: HashMap<String, String>,
ws_config: Option<WebSocketConfig>,
max_early_data: usize,
Expand All @@ -31,14 +33,18 @@ pub struct WebsocketStreamBuilder {

impl WebsocketStreamBuilder {
pub fn new(
uri: Uri,
server: String,
port: u16,
path: String,
headers: HashMap<String, String>,
ws_config: Option<WebSocketConfig>,
max_early_data: usize,
early_data_header_name: String,
) -> Self {
Self {
uri,
server,
port,
path,
headers,
ws_config,
max_early_data,
Expand All @@ -47,27 +53,19 @@ impl WebsocketStreamBuilder {
}

fn req(&self) -> Request<()> {
let authority = self.uri.authority().unwrap().as_str();
let host = authority
.find('@')
.map(|idx| authority.split_at(idx + 1).1)
.unwrap_or_else(|| authority);
let mut request = Request::builder()
.method("GET")
.header("Host", host)
.header("Connection", "Upgrade")
.header("Upgrade", "websocket")
.header("Sec-WebSocket-Version", "13")
.header("Sec-WebSocket-Key", generate_key())
.uri(self.uri.clone());
.uri(format!("ws://{}:{}{}", self.server, self.port, self.path));
for (k, v) in self.headers.iter() {
if k != "Host" {
request = request.header(k.as_str(), v.as_str());
}
request = request.header(k.as_str(), v.as_str());
}
if self.max_early_data > 0 {
// we will replace this field later
request = request.header(self.early_data_header_name.as_str(), "s");
request = request.header(self.early_data_header_name.as_str(), "xxoo");
}
request.body(()).unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/proxy/transport/websocket/websocket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt::Debug, pin::Pin, task::Poll};

use bytes::{Buf, Bytes, BytesMut};
use bytes::BytesMut;
use futures::{ready, Sink, Stream};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_tungstenite::{tungstenite::Message, WebSocketStream};
Expand Down
9 changes: 7 additions & 2 deletions clash_lib/src/proxy/urltest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
},
ThreadSafeDNSResolver,
},
p_debug,
session::{Session, SocksAddr},
};

Expand Down Expand Up @@ -104,14 +105,18 @@ impl Handler {
}
}

debug!(
p_debug!(
"{} fastest {} is {}",
self.name(),
fastest.name(),
fastest_delay
);

return inner.fastest_proxy.as_ref().unwrap().clone();
return inner
.fastest_proxy
.as_ref()
.unwrap_or(proxies.first().unwrap())
.clone();
}
}

Expand Down
7 changes: 3 additions & 4 deletions clash_lib/src/proxy/vmess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ impl Handler {

let underlying = match self.opts.transport {
Some(VmessTransport::Ws(ref opt)) => {
let uri = format!("ws://{}:{}{}", self.opts.server, self.opts.port, opt.path)
.parse::<Uri>()
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
let ws_builder = transport::WebsocketStreamBuilder::new(
uri,
self.opts.server.clone(),
self.opts.port,
opt.path.clone(),
opt.headers.clone(),
None,
opt.max_early_data,
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/proxy/vmess/vmess_impl/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ where

mbuf.put_slice(data.as_slice());
let out = mbuf.freeze();
vmess_debug!("send non aead handshake request for user{}", id.uuid);
vmess_debug!("send non aead handshake request for user {}", id.uuid);
stream.write_all(&out).await?;
} else {
let out = header::seal_vmess_aead_header(id.cmd_key, buf.freeze().to_vec(), now)
Expand Down

0 comments on commit 1da074a

Please sign in to comment.