embystream 0.0.17

Another Emby streaming application (frontend/backend separation) written in Rust.
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
use std::{
    fs,
    io::{Error as IoError, ErrorKind as IoErrorKind},
    path::{Path, PathBuf},
    process,
};

use directories::BaseDirs;
use libc;
use regex::Regex;
use serde::Deserialize;
use tokio::sync::OnceCell;

use super::{
    backend::{Backend, BackendConfig},
    error::ConfigError,
    frontend::Frontend,
    general::{General, StreamMode, UserAgent},
    http2::Http2,
    types::{FallbackConfig, RawConfig},
};

use crate::cli::RunArgs;
use crate::config::backend::types::{
    BackendFallbackConfig, BackendRouteConfig, BackendRoutingConfig,
};
use crate::config::general::{Log, types::Emby};
use crate::core::backend::types::{
    BackendConfig as CoreBackendConfig, BackendRoute, BackendRoutes,
};
use crate::util::resolve_fallback_video_path;
use crate::{
    CONFIG_LOGGER_DOMAIN, config_error_log, config_info_log, config_warn_log,
};

const CONFIG_DIR_NAME: &str = "embystream";
const CONFIG_FILE_NAME: &str = "config.toml";
const SSL_DIR_NAME: &str = "ssl";
const SSL_CER_FILE_NAME: &str = "ssl-cert";
const SSL_KEY_FILE_NAME: &str = "ssl-key";
const DOCKER_CONFIG_PATH: &str = "/config/embystream/config.toml";
const TEMPLATE_CONFIG_PATH: &str = "src/config.toml.template";
const ROOT_CONFIG_PATH: &str = "/root/.config/embystream";

#[derive(Clone, Debug, Deserialize)]
pub struct Config {
    #[serde(skip)]
    pub path: PathBuf,
    pub log: Log,
    pub general: General,
    pub emby: Emby,
    pub user_agent: UserAgent,
    pub frontend: Option<Frontend>,
    pub backend: Option<Backend>,
    /// Legacy single backend config (for backward compatibility)
    pub backend_config: Option<BackendConfig>,
    /// Backend routes configuration (new routing system)
    #[serde(skip)]
    pub backend_routes: Option<BackendRoutes>,
    pub http2: Http2,
    pub fallback: FallbackConfig,
}

impl Config {
    pub fn load_or_init(args: &RunArgs) -> Result<Self, ConfigError> {
        let config_path = match &args.config {
            Some(path) => path.clone(),
            None => Self::get_default_config_path()?.join(CONFIG_FILE_NAME),
        };

        if !config_path.exists() {
            Self::handle_missing_config(&config_path)?;
            process::exit(1);
        }

        config_info_log!(
            CONFIG_LOGGER_DOMAIN,
            "Loading config file from: {}",
            config_path.display()
        );

        let mut config =
            Self::load_from_path(&config_path).unwrap_or_else(|e| {
                config_error_log!(
                    CONFIG_LOGGER_DOMAIN,
                    "Failed to load or parse config file at '{}': {}",
                    config_path.display(),
                    e
                );
                process::exit(1);
            });

        if let Some(cert_path) = &args.ssl_cert_file {
            config.http2.ssl_cert_file =
                cert_path.to_string_lossy().to_string();
        }
        if let Some(key_path) = &args.ssl_key_file {
            config.http2.ssl_key_file = key_path.to_string_lossy().to_string();
        }

        Ok(config)
    }

    pub fn get_ssl_cert_path(&self) -> Option<PathBuf> {
        let config_dir = self.path.parent()?;
        let path_str = &self.http2.ssl_cert_file;

        if path_str.is_empty() {
            return Some(config_dir.join(SSL_DIR_NAME).join(SSL_CER_FILE_NAME));
        }

        let path = PathBuf::from(path_str);
        if path.is_absolute() {
            Some(path)
        } else {
            Some(config_dir.join(path))
        }
    }

    pub fn get_ssl_key_path(&self) -> Option<PathBuf> {
        let config_dir = self.path.parent()?;
        let path_str = &self.http2.ssl_key_file;

        if path_str.is_empty() {
            return Some(config_dir.join(SSL_DIR_NAME).join(SSL_KEY_FILE_NAME));
        }

        let path = PathBuf::from(path_str);
        if path.is_absolute() {
            Some(path)
        } else {
            Some(config_dir.join(path))
        }
    }

