rustpbx 0.3.19

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
use serde::{Deserialize, Serialize};

/// Re-export the real LicenseConfig from config for commerce builds.
#[cfg(feature = "commerce")]
pub use crate::config::LicenseConfig;

/// Stub used so the `can_enable_addon` signature stays consistent in
/// non-commerce builds.
#[cfg(not(feature = "commerce"))]
#[derive(Debug, Clone, Default)]
pub struct LicenseConfig;

/// License status for UI display (without exposing the actual key)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LicenseStatus {
    pub key_name: String,
    pub valid: bool,
    pub expired: bool,
    pub expiry: Option<String>,
    pub plan: String,
    /// True when running under the built-in free-trial window (no key required).
    #[serde(default)]
    pub is_trial: bool,
    /// Addon IDs this license covers. `None` = all addons (unlimited scope).
    #[serde(default)]
    pub scope: Option<Vec<String>>,
}

#[cfg(not(feature = "commerce"))]
impl LicenseConfig {
    pub fn get_license_for_addon(&self, _addon_id: &str) -> Option<(String, String)> {
        None
    }

    pub fn get_addons_for_key(&self, _key_name: &str) -> Vec<&str> {
        Vec::new()
    }
}

/// Check if an addon is allowed to run (always true without commerce feature).
#[cfg(not(feature = "commerce"))]
pub async fn can_enable_addon(
    _addon_id: &str,
    _is_commercial: bool,
    _license_config: &Option<LicenseConfig>,
) -> bool {
    true
}

#[cfg(not(feature = "commerce"))]
pub fn get_license_status(_addon_id: &str) -> Option<LicenseStatus> {
    None
}

// ── Commerce-only license functionality ──────────────────────────────────────

#[cfg(feature = "commerce")]
mod inner {
    use chrono::{DateTime, Utc};
    use once_cell::sync::Lazy;
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;
    use std::sync::Mutex;

