rustpbx 0.3.18

A SIP PBX implementation 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use crate::{
    call::{CallRecordingConfig, DialDirection, QueuePlan, user::SipUser},
    proxy::routing::{RouteQueueConfig, RouteRule, TrunkConfig},
    storage::StorageConfig,
};
use anyhow::{Error, Result};
use clap::Parser;
use rsip::StatusCode;
use rsipstack::dialog::invitation::InviteOption;
use rustrtc::IceServer;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};

#[derive(Parser, Debug)]
#[command(version)]
pub(crate) struct Cli {
    #[clap(long, default_value = "rustpbx.toml")]
    pub conf: Option<String>,
}

pub(crate) fn default_config_recorder_path() -> String {
    #[cfg(target_os = "windows")]
    return "./config/recorders".to_string();
    #[cfg(not(target_os = "windows"))]
    return "./config/recorders".to_string();
}

fn default_config_http_addr() -> String {
    "0.0.0.0:8080".to_string()
}

fn default_database_url() -> String {
    "sqlite://rustpbx.sqlite3".to_string()
}

fn default_console_session_secret() -> String {
    rsipstack::transaction::random_text(32)
}

fn default_console_base_path() -> String {
    "/console".to_string()
}

fn default_config_rtp_start_port() -> Option<u16> {
    Some(12000)
}

fn default_config_rtp_end_port() -> Option<u16> {
    Some(42000)
}

fn default_config_webrtc_start_port() -> Option<u16> {
    Some(30000)
}

fn default_config_webrtc_end_port() -> Option<u16> {
    Some(40000)
}

fn default_useragent() -> Option<String> {
    Some(crate::version::get_useragent())
}

fn default_nat_fix() -> bool {
    true
}

fn default_callid_suffix() -> Option<String> {
    Some("miuda.ai".to_string())
}

fn default_user_backends() -> Vec<UserBackendConfig> {
    vec![UserBackendConfig::default()]
}

fn default_generated_config_dir() -> String {
    "./config".to_string()
}

#[derive(Debug, Clone, Deserialize, Serialize, Copy, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum RecordingDirection {
    Inbound,
    Outbound,
    Internal,
}

impl RecordingDirection {
    pub fn matches(&self, direction: &DialDirection) -> bool {
        match (self, direction) {
            (RecordingDirection::Inbound, DialDirection::Inbound) => true,
            (RecordingDirection::Outbound, DialDirection::Outbound) => true,
            (RecordingDirection::Internal, DialDirection::Internal) => true,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "snake_case")]
pub struct RecordingPolicy {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub directions: Vec<RecordingDirection>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub caller_allow: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub caller_deny: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub callee_allow: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub callee_deny: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auto_start: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub filename_pattern: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub samplerate: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ptime: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
}

impl RecordingPolicy {
    pub fn new_recording_config(&self) -> CallRecordingConfig {
        crate::call::CallRecordingConfig {
            enabled: self.enabled,
            auto_start: self.auto_start.unwrap_or(true),
            option: None,
        }
    }
    pub fn recorder_path(&self) -> String {
        self.path
            .as_ref()
            .map(|p| p.trim())
            .filter(|p| !p.is_empty())
            .map(|p| p.to_string())
            .unwrap_or_else(default_config_recorder_path)
    }