    fn load_from_path(path: &Path) -> Result<Self, ConfigError> {
        let content = fs::read_to_string(path)?;
        let raw_config: RawConfig = toml::from_str(&content)?;

        Self::validate_stream_mode(&raw_config)?;
        let (backend_config, backend_routes) =
            Self::build_backend_configs(&raw_config, path)?;

        Ok(Config {
            path: path.to_path_buf(),
            log: raw_config.log,
            general: raw_config.general,
            emby: raw_config.emby,
            user_agent: raw_config.user_agent,
            frontend: raw_config.frontend,
            backend: raw_config.backend,
            backend_config,
            backend_routes,
            http2: raw_config.http2.unwrap_or_default(),
            fallback: raw_config.fallback,
        })
    }

    /// Validate stream mode configuration
    fn validate_stream_mode(raw_config: &RawConfig) -> Result<(), ConfigError> {
        let stream_mode = &raw_config.general.stream_mode;

        if (stream_mode == &StreamMode::Frontend
            || stream_mode == &StreamMode::Dual)
            && raw_config.frontend.is_none()
        {
            return Err(ConfigError::MissingConfig("Frontend".to_string()));
        }

        Ok(())
    }

    /// Build backend configurations based on stream mode
    fn build_backend_configs(
        raw_config: &RawConfig,
        path: &Path,
    ) -> Result<(Option<BackendConfig>, Option<BackendRoutes>), ConfigError>
    {
        let stream_mode = &raw_config.general.stream_mode;
        let needs_backend = stream_mode == &StreamMode::Frontend
            || stream_mode == &StreamMode::Backend
            || stream_mode == &StreamMode::Dual;

        if !needs_backend {
            return Ok((None, None));
        }

        if raw_config.backend.is_none() {
            return Err(ConfigError::MissingConfig("Backend".to_string()));
        }

        Self::log_configured_routes(raw_config);
        let routes = Self::build_backend_routes(raw_config, path)?;

        Ok((None, Some(routes)))
    }

    /// Log all configured routes (enabled and disabled)
    fn log_configured_routes(raw_config: &RawConfig) {
        let backend_routes = raw_config
            .backend
            .as_ref()
            .map(|b| b.routes.clone())
            .unwrap_or_default();

        if !backend_routes.is_empty() {
            config_info_log!(
                CONFIG_LOGGER_DOMAIN,
                "Found {} backend route(s) in configuration",
                backend_routes.len()
            );
            for (i, route) in backend_routes.iter().enumerate() {
                let status = if route.enable { "ENABLED" } else { "DISABLED" };
                config_info_log!(
                    CONFIG_LOGGER_DOMAIN,
                    "  Route #{}: [{}] pattern=\"{}\", backend_type=\"{}\"",
                    i + 1,
                    status,
                    route.pattern,
                    route.backend_type
                );
            }
        } else {
            config_warn_log!(
                CONFIG_LOGGER_DOMAIN,
                "DEPRECATED: backend_type in [General] section is deprecated. \
                Please configure [[Backend.Routes]] and [Backend.Fallback] instead. \
                Using backend_type=\"{}\" for default route.",
                raw_config.general.backend_type
            );
        }
    }

    fn get_default_config_path() -> Result<PathBuf, ConfigError> {
        if Path::new(DOCKER_CONFIG_PATH).exists() {
            return Ok(PathBuf::from(DOCKER_CONFIG_PATH));
        }

        let base_dirs = BaseDirs::new().ok_or(ConfigError::NoHomeDir)?;

        let path =
            if cfg!(target_os = "linux") && unsafe { libc::getuid() } == 0 {
                PathBuf::from(ROOT_CONFIG_PATH)
            } else if cfg!(target_os = "windows") {
                base_dirs.config_dir().join(CONFIG_DIR_NAME)
            } else {
                // macOS and other Unix-like systems
                base_dirs.home_dir().join(".config").join(CONFIG_DIR_NAME)
            };

        if !path.exists() {
            fs::create_dir_all(&path).map_err(|e| ConfigError::CreateDir {
                path: path.display().to_string(),
                source: e,
            })?;

            let ssl_path = path.join(SSL_DIR_NAME);
            fs::create_dir_all(&ssl_path).map_err(|e| {
                ConfigError::CreateDir {
                    path: ssl_path.display().to_string(),
                    source: e,
                }
            })?;
        }

        Ok(path)
    }

    fn handle_missing_config(target_path: &Path) -> Result<(), ConfigError> {
        let template_path = Path::new(TEMPLATE_CONFIG_PATH);

        if !template_path.exists() {
            config_error_log!(
                CONFIG_LOGGER_DOMAIN,
                "Missing template config file at {}",
                template_path.display()
            );
            return Err(ConfigError::CopyTemplate(IoError::new(
                IoErrorKind::NotFound,
                format!(
                    "Template file not found at {}",
                    template_path.display()
                ),
            )));
        }

        if let Some(parent) = target_path.parent() {
            fs::create_dir_all(parent).map_err(|e| ConfigError::CreateDir {
                path: parent.display().to_string(),
                source: e,
            })?;
        }

        fs::copy(template_path, target_path)
            .map_err(ConfigError::CopyTemplate)?;

        config_info_log!(
            CONFIG_LOGGER_DOMAIN,
            "Created new config file at {} from template",
            target_path.display()
        );

        config_error_log!(
            CONFIG_LOGGER_DOMAIN,
            "Please configure the new file and restart the application"
        );
        process::exit(0);
    }

