infrarust 1.2.0

A Rust universal Minecraft proxy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use std::{
    collections::{HashMap, HashSet},
    fs,
    io::{self, Read},
    path::{Path, PathBuf},
    sync::{
        RwLock,
        atomic::{AtomicUsize, Ordering},
    },
    thread::sleep,
    time::{Duration, Instant},
};

use lazy_static::lazy_static;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, de::DeserializeOwned};
use tokio::sync::mpsc::{self, Sender, channel};
use tracing::{Instrument, debug, debug_span, error, info, instrument, warn};
use walkdir::WalkDir;

use crate::{
    InfrarustConfig,
    core::{config::ServerConfig, event::ProviderMessage},
};

use super::Provider;

lazy_static! {
    static ref WATCHED_PATHS: RwLock<HashSet<String>> = RwLock::new(HashSet::new());
    static ref WATCHER_COUNT: AtomicUsize = AtomicUsize::new(0);
}

#[derive(Debug, Clone, Copy, Deserialize, Default)]
pub enum FileType {
    // serde default value
    #[serde(rename = "yaml")]
    #[default]
    Yaml,
}

// Configuration structure pour FileProvider
#[derive(Debug, Deserialize, Clone)]
pub struct FileProviderConfig {
    #[serde(default)]
    pub proxies_path: Vec<String>,
    #[serde(default)]
    pub file_type: FileType,
    #[serde(default)]
    pub watch: bool,
}

pub struct FileProvider {
    pub paths: Vec<String>,
    pub file_type: FileType,
    pub watch: bool,
    sender: Sender<ProviderMessage>,
    instance_id: usize,
}

#[derive(Debug)]
struct FileEvent {
    path: PathBuf,
    kind: notify::EventKind,
}

impl FileProvider {
    pub fn try_load_config(
        path: Option<&str>,
    ) -> Result<InfrarustConfig, Box<dyn std::error::Error>> {
        // try to read the file
        let mut file = fs::File::open(path.unwrap_or("config.yaml"))?;
        let mut content = String::new();

        // read the file
        file.read_to_string(&mut content)?;

        // decode the file
        let mut default_config = InfrarustConfig::default();
        let config: InfrarustConfig = serde_yaml::from_str(&content)?;
        default_config.merge(config);

        Ok(default_config)
    }

    #[instrument(skip(sender), fields(paths = ?paths, file_type = ?file_type, watch = watch), name = "file_provider: new")]
    pub fn new(
        paths: Vec<String>,
        file_type: FileType,
        watch: bool,
        sender: Sender<ProviderMessage>,
    ) -> Self {
        let mut unique_paths = Vec::new();
        for path in paths {
            if !unique_paths.contains(&path) {
                unique_paths.push(path);
            }
        }

        let instance_id = WATCHER_COUNT.fetch_add(1, Ordering::SeqCst);
        debug!("Initialized file provider instance {}", instance_id);

        Self {
            paths: unique_paths,
            file_type,
            watch,
            sender,
            instance_id,
        }
    }

    fn generate_config_id(path: &Path, provider_name: &str) -> String {
        format!(
            "{}@{}",
            path.file_name()
                .unwrap_or(path.as_os_str())
                .to_string_lossy(),
            provider_name
        )
    }

    async fn handle_config_update(
        path: &Path,
        file_type: FileType,
        provider_name: &str,
        sender: &Sender<ProviderMessage>,
    ) -> io::Result<()> {
        let span = debug_span!("file_provider: handle_config_update", ?path);
        async {
            if let Ok(config) = Self::load_server_config(path, file_type, provider_name.to_string())
            {
                let span =
                    debug_span!("file_provider: send_update", key = %config.config_id.clone());
                let message = ProviderMessage::Update {
                    span: span.clone(),
                    key: config.config_id.clone(),
                    configuration: if config.is_empty() {
                        None
                    } else {
                        Some(Box::new(config))
                    },
                };
                if let Err(e) = sender.send(message).instrument(span).await {
                    error!("Failed to send update message: {}", e);
                }
            }
            Ok(())
        }
        .instrument(span)
        .await
    }

