Skip to content

Commit 32895f4

Browse files
committed
Fix domain sniffing/override
1 parent f46cf41 commit 32895f4

File tree

15 files changed

+30
-43
lines changed

15 files changed

+30
-43
lines changed

leaf/src/app/dispatcher.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,23 @@ impl Dispatcher {
209209
match kind {
210210
sniff::SniffKind::Tls => {
211211
if do_tls {
212-
sess.tls_sniffed_domain = Some(domain);
212+
sess.tls_sniffed_domain = Some(domain.clone());
213213
}
214214
}
215215
sniff::SniffKind::Http => {
216216
if do_http {
217-
sess.http_sniffed_domain = Some(domain);
217+
sess.http_sniffed_domain = Some(domain.clone());
218218
}
219219
}
220220
}
221+
222+
if option::DOMAIN_OVERRIDE.load(std::sync::atomic::Ordering::Relaxed) {
223+
if let Ok(dest) = SocksAddr::try_from((domain, sess.destination.port()))
224+
{
225+
debug!("override destination with sniffed domain={}", dest);
226+
sess.destination = dest;
227+
}
228+
}
221229
}
222230
}
223231
Err(e) => {

leaf/src/app/router.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,7 @@ impl Router {
564564
}
565565

566566
pub async fn pick_route<'a>(&'a self, sess: &'a Session) -> Result<Option<&'a String>> {
567-
let effective_dest = sess
568-
.effective_destination()
569-
.unwrap_or_else(|_| std::borrow::Cow::Borrowed(&sess.destination));
567+
let effective_dest = &sess.destination;
570568
for rule in &self.rules {
571569
if rule.apply(sess) {
572570
return Ok(Some(&rule.target));

leaf/src/option/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ lazy_static! {
124124
AtomicBool::new(v)
125125
};
126126

127+
/// Override the original destination with the sniffed domain.
128+
pub static ref DOMAIN_OVERRIDE: AtomicBool = {
129+
let v: bool = get_env_var_or("DOMAIN_OVERRIDE", false);
130+
AtomicBool::new(v)
131+
};
132+
127133
/// Turn on DNS sniffing, if the destination is an IP, we try to find the
128134
/// domain from the DNS cache.
129135
pub static ref DNS_DOMAIN_SNIFFING: AtomicBool = {

leaf/src/proxy/failover/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl OutboundStreamHandler for Handler {
148148

149149
if let Some(cache) = &self.cache {
150150
// Try the cached actor first if exists.
151-
let cache_key = sess.effective_destination()?.to_string();
151+
let cache_key = sess.destination.to_string();
152152
if let Some(idx) = cache.lock().await.get(&cache_key) {
153153
let a = &self.actors[*idx];
154154
debug!(

leaf/src/proxy/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub async fn connect_stream_outbound(
403403
Ok(Some(new_tcp_stream(dns_client, &addr, &port).await?))
404404
}
405405
OutboundConnect::Direct => {
406-
let dest = sess.effective_destination()?;
406+
let dest = &sess.destination;
407407
trace!("connect stream direct dst={}", &dest);
408408
Ok(Some(
409409
new_tcp_stream(dns_client, &dest.host(), &dest.port()).await?,
@@ -437,7 +437,7 @@ pub async fn connect_datagram_outbound(
437437
Ok(Some(OutboundTransport::Stream(stream)))
438438
}
439439
},
440-
OutboundConnect::Direct => match sess.effective_destination()?.as_ref() {
440+
OutboundConnect::Direct => match &sess.destination {
441441
SocksAddr::Domain(domain, port) => {
442442
let socket = new_udp_socket(&crate::option::UNSPECIFIED_BIND_ADDR).await?;
443443
Ok(Some(OutboundTransport::Datagram(Box::new(

leaf/src/proxy/shadowsocks/outbound/datagram.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl OutboundDatagramHandler for Handler {
4242

4343
let dgram = ShadowedDatagram::new(&self.cipher, &self.password)?;
4444

45-
let destination = match sess.effective_destination()?.as_ref() {
45+
let destination = match &sess.destination {
4646
SocksAddr::Domain(domain, port) => Some(SocksAddr::Domain(domain.to_owned(), *port)),
4747
_ => None,
4848
};

leaf/src/proxy/shadowsocks/outbound/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl OutboundStreamHandler for Handler {
6161
self.prefix.as_ref().cloned(),
6262
)?;
6363
let mut buf = BytesMut::new();
64-
sess.effective_destination()?
64+
sess.destination
6565
.write_buf(&mut buf, SocksAddrWireType::PortLast);
6666

6767
let payload = peek_tcp_one_off(lhs).await;

leaf/src/proxy/socks/outbound/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl OutboundStreamHandler for Handler {
3434
password: auth_password.to_owned(),
3535
}),
3636
};
37-
match sess.effective_destination()?.as_ref() {
37+
match &sess.destination {
3838
SocksAddr::Ip(a) => {
3939
let _ = async_socks5::connect(&mut stream, a.to_owned(), auth)
4040
.map_err(io::Error::other)

leaf/src/proxy/tls/outbound/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl OutboundStreamHandler for Handler {
206206
let name = if !&self.server_name.is_empty() {
207207
self.server_name.clone()
208208
} else {
209-
sess.effective_destination()?.host()
209+
sess.destination.host()
210210
};
211211
if let Some(stream) = stream {
212212
#[cfg(feature = "rustls-tls")]

leaf/src/proxy/trojan/outbound/datagram.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ impl OutboundDatagramHandler for Handler {
4242
buf.put_slice(password.as_bytes());
4343
buf.put_slice(b"\r\n");
4444
buf.put_u8(0x03); // udp
45-
sess.effective_destination()?
45+
sess.destination
4646
.write_buf(&mut buf, SocksAddrWireType::PortLast);
4747
buf.put_slice(b"\r\n");
4848

49-
let destination = match sess.effective_destination()?.as_ref() {
49+
let destination = match &sess.destination {
5050
SocksAddr::Domain(domain, port) => Some(SocksAddr::Domain(domain.to_owned(), *port)),
5151
_ => None,
5252
};

0 commit comments

Comments
 (0)