    /// Build backend routes from raw configuration
    fn build_backend_routes(
        raw_config: &RawConfig,
        config_path: &Path,
    ) -> Result<BackendRoutes, ConfigError> {
        let routing_config = Self::get_routing_config(raw_config);

        if !routing_config.enable {
            return Self::build_default_routes(raw_config, config_path);
        }

        let routes = Self::build_route_list(raw_config, config_path)?;
        let fallback_config =
            Self::build_fallback_config(raw_config, config_path)?;

        Ok(BackendRoutes {
            routes,
            fallback: fallback_config,
            match_before_rewrite: routing_config.match_before_rewrite,
            match_priority_first: routing_config.match_priority == "first",
        })
    }

    /// Get routing configuration from raw config
    fn get_routing_config(raw_config: &RawConfig) -> BackendRoutingConfig {
        raw_config
            .backend
            .as_ref()
            .and_then(|b| b.routing.clone())
            .unwrap_or_else(|| BackendRoutingConfig {
                enable: false,
                match_before_rewrite: false,
                match_priority: "first".to_string(),
            })
    }

    /// Build default routes when routing is disabled
    fn build_default_routes(
        raw_config: &RawConfig,
        config_path: &Path,
    ) -> Result<BackendRoutes, ConfigError> {
        let default_route = BackendRouteConfig {
            enable: true,
            pattern: ".*".to_string(),
            backend_type: raw_config.general.backend_type.clone(),
        };

        let backend_config_enum = Self::get_backend_config_by_type(
            &default_route.backend_type,
            raw_config,
        )?;
        let backend_config = Self::build_backend_config_instance(
            raw_config,
            config_path,
            backend_config_enum,
        );

        let fallback_config_enum = Self::get_backend_config_by_type(
            &default_route.backend_type,
            raw_config,
        )?;
        let fallback_config = Self::build_backend_config_instance(
            raw_config,
            config_path,
            fallback_config_enum,
        );

        Ok(BackendRoutes {
            routes: vec![BackendRoute {
                pattern: default_route.pattern,
                regex: OnceCell::new(),
                backend_config,
            }],
            fallback: fallback_config,
            match_before_rewrite: false,
            match_priority_first: true,
        })
    }

    /// Build list of route rules from configuration
    fn build_route_list(
        raw_config: &RawConfig,
        config_path: &Path,
    ) -> Result<Vec<BackendRoute>, ConfigError> {
        let backend_routes = raw_config
            .backend
            .as_ref()
            .map(|b| b.routes.clone())
            .unwrap_or_default();

        let enabled_routes: Vec<_> =
            backend_routes.into_iter().filter(|r| r.enable).collect();

        Self::log_enabled_routes(&enabled_routes, raw_config);

        let routes_to_process = if enabled_routes.is_empty() {
            vec![BackendRouteConfig {
                enable: true,
                pattern: ".*".to_string(),
                backend_type: raw_config.general.backend_type.clone(),
            }]
        } else {
            enabled_routes
        };

        let mut routes = Vec::new();
        for (index, route_config) in routes_to_process.iter().enumerate() {
            Self::validate_route_pattern(&route_config.pattern, index + 1)?;

            let backend_config_enum = Self::get_backend_config_by_type(
                &route_config.backend_type,
                raw_config,
            )?;
            let backend_config = Self::build_backend_config_instance(
                raw_config,
                config_path,
                backend_config_enum,
            );

            routes.push(BackendRoute {
                pattern: route_config.pattern.clone(),
                regex: OnceCell::new(),
                backend_config,
            });
        }

        Ok(routes)
    }

    /// Log enabled routes information
    fn log_enabled_routes(
        enabled_routes: &[BackendRouteConfig],
        raw_config: &RawConfig,
    ) {
        if !enabled_routes.is_empty() {
            config_info_log!(
                CONFIG_LOGGER_DOMAIN,
                "Using {} enabled route(s) for routing",
                enabled_routes.len()
            );
        } else if !raw_config
            .backend
            .as_ref()
            .map(|b| b.routes.is_empty())
            .unwrap_or(true)
        {
            config_info_log!(
                CONFIG_LOGGER_DOMAIN,
                "No enabled routes found, using default route with backend_type=\"{}\"",
                raw_config.general.backend_type
            );
        }
    }

