Skip to content

Commit

Permalink
fix(shadowsocks-service): Server context made independent
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Nov 27, 2024
1 parent 4c4e3bd commit 84ccac6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ impl ServerIdent {
max_server_rtt: Duration,
check_window: Duration,
) -> ServerIdent {
#[allow(unused_mut)]
let mut connect_opts = context.connect_opts_ref().clone();

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down
29 changes: 13 additions & 16 deletions crates/shadowsocks-service/src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{context::ServiceContext, tcprelay::TcpServer, udprelay::UdpServer};

/// Shadowsocks Server Builder
pub struct ServerBuilder {
context: Arc<ServiceContext>,
context: ServiceContext,
svr_cfg: ServerConfig,
udp_expiry_duration: Option<Duration>,
udp_capacity: Option<usize>,
Expand All @@ -35,11 +35,11 @@ pub struct ServerBuilder {
impl ServerBuilder {
/// Create a new server builder from configuration
pub fn new(svr_cfg: ServerConfig) -> ServerBuilder {
ServerBuilder::with_context(Arc::new(ServiceContext::new()), svr_cfg)
ServerBuilder::with_context(ServiceContext::new(), svr_cfg)
}

/// Create a new server builder with context
pub fn with_context(context: Arc<ServiceContext>, svr_cfg: ServerConfig) -> ServerBuilder {
fn with_context(context: ServiceContext, svr_cfg: ServerConfig) -> ServerBuilder {
ServerBuilder {
context,
svr_cfg,
Expand All @@ -62,8 +62,7 @@ impl ServerBuilder {

/// Set `ConnectOpts`
pub fn set_connect_opts(&mut self, opts: ConnectOpts) {
let context = Arc::get_mut(&mut self.context).expect("cannot set ConnectOpts on a shared context");
context.set_connect_opts(opts)
self.context.set_connect_opts(opts)
}

/// Set UDP association's expiry duration
Expand All @@ -88,14 +87,12 @@ impl ServerBuilder {

/// Set customized DNS resolver
pub fn set_dns_resolver(&mut self, resolver: Arc<DnsResolver>) {
let context = Arc::get_mut(&mut self.context).expect("cannot set DNS resolver on a shared context");
context.set_dns_resolver(resolver)
self.context.set_dns_resolver(resolver)
}

/// Set access control list
pub fn set_acl(&mut self, acl: Arc<AccessControl>) {
let context = Arc::get_mut(&mut self.context).expect("cannot set ACL on a shared context");
context.set_acl(acl);
self.context.set_acl(acl);
}

/// Set `AcceptOpts` for accepting new connections
Expand All @@ -105,14 +102,12 @@ impl ServerBuilder {

/// Try to connect IPv6 addresses first if hostname could be resolved to both IPv4 and IPv6
pub fn set_ipv6_first(&mut self, ipv6_first: bool) {
let context = Arc::get_mut(&mut self.context).expect("cannot set ipv6_first on a shared context");
context.set_ipv6_first(ipv6_first);
self.context.set_ipv6_first(ipv6_first);
}

/// Set security config
pub fn set_security_config(&mut self, security: &SecurityConfig) {
let context = Arc::get_mut(&mut self.context).expect("cannot set security on a shared context");
context.set_security_config(security)
self.context.set_security_config(security)
}

/// Start the server
Expand All @@ -121,6 +116,8 @@ impl ServerBuilder {
/// 2. Starts TCP server (listener)
/// 3. Starts UDP server (listener)
pub async fn build(mut self) -> io::Result<Server> {
let context = Arc::new(self.context);

let mut plugin = None;

if let Some(plugin_cfg) = self.svr_cfg.plugin() {
Expand All @@ -131,14 +128,14 @@ impl ServerBuilder {

let mut tcp_server = None;
if self.svr_cfg.mode().enable_tcp() {
let server = TcpServer::new(self.context.clone(), self.svr_cfg.clone(), self.accept_opts.clone()).await?;
let server = TcpServer::new(context.clone(), self.svr_cfg.clone(), self.accept_opts.clone()).await?;
tcp_server = Some(server);
}

let mut udp_server = None;
if self.svr_cfg.mode().enable_udp() {
let server = UdpServer::new(
self.context.clone(),
context.clone(),
self.svr_cfg.clone(),
self.udp_expiry_duration,
self.udp_capacity,
Expand All @@ -149,7 +146,7 @@ impl ServerBuilder {
}

Ok(Server {
context: self.context,
context,
svr_cfg: self.svr_cfg,
tcp_server,
udp_server,
Expand Down

0 comments on commit 84ccac6

Please sign in to comment.