Skip to content

Commit

Permalink
refactor(proxy): some code refactorings (#614)
Browse files Browse the repository at this point in the history
* move group folders

* some refactors

* up

* clippy
  • Loading branch information
ibigbug authored Sep 30, 2024
1 parent 257e286 commit bfd9535
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 299 deletions.
17 changes: 17 additions & 0 deletions clash/tests/data/config/tproxy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
port: 8888
socks-port: 8889
mixed-port: 8899
tproxy-port: 8900

mode: rule
log-level: debug


proxies:
- name: "tor"
type: tor

rules:
- MATCH, tor
...
6 changes: 2 additions & 4 deletions clash_lib/src/app/api/handlers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,11 @@ async fn patch_configs(

inbound_manager.rebuild_listeners(ports);

if let Some(h) = global_state.inbound_listener_handle.take() {
h.abort()
}
global_state.inbound_listener_handle.abort();

let r = inbound_manager.get_runner().unwrap();

global_state.inbound_listener_handle = Some(tokio::spawn(r));
global_state.inbound_listener_handle = tokio::spawn(r);
}

if let Some(mode) = payload.mode {
Expand Down
17 changes: 17 additions & 0 deletions clash_lib/src/app/inbound/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ impl InboundManager {
ListenerType::Mixed => {
ports.mixed_port = Some(x.port);
}
ListenerType::Tproxy => {
ports.tproxy_port = Some(x.port);
}
});

ports
Expand Down Expand Up @@ -150,6 +153,20 @@ impl InboundManager {
);
}

if let Some(tproxy_port) = ports.tproxy_port {
network_listeners.insert(
ListenerType::Tproxy,
NetworkInboundListener {
name: "TProxy".to_string(),
bind_addr: self.bind_address.clone(),
port: tproxy_port,
listener_type: ListenerType::Tproxy,
dispatcher: self.dispatcher.clone(),
authenticator: self.authenticator.clone(),
},
);
}

self.network_listeners = network_listeners;
}
}
6 changes: 5 additions & 1 deletion clash_lib/src/app/inbound/network_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
common::auth::ThreadSafeAuthenticator, config::internal::config::BindAddress,
};

use crate::proxy::{http, mixed, socks, AnyInboundListener};
use crate::proxy::{http, mixed, socks, tproxy, AnyInboundListener};

use crate::{proxy::utils::Interface, Dispatcher, Error, Runner};
use futures::FutureExt;
Expand All @@ -19,6 +19,7 @@ pub enum ListenerType {
Http,
Socks5,
Mixed,
Tproxy,
}

pub struct NetworkInboundListener {
Expand Down Expand Up @@ -122,6 +123,9 @@ impl NetworkInboundListener {
self.dispatcher.clone(),
self.authenticator.clone(),
)),
ListenerType::Tproxy => {
Arc::new(tproxy::Listener::new((ip, self.port).into()))
}
};

if listener.handle_tcp() {
Expand Down
4 changes: 2 additions & 2 deletions clash_lib/src/app/profile/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use serde::{Deserialize, Serialize};
use tracing::{error, trace};
use tracing::{error, trace, warn};

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Db {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl CacheFile {
}
},
Err(e) => {
error!("failed to read cache file: {}, initializing a new one", e);
warn!("failed to read cache file: {}, initializing a new one", e);
Db {
selected: HashMap::new(),
ip_to_host: HashMap::new(),
Expand Down
1 change: 0 additions & 1 deletion clash_lib/src/config/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ pub struct Config {
/// The redir port
#[doc(hidden)]
pub redir_port: Option<u16>,
#[doc(hidden)]
pub tproxy_port: Option<u16>,
/// The HTTP/SOCKS5 mixed proxy port
/// # Example
Expand Down
2 changes: 1 addition & 1 deletion clash_lib/src/config/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ pub struct Inbound {
pub bind_address: BindAddress,
}

#[derive(Serialize, Deserialize, Default)]
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct Controller {
pub external_controller: Option<String>,
pub external_ui: Option<String>,
Expand Down
Loading

0 comments on commit bfd9535

Please sign in to comment.