    /// Validate regex pattern for a route
    fn validate_route_pattern(
        pattern: &str,
        index: usize,
    ) -> Result<(), ConfigError> {
        Regex::new(pattern).map_err(|e| {
            config_error_log!(
                CONFIG_LOGGER_DOMAIN,
                "Invalid regex pattern in route #{}: \"{}\", error: {}",
                index,
                pattern,
                e
            );
            ConfigError::InvalidRegex {
                pattern: pattern.to_string(),
                error: e.to_string(),
            }
        })?;
        Ok(())
    }

    /// Build fallback backend configuration
    fn build_fallback_config(
        raw_config: &RawConfig,
        config_path: &Path,
    ) -> Result<CoreBackendConfig, ConfigError> {
        let backend_fallback =
            raw_config.backend.as_ref().and_then(|b| b.fallback.clone());

        let fallback_config_enum = if let Some(fallback) = &backend_fallback {
            Self::resolve_fallback_backend_type(fallback, raw_config)?
        } else {
            Self::resolve_legacy_fallback_backend_type(raw_config)?
        };

        Ok(Self::build_backend_config_instance(
            raw_config,
            config_path,
            fallback_config_enum,
        ))
    }

    /// Resolve fallback backend type from configured fallback
    fn resolve_fallback_backend_type(
        fallback: &BackendFallbackConfig,
        raw_config: &RawConfig,
    ) -> Result<BackendConfig, ConfigError> {
        if fallback.enable {
            config_info_log!(
                CONFIG_LOGGER_DOMAIN,
                "Fallback backend enabled: type=\"{}\"",
                fallback.backend_type
            );
            Self::get_backend_config_by_type(&fallback.backend_type, raw_config)
        } else {
            let backend_type = raw_config.general.backend_type.as_str();
            config_info_log!(
                CONFIG_LOGGER_DOMAIN,
                "Fallback backend disabled, using backend_type=\"{}\" as fallback",
                backend_type
            );
            config_warn_log!(
                CONFIG_LOGGER_DOMAIN,
                "DEPRECATED: backend_type in [General] section is deprecated. \
                Please configure [Backend.Fallback] with enable=true instead."
            );
            Self::get_backend_config_by_type(backend_type, raw_config)
        }
    }

    /// Resolve fallback backend type from legacy backend_type
    fn resolve_legacy_fallback_backend_type(
        raw_config: &RawConfig,
    ) -> Result<BackendConfig, ConfigError> {
        let backend_type = raw_config.general.backend_type.as_str();
        config_info_log!(
            CONFIG_LOGGER_DOMAIN,
            "No [Backend.Fallback] configured, using backend_type=\"{}\" as fallback",
            backend_type
        );
        config_warn_log!(
            CONFIG_LOGGER_DOMAIN,
            "DEPRECATED: backend_type in [General] section is deprecated. \
            Please configure [Backend.Fallback] with enable=true instead."
        );
        Self::get_backend_config_by_type(backend_type, raw_config)
    }

    /// Build a BackendConfig instance from raw config and backend config enum
    fn build_backend_config_instance(
        raw_config: &RawConfig,
        config_path: &Path,
        backend_config_enum: BackendConfig,
    ) -> CoreBackendConfig {
        let backend =
            raw_config.backend.as_ref().expect("Backend config missing");
        let fallback_video_path = resolve_fallback_video_path(
            &raw_config.fallback.video_missing_path,
            config_path,
        );

        CoreBackendConfig {
            crypto_key: raw_config.general.encipher_key.clone(),
            crypto_iv: raw_config.general.encipher_iv.clone(),
            backend: backend.clone(),
            backend_config: backend_config_enum,
            fallback_video_path,
        }
    }

    /// Get backend config enum by backend type string
    fn get_backend_config_by_type(
        backend_type: &str,
        raw_config: &RawConfig,
    ) -> Result<BackendConfig, ConfigError> {
        match backend_type.to_lowercase().as_str() {
            "disk" => raw_config
                .disk
                .as_ref()
                .map(|d| BackendConfig::Disk(d.clone()))
                .ok_or_else(|| ConfigError::MissingConfig("Disk".to_string())),
            "openlist" => raw_config
                .open_list
                .as_ref()
                .map(|o| BackendConfig::OpenList(o.clone()))
                .ok_or_else(|| {
                    ConfigError::MissingConfig("OpenList".to_string())
                }),
            "direct_link" => raw_config
                .direct_link
                .as_ref()
                .map(|d| BackendConfig::DirectLink(d.clone()))
                .ok_or_else(|| {
                    ConfigError::MissingConfig("DirectLink".to_string())
                }),
            other => Err(ConfigError::InvalidBackendType(other.to_string())),
        }
    }
}