    #[instrument(skip(self), fields(path = %path), name = "file_provider: load_configs")]
    fn load_configs(&self, path: &str) -> io::Result<HashMap<String, ServerConfig>> {
        debug!("Loading configurations from directory");
        let proxies_path = fs::canonicalize(path)?;
        let mut configs = HashMap::new();

        for entry in WalkDir::new(&proxies_path)
            .follow_links(true)
            .into_iter()
            .filter_map(Result::ok)
            .filter(|e| e.file_type().is_file())
        {
            if let Ok(config) =
                Self::load_server_config(entry.path(), self.file_type, self.get_name())
            {
                if !config.is_empty() {
                    configs.insert(config.config_id.clone(), config);
                }
            }
        }

        Ok(configs)
    }

    #[instrument(skip(path, file_type), fields(path = %path.as_ref().display()), name = "file_provider: load_server_config")]
    fn load_server_config<P: AsRef<Path>>(
        path: P,
        file_type: FileType,
        provider_name: String,
    ) -> io::Result<ServerConfig> {
        debug!("Loading server configuration from file");
        let path = path.as_ref();
        let metadata = fs::metadata(path)?;

        if metadata.len() == 0 {
            debug!("File is empty: {:?}", path);
            return Err(io::Error::new(io::ErrorKind::InvalidData, "File is empty"));
        }

        let mut content = String::new();
        fs::File::open(path)?.read_to_string(&mut content)?;

        let mut config: ServerConfig = match file_type {
            FileType::Yaml => {
                yaml_decoder(&content).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
            }
        };

        config.config_id = Self::generate_config_id(path, &provider_name);
        Ok(config)
    }

    #[instrument(skip(self), name = "file_provider: setup_file_watcher")]
    async fn setup_file_watcher(&self) -> io::Result<RecommendedWatcher> {
        let span = debug_span!("setup_watcher", instance_id = self.instance_id);
        async {
            debug!("Setting up file watcher for instance {}", self.instance_id);

            let (file_event_tx, mut file_event_rx) = channel::<FileEvent>(100);

            let debounce_duration = Duration::from_millis(1000);

            let file_type = self.file_type;
            let provider_name = self.get_name();
            let provider_sender = self.sender.clone();

            tokio::spawn(async move {
                let mut last_paths: HashMap<PathBuf, Instant> = HashMap::new();
                let mut last_processed: HashMap<String, Instant> = HashMap::new();

                while let Some(event) = file_event_rx.recv().await {
                    Self::handle_file_event(
                        event,
                        &mut last_paths,
                        &mut last_processed,
                        debounce_duration,
                        file_type,
                        &provider_name,
                        &provider_sender,
                    )
                    .await;
                }
            });

            self.create_watcher(file_event_tx)
        }
        .instrument(span)
        .await
    }

    async fn handle_file_event(
        event: FileEvent,
        last_paths: &mut HashMap<PathBuf, Instant>,
        last_processed: &mut HashMap<String, Instant>,
        debounce_duration: Duration,
        file_type: FileType,
        provider_name: &str,
        provider_sender: &Sender<ProviderMessage>,
    ) {
        let root_span = debug_span!(
            "file_provider: file_change",
            path = ?event.path,
            event_kind = ?event.kind,
            provider = %provider_name
        );

        let span_clone = root_span.clone();
        async {
            let config_id = Self::generate_config_id(&event.path, provider_name);

            if notify::EventKind::is_remove(&event.kind) {
                debug!("File removed: {}", event.path.display());
                let message = ProviderMessage::Update {
                    span: span_clone,
                    key: config_id,
                    configuration: None,
                };
                if let Err(e) = provider_sender.send(message).await {
                    error!("Failed to send update message: {}", e);
                }
                return;
            }

            let now = Instant::now();

            if Self::should_skip_event(&event.path, now, last_paths, debounce_duration) {
                debug!(
                    "Skipping debounced event for path: {}",
                    event.path.display()
                );
                return;
            }

            if last_processed
                .get(&config_id)
                .is_some_and(|last_time| now.duration_since(*last_time) < debounce_duration)
            {
                debug!("Skipping recently processed config ID: {}", config_id);
                return;
            }

            last_paths.insert(event.path.clone(), now);
            last_processed.insert(config_id.clone(), now);

            last_paths.retain(|_, time| now.duration_since(*time) < debounce_duration * 2);
            last_processed.retain(|_, time| now.duration_since(*time) < debounce_duration * 2);

            if let Err(e) =
                Self::handle_config_update(&event.path, file_type, provider_name, provider_sender)
                    .instrument(debug_span!("process_file_change"))
                    .await
            {
                error!("Failed to handle config update: {}", e);
            }
        }
        .instrument(root_span)
        .await;
    }