    use super::{LicenseConfig, LicenseStatus};

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct LicenseInfo {
        pub key: String,
        pub valid: bool,
        pub expiry: Option<DateTime<Utc>>,
        pub plan: String,
        pub last_checked: DateTime<Utc>,
        /// Addon IDs this license covers. `None` = all addons (unlimited scope).
        #[serde(default)]
        pub scope: Option<Vec<String>>,
        /// Machine-readable rejection reason when `valid` is false.
        #[serde(default)]
        pub reject_reason: Option<String>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct VerifyResponse {
        pub valid: bool,
        pub expiry: Option<DateTime<Utc>>,
        pub plan: Option<String>,
        /// Addon IDs this license covers. `None` or absent = all addons.
        #[serde(default)]
        pub scope: Option<Vec<String>>,
        /// Machine-readable rejection reason when `valid` is false.
        #[serde(default)]
        pub reject_reason: Option<String>,
    }

    pub(super) static LICENSE_CACHE: Lazy<Mutex<HashMap<String, LicenseInfo>>> =
        Lazy::new(|| Mutex::new(HashMap::new()));

    /// Per-addon license results populated once at startup.
    pub(super) static STARTUP_LICENSE_RESULTS: Lazy<Mutex<HashMap<String, LicenseStatus>>> =
        Lazy::new(|| Mutex::new(HashMap::new()));

    // ── Startup-cache helpers ─────────────────────────────────────────────────────

    /// Store per-addon license results that were resolved at startup.
    pub fn record_startup_results(results: HashMap<String, LicenseStatus>) {
        if let Ok(mut cache) = STARTUP_LICENSE_RESULTS.lock() {
            *cache = results;
        }
    }

    /// Return the startup-time license status for an addon.
    pub fn get_license_status(addon_id: &str) -> Option<LicenseStatus> {
        STARTUP_LICENSE_RESULTS.lock().ok()?.get(addon_id).cloned()
    }

    /// Update the in-memory license status for one or more addons immediately
    /// after a successful verification (no restart required).
    pub fn update_license_status(addon_ids: &[String], status: LicenseStatus) {
        if let Ok(mut cache) = STARTUP_LICENSE_RESULTS.lock() {
            for id in addon_ids {
                cache.insert(id.clone(), status.clone());
            }
        }
    }

    // ─────────────────────────────────────────────────────────────────────────────

    /// Verify a license key against the license server.
    /// If the key has already been verified this session, returns the cached result
    /// without making a network request.
    pub async fn verify_license(key: &str) -> anyhow::Result<LicenseInfo> {
        // Fast path: return cached result if already verified this session.
        if let Ok(cache) = LICENSE_CACHE.lock() {
            if let Some(info) = cache.get(key) {
                tracing::debug!("License key {}... served from cache", &key[..key.len().min(8)]);
                return Ok(info.clone());
            }
        }

        let key_prefix = &key[..key.len().min(8)];
        tracing::info!("Verifying license key {}... against https://miuda.ai/api/verify", key_prefix);

        let client = reqwest::Client::new();
        let resp = client
            .post("https://miuda.ai/api/verify")
            .json(&serde_json::json!({ "license_key": key }))
            .timeout(std::time::Duration::from_secs(5))
            .send()
            .await;

        match resp {
            Ok(response) => {
                let status = response.status();
                tracing::info!("License verify response status: {}", status);
                if status.is_success() {
                    let body = response.text().await?;
                    tracing::debug!("License verify response body: {}", body);
                    let verify_data: VerifyResponse = serde_json::from_str(&body)
                        .map_err(|e| anyhow::anyhow!("Failed to parse verify response: {e}, body: {body}"))?;
                    let info = LicenseInfo {
                        key: key.to_string(),
                        valid: verify_data.valid,
                        expiry: verify_data.expiry,
                        plan: verify_data.plan.unwrap_or_default(),
                        last_checked: Utc::now(),
                        scope: verify_data.scope,
                        reject_reason: verify_data.reject_reason,
                    };

                    if let Ok(mut cache) = LICENSE_CACHE.lock() {
                        cache.insert(key.to_string(), info.clone());
                    }

                    Ok(info)
                } else {
                    let body = response.text().await.unwrap_or_default();
                    tracing::warn!(
                        "License verification failed: status={}, body={}",
                        status,
                        body
                    );
                    anyhow::bail!("Verification failed with status: {}, body: {}", status, body)
                }
            }
            Err(e) => {
                tracing::error!("License verification network error: {}", e);
                if let Ok(cache) = LICENSE_CACHE.lock() {
                    if let Some(info) = cache.get(key) {
                        tracing::warn!("Network error verifying license, using cached info: {}", e);
                        return Ok(info.clone());
                    }
                }
                Err(e.into())
            }
        }
    }

    /// Check if a license key is expired.
    pub fn is_expired(info: &LicenseInfo) -> bool {
        if let Some(expiry) = info.expiry {
            expiry < Utc::now()
        } else {
            false
        }
    }

    /// Get cached license info if available.
    pub fn get_cached_license(key: &str) -> Option<LicenseInfo> {
        LICENSE_CACHE.lock().ok()?.get(key).cloned()
    }

    /// Clear the license cache.
    pub fn clear_cache() {
        if let Ok(mut cache) = LICENSE_CACHE.lock() {
            cache.clear();
        }
    }

    /// Verify license for a specific addon using the license config.
    pub async fn verify_addon_license(
        addon_id: &str,
        license_config: &Option<LicenseConfig>,
    ) -> anyhow::Result<LicenseInfo> {
        let config = license_config
            .as_ref()
            .ok_or_else(|| anyhow::anyhow!("No license configuration found"))?;

        let (key_name, key_value) = config
            .get_license_for_addon(addon_id)
            .ok_or_else(|| anyhow::anyhow!("No license key configured for addon: {}", addon_id))?;

        let info = verify_license(&key_value).await?;

        if !info.valid {
            anyhow::bail!("License is invalid for addon: {}", addon_id);
        }

        if is_expired(&info) {
            anyhow::bail!("License has expired for addon: {}", addon_id);
        }

        if let Some(ref scope) = info.scope {
            if !scope.is_empty() && !scope.contains(&addon_id.to_string()) {
                anyhow::bail!(
                    "License scope {:?} does not cover addon: {}",
                    scope,
                    addon_id
                );
            }
        }

        tracing::info!(
            "License verified for addon {} with key {}: valid={}, expiry={:?}",
            addon_id,
            key_name,
            info.valid,
            info.expiry
        );

        Ok(info)
    }

    /// Check all commercial addons and return their license status.
    pub async fn check_all_addon_licenses(
        addon_ids: &[String],
        license_config: &Option<LicenseConfig>,
    ) -> HashMap<String, LicenseStatus> {
        let mut results = HashMap::new();

        let config = match license_config {
            Some(c) => c,
            None => return results,
        };

        for addon_id in addon_ids {
            let status = match config.get_license_for_addon(addon_id) {
                Some((key_name, key_value)) => match verify_license(&key_value).await {
                    Ok(info) => {
                        let expired = is_expired(&info);
                        LicenseStatus {
                            key_name: key_name.to_string(),
                            valid: info.valid && !expired,
                            expired,
                            expiry: info.expiry.map(|d| d.format("%Y-%m-%d").to_string()),
                            plan: info.plan,
                            is_trial: false,
                            scope: info.scope,
                        }
                    }
                    Err(e) => {
                        tracing::warn!("Failed to verify license for {}: {}", addon_id, e);
                        LicenseStatus {
                            key_name: key_name.to_string(),
                            valid: false,
                            expired: false,
                            expiry: None,
                            plan: "".to_string(),
                            is_trial: false,
                            scope: None,
                        }
                    }
                },
                None => continue,
            };
            results.insert(addon_id.clone(), status);
        }

        results
    }

    /// Check if an addon is allowed to run.
    pub async fn can_enable_addon(
        _addon_id: &str,
        _is_commercial: bool,
        _license_config: &Option<LicenseConfig>,
    ) -> bool {
        true
    }
}

#[cfg(feature = "commerce")]
pub use inner::{
    LicenseInfo, VerifyResponse, can_enable_addon, check_all_addon_licenses, clear_cache,
    get_cached_license, get_license_status, is_expired, record_startup_results,
    update_license_status, verify_addon_license, verify_license,
};

#[cfg(test)]
#[cfg(feature = "commerce")]
mod tests {
    use super::*;
    use chrono::{Duration, Utc};

