shadowsocks_service/server/
context.rs

1//! Shadowsocks Local Server Context
2
3use std::{net::SocketAddr, sync::Arc};
4
5use shadowsocks::{
6    config::ServerType,
7    context::{Context, SharedContext},
8    dns_resolver::DnsResolver,
9    net::ConnectOpts,
10    relay::Address,
11};
12
13use crate::{acl::AccessControl, config::SecurityConfig, net::FlowStat};
14
15/// Server Service Context
16#[derive(Clone)]
17pub struct ServiceContext {
18    context: SharedContext,
19    connect_opts: ConnectOpts,
20
21    // Access Control
22    acl: Option<Arc<AccessControl>>,
23
24    // Flow statistic report
25    flow_stat: Arc<FlowStat>,
26}
27
28impl Default for ServiceContext {
29    fn default() -> Self {
30        Self {
31            context: Context::new_shared(ServerType::Server),
32            connect_opts: ConnectOpts::default(),
33            acl: None,
34            flow_stat: Arc::new(FlowStat::new()),
35        }
36    }
37}
38
39impl ServiceContext {
40    /// Create a new `ServiceContext`
41    pub fn new() -> Self {
42        Self::default()
43    }
44
45    /// Get cloned `shadowsocks` Context
46    pub fn context(&self) -> SharedContext {
47        self.context.clone()
48    }
49
50    /// Get `shadowsocks` Context reference
51    pub fn context_ref(&self) -> &Context {
52        self.context.as_ref()
53    }
54
55    /// Set `ConnectOpts`
56    pub fn set_connect_opts(&mut self, connect_opts: ConnectOpts) {
57        self.connect_opts = connect_opts;
58    }
59
60    /// Get `ConnectOpts` reference
61    pub fn connect_opts_ref(&self) -> &ConnectOpts {
62        &self.connect_opts
63    }
64
65    /// Set Access Control List
66    pub fn set_acl(&mut self, acl: Arc<AccessControl>) {
67        self.acl = Some(acl);
68    }
69
70    /// Get Access Control List reference
71    pub fn acl(&self) -> Option<&AccessControl> {
72        self.acl.as_deref()
73    }
74
75    /// Get cloned flow statistic
76    pub fn flow_stat(&self) -> Arc<FlowStat> {
77        self.flow_stat.clone()
78    }
79
80    /// Get flow statistic reference
81    pub fn flow_stat_ref(&self) -> &FlowStat {
82        self.flow_stat.as_ref()
83    }
84
85    /// Set customized DNS resolver
86    pub fn set_dns_resolver(&mut self, resolver: Arc<DnsResolver>) {
87        let context = Arc::get_mut(&mut self.context).expect("cannot set DNS resolver on a shared context");
88        context.set_dns_resolver(resolver)
89    }
90
91    /// Get reference of DNS resolver
92    pub fn dns_resolver(&self) -> &DnsResolver {
93        self.context.dns_resolver()
94    }
95
96    /// Check if target should be bypassed
97    pub async fn check_outbound_blocked(&self, addr: &Address) -> bool {
98        match self.acl {
99            None => false,
100            Some(ref acl) => acl.check_outbound_blocked(&self.context, addr).await,
101        }
102    }
103
104    /// Check if client should be blocked
105    pub fn check_client_blocked(&self, addr: &SocketAddr) -> bool {
106        match self.acl {
107            None => false,
108            Some(ref acl) => acl.check_client_blocked(addr),
109        }
110    }
111
112    /// Try to connect IPv6 addresses first if hostname could be resolved to both IPv4 and IPv6
113    pub fn set_ipv6_first(&mut self, ipv6_first: bool) {
114        let context = Arc::get_mut(&mut self.context).expect("cannot set ipv6_first on a shared context");
115        context.set_ipv6_first(ipv6_first);
116    }
117
118    /// Set security config
119    pub fn set_security_config(&mut self, security: &SecurityConfig) {
120        let context = Arc::get_mut(&mut self.context).expect("cannot set security on a shared context");
121        context.set_replay_attack_policy(security.replay_attack.policy);
122    }
123}