    fn should_skip_event(
        path: &Path,
        now: Instant,
        last_paths: &HashMap<PathBuf, Instant>,
        debounce_duration: Duration,
    ) -> bool {
        last_paths
            .get(path)
            .is_some_and(|last_time| now.duration_since(*last_time) < debounce_duration)
    }

    #[instrument(skip(self), name = "file_provider: create_watcher")]
    fn create_watcher(&self, file_event_tx: Sender<FileEvent>) -> io::Result<RecommendedWatcher> {
        let mut watcher =
            match notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
                if let Ok(event) = res {
                    if notify::EventKind::is_access(&event.kind)
                        || notify::EventKind::is_other(&event.kind)
                    {
                        return;
                    }

                    if notify::EventKind::is_create(&event.kind)
                        || notify::EventKind::is_modify(&event.kind)
                    {
                        //HACK: Workaround to let the file be written before reading it
                        // Yes that's an actual bug that I can't find a solution to
                        sleep(Duration::from_millis(150)); // Increased sleep time
                    }

                    for path in event.paths {
                        if !path.is_dir() {
                            let _ = file_event_tx.blocking_send(FileEvent {
                                path,
                                kind: event.kind,
                            });
                        }
                    }
                }
            }) {
                Ok(watcher) => watcher,
                Err(e) => {
                    return Err(io::Error::new(io::ErrorKind::Other, e));
                }
            };

        let mut watched_paths = WATCHED_PATHS.write().unwrap();

        for path in &self.paths {
            let canonical_path = match fs::canonicalize(path) {
                Ok(p) => p,
                Err(e) => {
                    warn!("Could not canonicalize path {}: {}", path, e);
                    continue;
                }
            };

            let path_str = canonical_path.to_string_lossy().to_string();

            if watched_paths.contains(&path_str) {
                debug!("Path {} is already being watched, skipping", path_str);
                continue;
            }

            info!(
                "File Provider: Watching path: {} for changes",
                canonical_path.display()
            );

            if let Err(e) = watcher.watch(&canonical_path, RecursiveMode::Recursive) {
                error!("Failed to watch path {}: {}", canonical_path.display(), e);
                continue;
            }

            watched_paths.insert(path_str);
        }

        Ok(watcher)
    }
}

#[async_trait::async_trait]
impl Provider for FileProvider {
    #[instrument(skip(self), name = "file_provider: run", fields(paths = ?self.paths, instance_id = self.instance_id))]
    async fn run(&mut self) {
        let span = debug_span!("file_provider_run", instance_id = self.instance_id);
        async {
            let mut all_configs = HashMap::new();
            for path in &self.paths {
                match self.load_configs(path) {
                    Ok(configs) => {
                        for (id, config) in configs {
                            all_configs.insert(id, config);
                        }
                    }
                    Err(e) => warn!("Failed to load server configs from {}: {:?}", path, e),
                }
            }

            if !all_configs.is_empty() {
                if let Err(e) = self
                    .sender
                    .send(ProviderMessage::FirstInit(all_configs))
                    .await
                {
                    warn!("Failed to send FirstInit: {}", e);
                }

                tokio::time::sleep(Duration::from_millis(200)).await;
            }

            let _watcher = if self.watch {
                match self
                    .setup_file_watcher()
                    .instrument(debug_span!("watcher_setup", instance_id = self.instance_id))
                    .await
                {
                    Ok(watcher) => Some(watcher),
                    Err(e) => {
                        error!(
                            "Failed to setup file watcher for instance {}: {}",
                            self.instance_id, e
                        );
                        None
                    }
                }
            } else {
                None
            };

            // Block indefinitely to keep the watcher alive
            loop {
                tokio::time::sleep(Duration::from_secs(3600)).await;
            }
        }
        .instrument(span)
        .await
    }

    fn get_name(&self) -> String {
        "FileProvider".to_string()
    }

    fn new(sender: mpsc::Sender<ProviderMessage>) -> Self {
        Self {
            paths: Vec::new(),
            file_type: FileType::Yaml,
            watch: false,
            sender,
            instance_id: WATCHER_COUNT.fetch_add(1, Ordering::SeqCst),
        }
    }
}

fn yaml_decoder<T: DeserializeOwned>(content: &str) -> Result<T, serde_yaml::Error> {
    serde_yaml::from_str(content)
}