Skip to content

Commit 041eab8

Browse files
committed
chore(proxy): merge functionalities into construct_outbound
1 parent 1441267 commit 041eab8

File tree

2 files changed

+67
-61
lines changed

2 files changed

+67
-61
lines changed

boltconn/src/proxy/dispatcher.rs

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,39 @@ impl Dispatcher {
226226
Ok(ChainOutbound::new(chain_name, res))
227227
}
228228

229+
pub async fn construct_outbound(
230+
&self,
231+
src_addr: SocketAddr,
232+
dst_addr: &NetworkAddr,
233+
proxy_config: &ProxyImpl,
234+
proxy_name: &str,
235+
iface_name: &str,
236+
resolved_dst: Option<&SocketAddr>,
237+
) -> Result<(Box<dyn Outbound>, OutboundType), DispatchError> {
238+
Ok(match proxy_config {
239+
ProxyImpl::Chain(vec) => (
240+
Box::new(
241+
self.create_chain(proxy_name, vec, src_addr, dst_addr, iface_name)
242+
.map_err(|_| DispatchError::BadChain)?,
243+
),
244+
OutboundType::Chain,
245+
),
246+
ProxyImpl::BlackHole => {
247+
return Err(DispatchError::BlackHole);
248+
}
249+
_ => self
250+
.build_normal_outbound(
251+
proxy_name,
252+
iface_name,
253+
proxy_config,
254+
src_addr,
255+
dst_addr,
256+
resolved_dst,
257+
)
258+
.map_err(|_| DispatchError::Reject)?,
259+
})
260+
}
261+
229262
pub async fn submit_tcp<S: StreamOutboundTrait>(
230263
&self,
231264
inbound: InboundInfo,
@@ -251,33 +284,28 @@ impl Dispatcher {
251284
let iface_name = iface
252285
.as_ref()
253286
.map_or(self.iface_name.as_str(), |s| s.as_str());
254-
let (outbounding, proxy_type): (Box<dyn Outbound>, OutboundType) =
255-
match proxy_config.as_ref() {
256-
ProxyImpl::Chain(vec) => (
257-
Box::new(
258-
self.create_chain(&proxy_name, vec, src_addr, &dst_addr, iface_name)
259-
.map_err(|_| DispatchError::BadChain)?,
260-
),
261-
OutboundType::Chain,
262-
),
263-
ProxyImpl::BlackHole => {
264-
tokio::spawn(async move {
265-
tokio::time::sleep(Duration::from_secs(30)).await;
266-
drop(stream)
267-
});
268-
return Err(DispatchError::BlackHole);
269-
}
270-
_ => self
271-
.build_normal_outbound(
272-
&proxy_name,
273-
iface_name,
274-
proxy_config.as_ref(),
275-
src_addr,
276-
&dst_addr,
277-
conn_info.resolved_dst.as_ref(),
278-
)
279-
.map_err(|_| DispatchError::Reject)?,
280-
};
287+
let (outbounding, proxy_type): (Box<dyn Outbound>, OutboundType) = match self
288+
.construct_outbound(
289+
src_addr,
290+
&dst_addr,
291+
&proxy_config,
292+
&proxy_name,
293+
iface_name,
294+
conn_info.resolved_dst.as_ref(),
295+
)
296+
.await
297+
{
298+
Ok(r) => r,
299+
Err(DispatchError::Reject) => return Err(DispatchError::Reject),
300+
Err(DispatchError::BlackHole) => {
301+
tokio::spawn(async move {
302+
tokio::time::sleep(Duration::from_secs(30)).await;
303+
drop(stream)
304+
});
305+
return Err(DispatchError::BlackHole);
306+
}
307+
Err(e) => return Err(e),
308+
};
281309

282310
// conn info
283311
let abort_handle = ConnAbortHandle::new();

boltconn/src/proxy/mod.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ mod socks5_inbound;
99
mod tun_inbound;
1010
mod tun_udp_inbound;
1111

12-
use crate::adapter::{Connector, Outbound};
12+
use crate::adapter::Connector;
1313
use crate::common::create_tls_connector;
1414
use crate::common::duplex_chan::DuplexChan;
15-
use crate::dispatch::{Latency, Proxy, ProxyImpl};
15+
use crate::dispatch::{Latency, Proxy};
1616
use crate::proxy::error::RuntimeError;
1717
use bytes::Bytes;
1818
pub use context::*;
@@ -100,9 +100,17 @@ pub async fn latency_test(
100100
);
101101
let src_addr = get_random_local_addr(&dst_addr, rng.gen_range(32768..65535));
102102
// create outbound
103-
let creator = construct_outbound(dispatcher, &proxy, src_addr, &dst_addr, &iface)
103+
let (creator, _) = dispatcher
104+
.construct_outbound(
105+
src_addr,
106+
&dst_addr,
107+
&proxy.get_impl(),
108+
&proxy.get_name(),
109+
&iface,
110+
None,
111+
)
104112
.await
105-
.ok_or_else(|| {
113+
.map_err(|_| {
106114
proxy.set_latency(Latency::Failed);
107115
RuntimeError::LatencyTest("Create outbound failed")
108116
})?;
@@ -145,33 +153,3 @@ pub async fn latency_test(
145153
});
146154
Ok(timeout_future)
147155
}
148-
149-
async fn construct_outbound(
150-
dispatcher: &Dispatcher,
151-
proxy: &Proxy,
152-
src_addr: SocketAddr,
153-
dst_addr: &NetworkAddr,
154-
iface: &str,
155-
) -> Option<Box<dyn Outbound>> {
156-
match proxy.get_impl().as_ref() {
157-
ProxyImpl::Chain(vec) => {
158-
match dispatcher.create_chain(&proxy.get_name(), vec, src_addr, dst_addr, iface) {
159-
Ok(o) => Some(Box::new(o)),
160-
Err(_) => None,
161-
}
162-
}
163-
proxy_config => {
164-
match dispatcher.build_normal_outbound(
165-
&proxy.get_name(),
166-
iface,
167-
proxy_config,
168-
src_addr,
169-
dst_addr,
170-
None,
171-
) {
172-
Ok((o, _)) => Some(o),
173-
Err(_) => None,
174-
}
175-
}
176-
}
177-
}

0 commit comments

Comments
 (0)