    #[test]
    fn test_is_expired_with_future_expiry() {
        let info = LicenseInfo {
            key: "test-key".to_string(),
            valid: true,
            expiry: Some(Utc::now() + Duration::days(30)),
            plan: "pro".to_string(),
            last_checked: Utc::now(),
            scope: None,
            reject_reason: None,
        };
        assert!(!is_expired(&info));
    }

    #[test]
    fn test_is_expired_with_past_expiry() {
        let info = LicenseInfo {
            key: "test-key".to_string(),
            valid: true,
            expiry: Some(Utc::now() - Duration::days(1)),
            plan: "pro".to_string(),
            last_checked: Utc::now(),
            scope: None,
            reject_reason: None,
        };
        assert!(is_expired(&info));
    }

    #[test]
    fn test_is_expired_with_no_expiry() {
        let info = LicenseInfo {
            key: "test-key".to_string(),
            valid: true,
            expiry: None,
            plan: "pro".to_string(),
            last_checked: Utc::now(),
            scope: None,
            reject_reason: None,
        };
        assert!(!is_expired(&info));
    }

    #[test]
    fn test_license_cache() {
        clear_cache();

        let info = LicenseInfo {
            key: "test-key".to_string(),
            valid: true,
            expiry: Some(Utc::now() + Duration::days(30)),
            plan: "pro".to_string(),
            last_checked: Utc::now(),
            scope: None,
            reject_reason: None,
        };

        if let Ok(mut cache) = inner::LICENSE_CACHE.lock() {
            cache.insert("test-key".to_string(), info.clone());
        }

        let cached = get_cached_license("test-key");
        assert!(cached.is_some());
        assert_eq!(cached.unwrap().key, "test-key");

        clear_cache();
        let cached = get_cached_license("test-key");
        assert!(cached.is_none());
    }

    mod commerce_tests {
        use crate::config::LicenseConfig;

        #[test]
        fn test_license_config_get_license_for_addon() {
            let mut config = LicenseConfig::default();
            config
                .addons
                .insert("wholesale".to_string(), "enterprise".to_string());
            config
                .keys
                .insert("enterprise".to_string(), "test-key-123".to_string());

            let result = config.get_license_for_addon("wholesale");
            assert!(result.is_some());
            let (key_name, key_value) = result.unwrap();
            assert_eq!(key_name, "enterprise");
            assert_eq!(key_value, "test-key-123");
        }

        #[test]
        fn test_license_config_get_license_for_addon_not_found() {
            let config = LicenseConfig::default();
            let result = config.get_license_for_addon("wholesale");
            assert!(result.is_none());
        }

        #[test]
        fn test_license_config_get_addons_for_key() {
            let mut config = LicenseConfig::default();
            config
                .addons
                .insert("wholesale".to_string(), "enterprise".to_string());
            config
                .addons
                .insert("endpoint-manager".to_string(), "enterprise".to_string());
            config
                .addons
                .insert("voicemail".to_string(), "basic".to_string());

            let addons = config.get_addons_for_key("enterprise");
            assert_eq!(addons.len(), 2);
            assert!(addons.contains(&"wholesale"));
            assert!(addons.contains(&"endpoint-manager"));

            let basic_addons = config.get_addons_for_key("basic");
            assert_eq!(basic_addons.len(), 1);
            assert!(basic_addons.contains(&"voicemail"));
        }

        #[test]
        fn test_license_config_empty() {
            let config = LicenseConfig::default();
            assert!(config.addons.is_empty());
            assert!(config.keys.is_empty());
        }
    }
}