rustpbx 0.4.4

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
use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use bytes::Bytes;
use chrono::{DateTime, Local, TimeZone};
use sea_orm::DatabaseConnection;
use tracing::{info, warn};

use crate::{
    callrecord::{CallRecord, CallRecordHook},
    config::SipFlowUploadConfig,
    sipflow::SipFlowBackend,
};

pub struct SipFlowUploadHook {
    pub backend: Arc<dyn SipFlowBackend>,
    pub upload_config: SipFlowUploadConfig,
    pub db: Option<DatabaseConnection>,
}

#[async_trait]
impl CallRecordHook for SipFlowUploadHook {
    async fn on_record_completed(&self, record: &mut CallRecord) -> anyhow::Result<()> {
        if record.answer_time.is_none() {
            return Ok(());
        }

        let backend = self.backend.clone();
        let upload_config = self.upload_config.clone();
        let db = self.db.clone();
        let call_id = record.call_id.clone();
        let start = Local.from_utc_datetime(&record.start_time.naive_utc());
        let end = Local.from_utc_datetime(&record.end_time.naive_utc());
        let duration_secs = (record.end_time - record.start_time).num_seconds() as i32;
        let date_prefix = record.start_time.format("%Y%m%d").to_string();

        tokio::spawn(async move {
            crate::callrecord::sipflow_upload::do_upload(
                backend,
                upload_config,
                db,
                &call_id,
                start,
                end,
                duration_secs,
                &date_prefix,
            )
            .await;
        });

        Ok(())
    }
}

async fn do_upload(
    backend: Arc<dyn SipFlowBackend>,
    upload_config: SipFlowUploadConfig,
    db: Option<DatabaseConnection>,
    call_id: &str,
    start: DateTime<Local>,
    end: DateTime<Local>,
    duration_secs: i32,
    date_prefix: &str,
) {
    // Flush the SipFlow batch so all RTP packets are in the SQLite file.
    if let Err(e) = backend.flush().await {
        warn!(call_id, "SipFlowUploadHook: flush failed: {e}");
    }

    let wav_bytes = match backend.query_media(call_id, start, end).await {
        Ok(b) => b,
        Err(e) => {
            warn!(call_id, "SipFlowUploadHook: query_media failed: {e}");
            return;
        }
    };

    if wav_bytes.is_empty() {
        return;
    }

    // Path inside the storage target: YYYYMMDD/<call_id>.wav
    let key = format!("{}/{}.wav", date_prefix, call_id);

    let wav_len = wav_bytes.len();
    let url_result = match &upload_config {
        SipFlowUploadConfig::S3 {
            vendor,
            bucket,
            region,
            access_key,
            secret_key,
            endpoint,
            root,
        } => {
            let full_key = if root.is_empty() {
                key.clone()
            } else {
                format!("{}/{}", root.trim_end_matches('/'), key)
            };
            upload_s3(
                vendor, bucket, region, access_key, secret_key, endpoint, &full_key, wav_bytes,
            )
            .await
            .map(|_| {
                format!(
                    "{}/{}/{}",
                    endpoint.trim_end_matches('/'),
                    bucket.trim_matches('/'),
                    full_key.trim_start_matches('/')
                )
            })
        }
        SipFlowUploadConfig::Http { url, headers } => {
            upload_http(url, headers.as_ref(), call_id, wav_bytes).await
        }
    };

    match url_result {
        Ok(url) => {
            info!(
                call_id,
                url,
                bytes = wav_len,
                "SipFlowUploadHook: recording uploaded"
            );
            if let Some(ref db) = db {
                if let Err(e) = crate::models::call_record::update_recording_url(
                    db,
                    call_id,
                    &url,
                    duration_secs,
                )
                .await
                {
                    warn!(
                        call_id,
                        "SipFlowUploadHook: failed to update recording_url: {e}"
                    );
                }
            }
        }
        Err(e) => {
            warn!(call_id, "SipFlowUploadHook: upload failed: {e}");
        }
    }
}