    pub fn ensure_defaults(&mut self) -> bool {
        if self
            .path
            .as_ref()
            .map(|p| p.trim().is_empty())
            .unwrap_or(true)
        {
            self.path = Some(default_config_recorder_path());
            true
        } else {
            false
        }
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
    #[serde(default = "default_config_http_addr")]
    pub http_addr: String,
    #[serde(default)]
    pub http_gzip: bool,
    pub https_addr: Option<String>,
    pub ssl_certificate: Option<String>,
    pub ssl_private_key: Option<String>,
    pub log_level: Option<String>,
    pub log_file: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub http_access_skip_paths: Vec<String>,
    pub proxy: ProxyConfig,

    pub external_ip: Option<String>,
    #[serde(default = "default_config_rtp_start_port")]
    pub rtp_start_port: Option<u16>,
    #[serde(default = "default_config_rtp_end_port")]
    pub rtp_end_port: Option<u16>,

    #[serde(default = "default_config_webrtc_start_port")]
    pub webrtc_port_start: Option<u16>,
    #[serde(default = "default_config_webrtc_end_port")]
    pub webrtc_port_end: Option<u16>,

    pub callrecord: Option<CallRecordConfig>,
    pub ice_servers: Option<Vec<IceServer>>,
    #[serde(default)]
    pub ami: Option<AmiConfig>,
    #[cfg(feature = "console")]
    pub console: Option<ConsoleConfig>,
    #[serde(default = "default_database_url")]
    pub database_url: String,
    #[serde(default)]
    pub recording: Option<RecordingPolicy>,
    #[serde(default)]
    pub archive: Option<ArchiveConfig>,
    #[serde(default)]
    pub demo_mode: bool,
    #[serde(default)]
    pub addons: HashMap<String, HashMap<String, String>>,
    #[serde(default)]
    pub storage: Option<StorageConfig>,
    #[serde(default)]
    pub sipflow: Option<SipFlowConfig>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ArchiveConfig {
    pub enabled: bool,
    pub archive_time: String,
    pub timezone: Option<String>,
    pub retention_days: u32,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ConsoleConfig {
    #[serde(default = "default_console_session_secret")]
    pub session_secret: String,
    #[serde(default = "default_console_base_path")]
    pub base_path: String,
    #[serde(default)]
    pub allow_registration: bool,
    #[serde(default)]
    pub secure_cookie: bool,
    pub alpine_js: Option<String>,
    pub tailwind_js: Option<String>,
    pub chart_js: Option<String>,
}

impl Default for ConsoleConfig {
    fn default() -> Self {
        Self {
            session_secret: default_console_session_secret(),
            base_path: default_console_base_path(),
            allow_registration: false,
            secure_cookie: false,
            alpine_js: None,
            tailwind_js: None,
            chart_js: None,
        }
    }
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum UserBackendConfig {
    Memory {
        users: Option<Vec<SipUser>>,
    },
    Http {
        url: String,
        method: Option<String>,
        username_field: Option<String>,
        realm_field: Option<String>,
        headers: Option<HashMap<String, String>>,
        sip_headers: Option<Vec<String>>,
    },
    Plain {
        path: String,
    },
    Database {
        url: Option<String>,
        table_name: Option<String>,
        id_column: Option<String>,
        username_column: Option<String>,
        password_column: Option<String>,
        enabled_column: Option<String>,
        realm_column: Option<String>,
    },
    Extension {
        #[serde(default)]
        database_url: Option<String>,
        #[serde(default)]
        ttl: Option<u64>,
    },
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum LocatorConfig {
    Memory,
    Http {
        url: String,
        method: Option<String>,
        username_field: Option<String>,
        expires_field: Option<String>,
        realm_field: Option<String>,
        headers: Option<HashMap<String, String>>,
    },
    Database {
        url: String,
    },
}

pub use crate::storage::S3Vendor;

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum CallRecordConfig {
    Local {
        root: String,
    },
    S3 {
        vendor: S3Vendor,
        bucket: String,
        region: String,
        access_key: String,
        secret_key: String,
        endpoint: String,
        root: String,
        with_media: Option<bool>,
        keep_media_copy: Option<bool>,
    },
    Http {
        url: String,
        headers: Option<HashMap<String, String>>,
        with_media: Option<bool>,
        keep_media_copy: Option<bool>,
    },
}

/// Directory structure for sipflow storage
#[derive(Debug, Deserialize, Clone, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum SipFlowSubdirs {
    /// No subdirectory structure - all files in root
    None,
    /// Daily subdirectories (YYYYMMDD)
    Daily,
    /// Hourly subdirectories (YYYYMMDD/HH)
    Hourly,
}

impl Default for SipFlowSubdirs {
    fn default() -> Self {
        Self::Daily
    }
}

#[derive(Debug, Deserialize, Clone, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum SipFlowConfig {
    Local {
        root: String,
        #[serde(default)]
        subdirs: SipFlowSubdirs,
        #[serde(default = "default_sipflow_flush_count")]
        flush_count: usize,
        #[serde(default = "default_sipflow_flush_interval")]
        flush_interval_secs: u64,
        #[serde(default = "default_sipflow_id_cache_size")]
        id_cache_size: usize,
    },
    Remote {
        udp_addr: String,
        http_addr: String,
        #[serde(default = "default_sipflow_timeout")]
        timeout_secs: u64,
    },
}

fn default_sipflow_flush_count() -> usize {
    1000
}

fn default_sipflow_flush_interval() -> u64 {
    5
}

fn default_sipflow_timeout() -> u64 {
    10
}

fn default_sipflow_id_cache_size() -> usize {
    8192
}

#[derive(Debug, Deserialize, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(PartialEq)]
pub enum MediaProxyMode {
    /// All media goes through proxy
    All,
    /// Auto detect if media proxy is needed (webrtc to rtp)
    Auto,
    /// Only handle NAT (private IP addresses)
    Nat,
    /// Do not handle media proxy
    None,
}

impl Default for MediaProxyMode {
    fn default() -> Self {
        Self::Auto
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Default)]
pub struct RtpConfig {
    pub external_ip: Option<String>,
    pub start_port: Option<u16>,
    pub end_port: Option<u16>,
    pub webrtc_start_port: Option<u16>,
    pub webrtc_end_port: Option<u16>,
    pub ice_servers: Option<Vec<IceServer>>,
}

#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct HttpRouterConfig {
    pub url: String,
    pub headers: Option<HashMap<String, String>>,
    #[serde(default)]
    pub fallback_to_static: bool,
    pub timeout_ms: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LocatorWebhookConfig {
    pub url: String,
    #[serde(default)]
    pub events: Vec<String>,
    pub headers: Option<HashMap<String, String>>,
    pub timeout_ms: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ProxyConfig {
    pub modules: Option<Vec<String>>,
    pub addr: String,
    #[serde(default = "default_useragent")]
    pub useragent: Option<String>,
    #[serde(default = "default_callid_suffix")]
    pub callid_suffix: Option<String>,
    pub ssl_private_key: Option<String>,
    pub ssl_certificate: Option<String>,
    pub udp_port: Option<u16>,
    pub tcp_port: Option<u16>,
    pub tls_port: Option<u16>,
    pub ws_port: Option<u16>,
    pub acl_rules: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub acl_files: Vec<String>,
    pub ua_white_list: Option<Vec<String>>,
    pub ua_black_list: Option<Vec<String>>,
    pub max_concurrency: Option<usize>,
    pub registrar_expires: Option<u32>,
    pub ensure_user: Option<bool>,
    #[serde(default = "default_user_backends")]
    pub user_backends: Vec<UserBackendConfig>,
    #[serde(default)]
    pub locator: LocatorConfig,
    pub locator_webhook: Option<LocatorWebhookConfig>,
    #[serde(default)]
    pub media_proxy: MediaProxyMode,
    pub codecs: Option<Vec<String>>,
    #[serde(default)]
    pub frequency_limiter: Option<String>,
    #[serde(default)]
    pub realms: Option<Vec<String>>,
    pub ws_handler: Option<String>,
    pub http_router: Option<HttpRouterConfig>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub routes_files: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub routes: Option<Vec<RouteRule>>,
    #[serde(default)]
    pub session_timer: bool,
    #[serde(default)]
    pub session_expires: Option<u64>,
    #[serde(default)]
    pub queues: HashMap<String, RouteQueueConfig>,
    #[serde(default)]
    pub enable_latching: bool,
    #[serde(default)]
    pub trunks: HashMap<String, TrunkConfig>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub trunks_files: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub queue_dir: Option<String>,
    #[serde(default)]
    pub recording: Option<RecordingPolicy>,
    #[serde(default = "default_generated_config_dir")]
    pub generated_dir: String,
    #[serde(default = "default_nat_fix")]
    pub nat_fix: bool,
    pub sip_flow_max_items: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub addons: Option<Vec<String>>,
}

#[derive(Default, Clone)]
pub struct DialplanHints {
    pub enable_recording: Option<bool>,
    pub bypass_media: Option<bool>,
    pub max_duration: Option<std::time::Duration>,
    pub enable_sipflow: Option<bool>,
    pub allow_codecs: Option<Vec<String>>,
    pub extensions: http::Extensions,
}

impl std::fmt::Debug for DialplanHints {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DialplanHints")
            .field("enable_recording", &self.enable_recording)
            .field("bypass_media", &self.bypass_media)
            .field("max_duration", &self.max_duration)
            .field("enable_sipflow", &self.enable_sipflow)
            .finish()
    }
}

pub enum RouteResult {
    Forward(InviteOption, Option<DialplanHints>),
    Queue {
        option: InviteOption,
        queue: QueuePlan,
        hints: Option<DialplanHints>,
    },
    NotHandled(InviteOption, Option<DialplanHints>),
    Abort(StatusCode, Option<String>),
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AmiConfig {
    pub allows: Option<Vec<String>>,
}

impl AmiConfig {
    pub fn is_allowed(&self, addr: &str) -> bool {
        if let Some(allows) = &self.allows {
            allows.iter().any(|a| a == addr || a == "*")
        } else {
            addr == "127.0.0.1" || addr == "::1" || addr == "localhost"
        }
    }
}

impl Default for AmiConfig {
    fn default() -> Self {
        Self { allows: None }
    }
}

impl ProxyConfig {
    pub fn normalize_realm(realm: &str) -> &str {
        let realm = if let Some(pos) = realm.find(':') {
            &realm[..pos]
        } else {
            realm
        };
        if realm.is_empty() || realm == "*" || realm == "127.0.0.1" || realm == "::1" {
            "localhost"
        } else {
            realm
        }
    }

    pub fn select_realm(&self, request_host: &str) -> String {
        let requested = request_host.trim();
        let normalized = ProxyConfig::normalize_realm(requested);
        if let Some(realms) = self.realms.as_ref() {
            if let Some(existing) = realms
                .iter()
                .find(|realm| realm.as_str() == requested || realm.as_str() == normalized)
            {
                return existing.clone();
            }
            if let Some(first) = realms.first() {
                if !first.is_empty() {
                    return first.clone();
                }
            }
        }

        if requested.is_empty() {
            normalized.to_string()
        } else {
            requested.to_string()
        }
    }

    pub fn generated_root_dir(&self) -> PathBuf {
        let trimmed = self.generated_dir.trim();
        if trimmed.is_empty() {
            return PathBuf::from("./config");
        }
        PathBuf::from(trimmed)
    }

    pub fn generated_trunks_dir(&self) -> PathBuf {
        self.generated_root_dir().join("trunks")
    }

    pub fn generated_routes_dir(&self) -> PathBuf {
        self.generated_root_dir().join("routes")
    }

    pub fn generated_queue_dir(&self) -> PathBuf {
        if let Some(dir) = self
            .queue_dir
            .as_ref()
            .map(|path| path.trim())
            .filter(|path| !path.is_empty())
        {
            PathBuf::from(dir)
        } else {
            self.generated_root_dir().join("queue")
        }
    }

    pub fn generated_acl_dir(&self) -> PathBuf {
        self.generated_root_dir().join("acl")
    }

    pub fn ensure_recording_defaults(&mut self) -> bool {
        let mut fallback = false;

        if let Some(policy) = self.recording.as_mut() {
            fallback |= policy.ensure_defaults();
        }

        for trunk in self.trunks.values_mut() {
            if let Some(policy) = trunk.recording.as_mut() {
                fallback |= policy.ensure_defaults();
            }
        }
        fallback
    }
}

impl Default for ProxyConfig {
    fn default() -> Self {
        Self {
            acl_rules: Some(vec!["allow all".to_string(), "deny all".to_string()]),
            ua_white_list: Some(vec![]),
            ua_black_list: Some(vec![]),
            addr: "0.0.0.0".to_string(),
            modules: Some(vec![
                "acl".to_string(),
                "auth".to_string(),
                "registrar".to_string(),
                "call".to_string(),
            ]),
            useragent: default_useragent(),
            callid_suffix: default_callid_suffix(),
            ssl_private_key: None,
            ssl_certificate: None,
            udp_port: Some(5060),
            tcp_port: None,
            tls_port: None,
            ws_port: None,
            max_concurrency: None,
            registrar_expires: Some(60),
            ensure_user: Some(true),
            enable_latching: false,
            user_backends: default_user_backends(),
            locator: LocatorConfig::default(),
            locator_webhook: None,
            media_proxy: MediaProxyMode::default(),
            codecs: None,
            frequency_limiter: None,
            realms: Some(vec![]),
            ws_handler: None,
            http_router: None,
            routes_files: Vec::new(),
            acl_files: Vec::new(),
            routes: None,
            session_timer: false,
            session_expires: None,
            queues: HashMap::new(),
            trunks: HashMap::new(),
            trunks_files: Vec::new(),
            queue_dir: None,
            recording: None,
            generated_dir: default_generated_config_dir(),
            nat_fix: true,
            sip_flow_max_items: None,
            addons: None,
        }
    }
}

impl Default for UserBackendConfig {
    fn default() -> Self {
        Self::Memory { users: None }
    }
}

impl Default for LocatorConfig {
    fn default() -> Self {
        Self::Memory
    }
}

impl Default for CallRecordConfig {
    fn default() -> Self {
        Self::Local {
            #[cfg(target_os = "windows")]
            root: "./config/cdr".to_string(),
            #[cfg(not(target_os = "windows"))]
            root: "./config/cdr".to_string(),
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            http_addr: default_config_http_addr(),
            http_gzip: false,
            https_addr: None,
            ssl_certificate: None,
            ssl_private_key: None,
            log_level: None,
            log_file: None,
            http_access_skip_paths: Vec::new(),
            proxy: ProxyConfig::default(),
            callrecord: None,
            ice_servers: None,
            ami: Some(AmiConfig::default()),
            external_ip: None,
            rtp_start_port: default_config_rtp_start_port(),
            rtp_end_port: default_config_rtp_end_port(),
            webrtc_port_start: default_config_webrtc_start_port(),
            webrtc_port_end: default_config_webrtc_end_port(),
            #[cfg(feature = "console")]
            console: None,
            database_url: default_database_url(),
            recording: None,
            archive: None,
            demo_mode: false,
            storage: None,
            addons: HashMap::new(),
            sipflow: None,
        }
    }
}

impl Clone for Config {
    fn clone(&self) -> Self {
        // This is a bit expensive but Config is not cloned often in hot paths
        // and implementing Clone manually for all nested structs is tedious
        let s = toml::to_string(self).unwrap();
        toml::from_str(&s).unwrap()
    }
}

impl Config {
    pub fn load(path: &str) -> Result<Self, Error> {
        let mut config: Self = toml::from_str(
            &std::fs::read_to_string(path).map_err(|e| anyhow::anyhow!("{}: {}", e, path))?,
        )?;
        if std::env::var("RUSTPBX_DEMO_MODE")
            .map(|v| v == "true" || v == "1")
            .unwrap_or(false)
        {
            config.demo_mode = true;
        }
        if config.ensure_recording_defaults() {
            tracing::warn!(
                "recorder_format=ogg requires compiling with the 'opus' feature; falling back to wav"
            );
        }
        Ok(config)
    }

    pub fn rtp_config(&self) -> RtpConfig {
        RtpConfig {
            external_ip: self.external_ip.clone(),
            start_port: self.rtp_start_port.clone(),
            end_port: self.rtp_end_port.clone(),
            webrtc_start_port: self.webrtc_port_start.clone(),
            webrtc_end_port: self.webrtc_port_end.clone(),
            ice_servers: self.ice_servers.clone(),
        }
    }

    pub fn recorder_path(&self) -> String {
        self.recording
            .as_ref()
            .map(|policy| policy.recorder_path())
            .unwrap_or_else(default_config_recorder_path)
    }

    pub fn ensure_recording_defaults(&mut self) -> bool {
        let mut fallback = false;

        if let Some(policy) = self.recording.as_mut() {
            fallback |= policy.ensure_defaults();
        }

        fallback |= self.proxy.ensure_recording_defaults();

        fallback
    }

    pub fn config_dir(&self) -> std::path::PathBuf {
        self.proxy.generated_root_dir()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_dump() {
        let mut config = Config::default();
        let mut prxconfig = ProxyConfig::default();
        let mut trunks = HashMap::new();
        let mut routes = Vec::new();
        let mut ice_servers = Vec::new();
        ice_servers.push(IceServer {
            urls: vec!["stun:stun.l.google.com:19302".to_string()],
            username: Some("user".to_string()),
            ..Default::default()
        });
        ice_servers.push(IceServer {
            urls: vec![
                "stun:restsend.com:3478".to_string(),
                "turn:stun.l.google.com:1112?transport=TCP".to_string(),
            ],
            username: Some("user".to_string()),
            ..Default::default()
        });

        routes.push(crate::proxy::routing::RouteRule {
            name: "default".to_string(),
            description: None,
            priority: 1,
            match_conditions: crate::proxy::routing::MatchConditions {
                to_user: Some("xx".to_string()),
                ..Default::default()
            },
            rewrite: Some(crate::proxy::routing::RewriteRules {
                to_user: Some("xx".to_string()),
                ..Default::default()
            }),
            action: crate::proxy::routing::RouteAction::default(),
            disabled: None,
            ..Default::default()
        });
        routes.push(crate::proxy::routing::RouteRule {
            name: "default3".to_string(),
            description: None,
            priority: 1,
            match_conditions: crate::proxy::routing::MatchConditions {
                to_user: Some("xx3".to_string()),
                ..Default::default()
            },
            rewrite: Some(crate::proxy::routing::RewriteRules {
                to_user: Some("xx3".to_string()),
                ..Default::default()
            }),
            action: crate::proxy::routing::RouteAction::default(),
            disabled: None,
            ..Default::default()
        });
        prxconfig.routes = Some(routes);
        trunks.insert(
            "hello".to_string(),
            crate::proxy::routing::TrunkConfig {
                dest: "sip:127.0.0.1:5060".to_string(),
                ..Default::default()
            },
        );
        prxconfig.trunks = trunks;
        config.proxy = prxconfig;
        config.ice_servers = Some(ice_servers);
        let config_str = toml::to_string(&config).unwrap();
        println!("{}", config_str);
    }

    #[test]
    fn test_normalize_realm() {
        assert_eq!(ProxyConfig::normalize_realm("localhost"), "localhost");
        assert_eq!(ProxyConfig::normalize_realm("127.0.0.1"), "localhost");
        assert_eq!(ProxyConfig::normalize_realm("::1"), "localhost");
        assert_eq!(ProxyConfig::normalize_realm(""), "localhost");
        assert_eq!(ProxyConfig::normalize_realm("*"), "localhost");
        assert_eq!(ProxyConfig::normalize_realm("example.com"), "example.com");
        assert_eq!(
            ProxyConfig::normalize_realm("example.com:5060"),
            "example.com"
        );
        assert_eq!(ProxyConfig::normalize_realm("127.0.0.1:5060"), "localhost");
    }

    #[test]
    fn test_select_realm() {
        let mut config = ProxyConfig::default();
        config.realms = Some(vec!["example.com".to_string(), "test.com".to_string()]);

        // Exact match
        assert_eq!(config.select_realm("example.com"), "example.com");
        // Match with port (should return normalized/existing realm)
        assert_eq!(config.select_realm("example.com:5060"), "example.com");
        // Match with different port
        assert_eq!(config.select_realm("test.com:8888"), "test.com");
        // No match, return first realm if configured
        assert_eq!(config.select_realm("other.com"), "example.com");
        // No match with port, return first realm if configured
        assert_eq!(config.select_realm("other.com:5060"), "example.com");
    }
}