Skip to content

Commit

Permalink
relay
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Aug 23, 2023
1 parent 3bd68f7 commit 4f98a27
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
8 changes: 6 additions & 2 deletions clash/tests/data/config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ proxy-groups:
- "load-balance"
- "select"
- DIRECT

- name: "relay-one"
type: relay
proxies:
- "ss"

- name: "auto"
type: url-test
Expand Down Expand Up @@ -84,7 +89,6 @@ proxy-groups:
proxies:
- "ss"
- DIRECT
- REJECT


proxies:
Expand All @@ -107,7 +111,7 @@ proxies:

rules:
- DOMAIN,ipinfo.io,relay
- DOMAIN-SUFFIX,google.com,plain-vmess
- DOMAIN-KEYWORD,httpbin,relay-one
- DOMAIN-SUFFIX,facebook.com,REJECT
- DOMAIN-KEYWORD,google,ss
- DOMAIN,google.com,ss
Expand Down
3 changes: 3 additions & 0 deletions clash_lib/src/proxy/fallback/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::io;

use tracing::debug;

use crate::{
app::{
proxy_manager::{
Expand Down Expand Up @@ -52,6 +54,7 @@ impl Handler {
let proxies = self.get_proxies(touch).await;
for proxy in proxies.iter() {
if self.proxy_manager.lock().await.alive(proxy.name()).await {
debug!("{} fastest {} is alive", self.name(), proxy.name());
return proxy.clone();
}
}
Expand Down
4 changes: 4 additions & 0 deletions clash_lib/src/proxy/loadbalance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod helpers;
use std::{io, sync::Arc};

use tokio::sync::Mutex;
use tracing::debug;

use crate::{
app::{
Expand Down Expand Up @@ -98,6 +99,7 @@ impl OutboundHandler for Handler {
) -> io::Result<AnyStream> {
let proxies = self.get_proxies(false).await;
let proxy = (self.inner.lock().await.strategy_fn)(proxies, &sess).await?;
debug!("{} use proxy {}", self.name(), proxy.name());
proxy.connect_stream(sess, resolver).await
}

Expand All @@ -110,6 +112,7 @@ impl OutboundHandler for Handler {
) -> io::Result<AnyStream> {
let proxies = self.get_proxies(false).await;
let proxy = (self.inner.lock().await.strategy_fn)(proxies, &sess).await?;
debug!("{} use proxy {}", self.name(), proxy.name());
proxy.proxy_stream(s, sess, resolver).await
}

Expand All @@ -121,6 +124,7 @@ impl OutboundHandler for Handler {
) -> io::Result<AnyOutboundDatagram> {
let proxies = self.get_proxies(false).await;
let proxy = (self.inner.lock().await.strategy_fn)(proxies, &sess).await?;
debug!("{} use proxy {}", self.name(), proxy.name());
proxy.connect_datagram(sess, resolver).await
}
}
38 changes: 31 additions & 7 deletions clash_lib/src/proxy/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ use std::{io, sync::Arc};

use async_trait::async_trait;
use futures::stream::{self, StreamExt};
use tracing::{debug, error};

use crate::{
app::{
proxy_manager::providers::proxy_provider::ThreadSafeProxyProvider, ThreadSafeDNSResolver,
},
common::errors::new_io_error,
config::internal::proxy::{OutboundGroupRelay, OutboundProxy},
proxy::utils::new_tcp_stream,
session::{Session, SocksAddr},
};

Expand Down Expand Up @@ -85,15 +87,37 @@ impl OutboundHandler for Handler {
proxy.connect_stream(sess, resolver).await
}
_ => {
let first = proxies[0].clone();
let mut first = proxies[0].clone();
let last = proxies[proxies.len() - 1].clone();

let mut s = first.connect_stream(sess, resolver.clone()).await?;
let mut sess = sess.clone();
for i in 1..proxies.len() - 1 {
let remote_addr = first.remote_addr().await.unwrap();

let mut s = new_tcp_stream(
resolver.clone(),
remote_addr.host().as_str(),
remote_addr.port(),
None,
)
.await?;

let mut next_sess = sess.clone();
for i in 1..proxies.len() {
let proxy = proxies[i].clone();
sess.destination = proxy.remote_addr().await.expect("must have remote addr");
s = proxy.proxy_stream(s, &sess, resolver.clone()).await?;
error!(
"relay {} -> {} -> {} -> {}",
first.name(),
proxy.name(),
proxy.remote_addr().await.unwrap(),
proxies.len()
);
next_sess.destination =
proxy.remote_addr().await.expect("must have remote addr");
s = first.proxy_stream(s, &next_sess, resolver.clone()).await?;

first = proxy;
}

s = last.proxy_stream(s, &sess, resolver).await?;
Ok(s)
}
}
Expand All @@ -113,6 +137,6 @@ impl OutboundHandler for Handler {
_sess: &Session,
_resolver: ThreadSafeDNSResolver,
) -> io::Result<AnyOutboundDatagram> {
todo!()
Err(new_io_error("not implemented for Relay"))
}
}
2 changes: 2 additions & 0 deletions clash_lib/src/proxy/selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{io, sync::Arc};

use async_trait::async_trait;
use tokio::sync::Mutex;
use tracing::debug;

use crate::{
app::{
Expand Down Expand Up @@ -68,6 +69,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());
return proxy;
}
}
Expand Down
10 changes: 9 additions & 1 deletion clash_lib/src/proxy/urltest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{io, sync::Arc};

use tokio::sync::Mutex;
use tracing::debug;

use crate::{
app::{
Expand Down Expand Up @@ -103,6 +104,13 @@ impl Handler {
}
}

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

return inner.fastest_proxy.as_ref().unwrap().clone();
}
}
Expand Down Expand Up @@ -153,7 +161,7 @@ impl OutboundHandler for Handler {
sess: &Session,
resolver: ThreadSafeDNSResolver,
) -> io::Result<AnyStream> {
self.fastest(false)
self.fastest(true)
.await
.proxy_stream(s, sess, resolver)
.await
Expand Down

0 comments on commit 4f98a27

Please sign in to comment.