// ── Internal upload helpers ───────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
async fn upload_s3(
    vendor: &crate::config::S3Vendor,
    bucket: &str,
    region: &str,
    access_key: &str,
    secret_key: &str,
    endpoint: &str,
    key: &str,
    data: Vec<u8>,
) -> Result<()> {
    use crate::storage::{Storage, StorageConfig};
    let storage = Storage::new(&StorageConfig::S3 {
        vendor: vendor.clone(),
        bucket: bucket.to_string(),
        region: region.to_string(),
        access_key: access_key.to_string(),
        secret_key: secret_key.to_string(),
        endpoint: Some(endpoint.to_string()),
        prefix: None,
    })?;
    storage.write(key, Bytes::from(data)).await?;
    Ok(())
}

async fn upload_http(
    url: &str,
    headers: Option<&std::collections::HashMap<String, String>>,
    call_id: &str,
    data: Vec<u8>,
) -> Result<String> {
    let client = reqwest::Client::new();
    let file_name = format!("{}.wav", call_id);
    let part = reqwest::multipart::Part::bytes(data)
        .file_name(file_name)
        .mime_str("audio/wav")?;
    let form = reqwest::multipart::Form::new().part("recording", part);

    let mut req = client.post(url).multipart(form);
    if let Some(h) = headers {
        for (k, v) in h {
            req = req.header(k.as_str(), v.as_str());
        }
    }
    let response = req.send().await?;
    if response.status().is_success() {
        let body = response.text().await.unwrap_or_default();
        // Return a URL: use the posted URL or parse from response if it looks like one.
        let recording_url = if body.starts_with("http") {
            body.trim().to_string()
        } else {
            url.to_string()
        };
        Ok(recording_url)
    } else {
        Err(anyhow::anyhow!(
            "HTTP upload failed: {} – {}",
            response.status(),
            response.text().await.unwrap_or_default()
        ))
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sipflow::{SipFlowBackend, SipFlowItem};
    use chrono::{DateTime, Local};

    struct MockBackend {
        /// WAV bytes returned by query_media
        media: Vec<u8>,
        /// Track flush call count
        flush_count: std::sync::Arc<std::sync::atomic::AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl SipFlowBackend for MockBackend {
        fn record(&self, _call_id: &str, _item: SipFlowItem) -> anyhow::Result<()> {
            Ok(())
        }
        async fn flush(&self) -> anyhow::Result<()> {
            self.flush_count
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            Ok(())
        }
        async fn query_flow(
            &self,
            _call_id: &str,
            _start: DateTime<Local>,
            _end: DateTime<Local>,
        ) -> anyhow::Result<Vec<SipFlowItem>> {
            Ok(vec![])
        }
        async fn query_media_stats(
            &self,
            _call_id: &str,
            _start: DateTime<Local>,
            _end: DateTime<Local>,
        ) -> anyhow::Result<Vec<(i32, String, usize)>> {
            Ok(vec![])
        }
        async fn query_media(
            &self,
            _call_id: &str,
            _start: DateTime<Local>,
            _end: DateTime<Local>,
        ) -> anyhow::Result<Vec<u8>> {
            Ok(self.media.clone())
        }
    }

    fn make_record() -> CallRecord {
        use crate::callrecord::CallDetails;
        let now = chrono::Utc::now();
        let mut record = CallRecord::default();
        record.call_id = "test-call-id".to_string();
        record.start_time = now - chrono::Duration::seconds(30);
        record.answer_time = Some(now - chrono::Duration::seconds(20));
        record.end_time = now;
        record.caller = "alice".to_string();
        record.callee = "bob".to_string();
        record.details = CallDetails {
            direction: "inbound".to_string(),
            status: "completed".to_string(),
            ..Default::default()
        };
        record
    }

    #[tokio::test]
    async fn test_hook_returns_immediately() {
        let flush_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let hook = SipFlowUploadHook {
            backend: Arc::new(MockBackend {
                media: vec![],
                flush_count: flush_count.clone(),
            }),
            upload_config: SipFlowUploadConfig::Http {
                url: "http://localhost:9999/upload".to_string(),
                headers: None,
            },
            db: None,
        };
        let mut record = make_record();
        // Hook returns immediately without blocking
        let start = std::time::Instant::now();
        hook.on_record_completed(&mut record).await.unwrap();
        let elapsed = start.elapsed();
        // Should return in well under 100ms (no actual work done synchronously)
        assert!(elapsed.as_millis() < 100, "hook blocked for {elapsed:?}");
        // URL should NOT be set (the background task would set it later)
        assert!(record.details.recording_url.is_none());
        // Give background task time to run, then check flush was called
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        assert_eq!(flush_count.load(std::sync::atomic::Ordering::Relaxed), 1);
    }

    #[test]
    fn test_upload_config_parse_s3() {
        let toml_str = r#"
type = "local"
root = "/var/sipflow"

[upload]
type = "s3"
vendor = "aws"
bucket = "my-recordings"
region = "us-east-1"
access_key = "AKID"
secret_key = "SECRET"
endpoint = "https://s3.amazonaws.com"
root = "recordings"
"#;
        let cfg: crate::config::SipFlowConfig =
            toml::from_str(toml_str).expect("should parse s3 upload config");
        match cfg {
            crate::config::SipFlowConfig::Local { upload, .. } => {
                let upload = upload.expect("upload should be set");
                match upload {
                    SipFlowUploadConfig::S3 { bucket, region, .. } => {
                        assert_eq!(bucket, "my-recordings");
                        assert_eq!(region, "us-east-1");
                    }
                    _ => panic!("expected S3 variant"),
                }
            }
            _ => panic!("expected Local sipflow config"),
        }
    }

    #[test]
    fn test_upload_config_parse_http() {
        let toml_str = r#"
type = "local"
root = "/var/sipflow"

[upload]
type = "http"
url = "https://example.com/recordings"
"#;
        let cfg: crate::config::SipFlowConfig =
            toml::from_str(toml_str).expect("should parse http upload config");
        match cfg {
            crate::config::SipFlowConfig::Local { upload, .. } => {
                let upload = upload.expect("upload should be set");
                match upload {
                    SipFlowUploadConfig::Http { url, .. } => {
                        assert_eq!(url, "https://example.com/recordings");
                    }
                    _ => panic!("expected Http variant"),
                }
            }
            _ => panic!("expected Local sipflow config"),
        }
    }

    #[test]
    fn test_upload_config_parse_remote_http() {
        let toml_str = r#"
type = "remote"
udp_addr = "127.0.0.1:3000"
http_addr = "http://127.0.0.1:3001"

[upload]
type = "http"
url = "https://example.com/recordings"
"#;
        let cfg: crate::config::SipFlowConfig =
            toml::from_str(toml_str).expect("should parse remote sipflow with upload");
        match cfg {
            crate::config::SipFlowConfig::Remote { upload, .. } => {
                let upload = upload.expect("upload should be set");
                match upload {
                    SipFlowUploadConfig::Http { url, .. } => {
                        assert_eq!(url, "https://example.com/recordings");
                    }
                    _ => panic!("expected Http variant"),
                }
            }
            _ => panic!("expected Remote sipflow config"),
        }
    }

    #[test]
    fn test_sipflow_config_default_no_upload() {
        let toml_str = r#"
type = "local"
root = "/var/sipflow"
"#;
        let cfg: crate::config::SipFlowConfig =
            toml::from_str(toml_str).expect("should parse sipflow without upload");
        match cfg {
            crate::config::SipFlowConfig::Local { upload, .. } => {
                assert!(upload.is_none(), "upload should default to None");
            }
            _ => panic!("expected Local sipflow config"),
        }
    }

    #[test]
    fn test_sipflow_remote_config_default_no_upload() {
        let toml_str = r#"
type = "remote"
udp_addr = "127.0.0.1:3000"
http_addr = "http://127.0.0.1:3001"
"#;
        let cfg: crate::config::SipFlowConfig =
            toml::from_str(toml_str).expect("should parse remote sipflow without upload");
        match cfg {
            crate::config::SipFlowConfig::Remote { upload, .. } => {
                assert!(upload.is_none(), "upload should default to None");
            }
            _ => panic!("expected Remote sipflow config"),
        }
    }
}