1use std::sync::
20{
21 OnceLock,
22 atomic::{ AtomicBool, Ordering },
23};
24
25#[cfg(feature = "client")]
26use std::
27{
28 collections::HashSet,
29 sync::
30 {
31 Arc,
32 Mutex,
33 RwLock,
34 LazyLock,
35 atomic::AtomicUsize,
36 },
37};
38
39#[cfg(feature = "client")]
40use crate::consts::SharedKeys;
41
42static SERVER_USERNAME: OnceLock<String> = OnceLock::new();
44
45#[cfg(feature = "server")]
46static VOICE_CHAT: AtomicBool = AtomicBool::new(false);
47
48#[cfg(feature = "client")]
49static KEYS: LazyLock<RwLock<Option<SharedKeys>>> = LazyLock::new(|| {
51 RwLock::new(None)
52});
53
54#[cfg(feature = "client")]
55static ASKING_PASSWORD: AtomicBool = AtomicBool::new(false); #[cfg(feature = "client")]
58static EXTRA_SPACE: AtomicBool = AtomicBool::new(false); #[cfg(feature = "client")]
61static SENDING_MESSAGES: AtomicBool = AtomicBool::new(false); #[cfg(feature = "client")]
64pub static INPUT_READ: LazyLock<Arc<Mutex<Vec<char>>>> = LazyLock::new(|| {
66 Arc::new(Mutex::new(Vec::new()))
67});
68
69#[cfg(feature = "client")]
70static SEQ: AtomicUsize = AtomicUsize::new(0); #[cfg(feature = "client")]
73static SERVER_SEQ: AtomicUsize = AtomicUsize::new(0); #[cfg(feature = "client")]
76static SERVER_ADDRESS: OnceLock<String> = OnceLock::new();
77
78#[cfg(feature = "client")]
79static OBFUSCATION_KEY: OnceLock<[u8; 32]> = OnceLock::new();
80
81#[cfg(feature = "client")]
82static CHANNEL: LazyLock<RwLock<String>> = LazyLock::new(|| RwLock::new(String::new())); #[cfg(feature = "client")]
85static MUTE: AtomicBool = AtomicBool::new(false); #[cfg(feature = "client")]
87static MUTED: LazyLock<Mutex<HashSet<usize>>> = LazyLock::new(|| Mutex::new(HashSet::new())); #[cfg(feature = "client")]
90static SOCKS5: AtomicBool = AtomicBool::new(false); pub fn get_server_username() -> String {
96 SERVER_USERNAME.get().unwrap().to_owned()
97}
98
99pub fn set_server_username(username: &str) {
101 SERVER_USERNAME.set(username.to_owned()).unwrap();
102}
103
104#[cfg(feature = "server")]
106pub fn enable_voice_chat() {
108 VOICE_CHAT.store(true, Ordering::Relaxed);
109}
110
111#[cfg(feature = "server")]
112pub fn voice_chat_enabled() -> bool {
114 VOICE_CHAT.load(Ordering::Relaxed)
115}
116
117#[cfg(feature = "client")]
119pub fn set_keys(keys: SharedKeys) {
121 let mut shared_key = KEYS.write().unwrap();
122 *shared_key = Some(keys);
123}
124
125#[cfg(feature = "client")]
126pub fn get_keys() -> Option<SharedKeys> {
128 let shared_key = KEYS.read().unwrap();
129 shared_key.clone()
130}
131
132#[cfg(feature = "client")]
134pub fn set_asking_password(value: bool) {
136 ASKING_PASSWORD.store(value, Ordering::Relaxed);
137}
138
139#[cfg(feature = "client")]
140pub fn get_asking_password() -> bool {
142 ASKING_PASSWORD.load(Ordering::Relaxed)
143}
144
145#[cfg(feature = "client")]
147pub fn set_extra_space(value: bool) {
149 EXTRA_SPACE.store(value, Ordering::Relaxed);
150}
151
152#[cfg(feature = "client")]
153pub fn get_extra_space() -> bool {
155 EXTRA_SPACE.load(Ordering::Relaxed)
156}
157
158#[cfg(feature = "client")]
160pub fn get_sending_messages() -> bool {
162 SENDING_MESSAGES.load(Ordering::Relaxed)
163}
164
165#[cfg(feature = "client")]
166pub fn set_sending_messages(value: bool) {
168 SENDING_MESSAGES.store(value, Ordering::Relaxed);
169}
170
171#[cfg(feature = "client")]
172pub fn get_seq() -> usize {
174 SEQ.load(Ordering::Relaxed)
175}
176
177#[cfg(feature = "client")]
178pub fn set_seq(value: usize) {
180 SEQ.store(value, Ordering::Relaxed)
181}
182
183#[cfg(feature = "client")]
184pub fn get_server_seq() -> usize {
186 SERVER_SEQ.load(Ordering::Relaxed)
187}
188
189#[cfg(feature = "client")]
190pub fn set_server_seq(value: usize) {
192 SERVER_SEQ.store(value, Ordering::Relaxed)
193}
194
195#[cfg(feature = "client")]
197pub fn get_server_address() -> String {
199 SERVER_ADDRESS.get().unwrap().to_owned()
200}
201
202#[cfg(feature = "client")]
203pub fn set_server_address(address: &str) {
205 SERVER_ADDRESS.set(address.to_owned()).unwrap();
206}
207
208#[cfg(feature = "client")]
210pub fn get_obfuscation_key() -> [u8; 32] {
212 *OBFUSCATION_KEY.get().unwrap()
213}
214
215#[cfg(feature = "client")]
216pub fn set_obfuscation_key(key: &[u8; 32]) {
218 OBFUSCATION_KEY.set(*key).unwrap();
219}
220
221#[cfg(feature = "client")]
223pub fn get_channel() -> String {
225 CHANNEL.read().unwrap().clone()
226}
227
228#[cfg(feature = "client")]
229pub fn set_channel(channel: String) {
231 *CHANNEL.write().unwrap() = channel;
232}
233
234#[cfg(feature = "client")]
236pub fn toggle_mute(id: Option<usize>) -> bool {
238 if let Some(id) = id {
240 let mut muted = MUTED.lock().unwrap();
241
242 if muted.remove(&id)
243 {
244 false
245 } else
246 {
247 muted.insert(id);
248 true
249 }
250
251 } else {
253 !MUTE.fetch_xor(true, Ordering::Relaxed)
254 }
255}
256
257#[cfg(feature = "client")]
258pub fn is_muted(id: Option<usize>) -> bool {
260 if let Some(id) = id
261 {
262 MUTED.lock().unwrap().contains(&id)
263 } else
264 {
265 MUTE.load(Ordering::Relaxed)
266 }
267}
268
269#[cfg(feature = "client")]
271pub fn enable_socks5() {
273 SOCKS5.store(true, Ordering::Relaxed);
274}
275
276#[cfg(feature = "client")]
277pub fn socks5_enabled() -> bool {
279 SOCKS5.load(Ordering::Relaxed)
280}