rustpbx 0.4.9

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
use crate::console::{ConsoleState, middleware::AuthRequired};
use crate::proxy::active_call_registry::ActiveProxyCallRegistry;
use crate::proxy::proxy_call::sip_session::SessionSnapshot;
use axum::extract::{Path as AxumPath, Query, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;

const DEFAULT_ACTIVE_CALL_LIMIT: usize = 50;
const MAX_ACTIVE_CALL_LIMIT: usize = 500;
#[cfg(feature = "commerce")]
const CLUSTER_FORWARD_TIMEOUT_SECS: u64 = 10;

pub fn urls() -> Router<Arc<ConsoleState>> {
    Router::new()
        .route("/calls/active", get(list_active_calls))
        .route("/calls/active/{session_id}", get(show_active_call))
        .route(
            "/calls/active/{session_id}/commands",
            post(dispatch_call_command),
        )
}

pub fn api_urls() -> Router<Arc<ConsoleState>> {
    urls()
}

#[derive(Default, Deserialize)]
pub struct ActiveCallListQuery {
    #[serde(default)]
    pub limit: Option<usize>,
}

#[derive(Debug, Clone, Deserialize, serde::Serialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum CallCommandPayload {
    Hangup {
        reason: Option<String>,
        code: Option<u16>,
        initiator: Option<String>,
    },
    #[serde(alias = "accept")]
    Accept {
        callee: Option<String>,
        sdp: Option<String>,
    },
    Transfer {
        target: String,
    },
    Mute {
        track_id: String,
    },
    Unmute {
        track_id: String,
    },
    Play {
        source: ConsoleMediaSource,
        #[serde(default)]
        leg_id: Option<String>,
        #[serde(default)]
        interrupt_on_dtmf: bool,
        #[serde(default)]
        loop_playback: bool,
    },
    StopPlayback {
        #[serde(default)]
        leg_id: Option<String>,
    },
}

#[derive(Debug, Clone, Deserialize, serde::Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ConsoleMediaSource {
    File { path: String },
    Url { url: String },
    Silence,
    Tone { frequency: u32, duration_ms: u32 },
}

pub async fn list_active_calls(
    State(state): State<Arc<ConsoleState>>,
    AuthRequired(_): AuthRequired,
    Query(query): Query<ActiveCallListQuery>,
) -> Response {
    list_active_calls_inner(&state, &query).await
}

pub async fn show_active_call(
    State(state): State<Arc<ConsoleState>>,
    AuthRequired(_): AuthRequired,
    AxumPath(session_id): AxumPath<String>,
) -> Response {
    show_active_call_inner(&state, &session_id).await
}

pub async fn dispatch_call_command(
    State(state): State<Arc<ConsoleState>>,
    AuthRequired(_): AuthRequired,
    AxumPath(session_id): AxumPath<String>,
    Json(payload): Json<CallCommandPayload>,
) -> Response {
    dispatch_call_command_inner(&state, &session_id, payload).await
}

pub async fn list_active_calls_inner(
    state: &Arc<ConsoleState>,
    query: &ActiveCallListQuery,
) -> Response {
    let limit = query
        .limit
        .unwrap_or(DEFAULT_ACTIVE_CALL_LIMIT)
        .clamp(1, MAX_ACTIVE_CALL_LIMIT);

    let mut all_entries: Vec<serde_json::Value> = Vec::new();

    if let Some(server) = state.sip_server() {
        let registry = server.active_call_registry.clone();
        let entries = registry.list_recent(limit);
        for entry in entries {
            let session_id = entry.session_id.clone();
            all_entries.push(json!({
                "node": "local",
                "meta": entry,
                "state": snapshot_for(&registry, &session_id),
            }));
        }
    }

    #[cfg(feature = "commerce")]
    {
        let peer_entries = fetch_peer_calls(state, limit).await;
        all_entries.extend(peer_entries);
    }

    all_entries.sort_by(|a, b| {
        let ts_a = a
            .get("meta")
            .and_then(|m| m.get("started_at"))
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let ts_b = b
            .get("meta")
            .and_then(|m| m.get("started_at"))
            .and_then(|v| v.as_str())
            .unwrap_or("");
        ts_b.cmp(ts_a)
    });
    all_entries.truncate(limit);

    Json(json!({ "data": all_entries })).into_response()
}

pub async fn show_active_call_inner(state: &Arc<ConsoleState>, session_id: &str) -> Response {
    if let Some(server) = state.sip_server() {
        let registry = server.active_call_registry.clone();

        if let Some(handle) = registry.get_handle(session_id) {
            return Json(json!({ "data": json!({
                "meta": registry.get(session_id),
                "state": handle.snapshot(),
            }) }))
            .into_response();
        }
    }

    #[cfg(feature = "commerce")]
    {
        if let Some(resp) = query_session_from_peers(state, session_id).await {
            return resp;
        }
    }

    (
        StatusCode::NOT_FOUND,
        Json(json!({ "message": "Call not found" })),
    )
        .into_response()
}

pub async fn dispatch_call_command_inner(
    state: &Arc<ConsoleState>,
    session_id: &str,
    payload: CallCommandPayload,
) -> Response {
    if let Some(server) = state.sip_server() {
        let registry = server.active_call_registry.clone();

        if registry.get_handle(session_id).is_some() {
            use crate::call::runtime::dispatch_console_command;

            return match dispatch_console_command(&registry, session_id, payload) {
                Ok(result) => {
                    if result.success {
                        let mut resp = json!({ "message": "Command dispatched" });
                        if let Some(data) = result.data {
                            resp.as_object_mut().unwrap().insert("data".into(), data);
                        }
                        Json(resp).into_response()
                    } else {
                        (
                            StatusCode::BAD_REQUEST,
                            Json(json!({ "message": result.message })),
                        )
                            .into_response()
                    }
                }
                Err(e) => (
                    StatusCode::CONFLICT,
                    Json(json!({ "message": format!("Failed to deliver command: {}", e) })),
                )
                    .into_response(),
            };
        }
    }

    #[cfg(feature = "commerce")]
    {
        let resp = forward_command_to_peers(state, session_id, &payload).await;
        if resp.is_some() {
            return resp.unwrap();
        }
    }

    (
        StatusCode::NOT_FOUND,
        Json(json!({ "message": "Call not found" })),
    )
        .into_response()
}

fn snapshot_for(
    registry: &Arc<ActiveProxyCallRegistry>,
    session_id: &str,
) -> Option<SessionSnapshot> {
    registry
        .get_handle(session_id)
        .and_then(|handle| handle.snapshot())
}

#[cfg(feature = "commerce")]
fn get_cluster_peers(state: &ConsoleState) -> Option<Vec<crate::config::ClusterPeer>> {
    state
        .app_state()
        .and_then(|app| app.config().cluster.as_ref().map(|c| c.peers.clone()))
}

#[cfg(feature = "commerce")]
fn get_ami_path(state: &ConsoleState) -> String {
    state
        .app_state()
        .map(|app| {
            app.config()
                .proxy
                .ami_path
                .clone()
                .unwrap_or_else(|| "/ami/v1".to_string())
        })
        .unwrap_or_else(|| "/ami/v1".to_string())
}

#[cfg(feature = "commerce")]
async fn forward_command_to_peers(
    state: &ConsoleState,
    session_id: &str,
    payload: &CallCommandPayload,
) -> Option<Response> {
    let peers = get_cluster_peers(state)?;
    if peers.is_empty() {
        return None;
    }

    let ami_path = get_ami_path(state);
    let body = json!({
        "session_id": session_id,
        "payload": payload,
    });

    let client = reqwest::Client::new();
    let mut handles: Vec<tokio::task::JoinHandle<Option<Response>>> = Vec::new();

    for peer in &peers {
        let url = format!(
            "http://{}:{}{}/cluster/dispatch_command",
            peer.addr, peer.ami_port, ami_path
        );
        let client = client.clone();
        let body = body.clone();

        handles.push(tokio::spawn(async move {
            let opts = crate::http_util::HttpFetchOptions::new()
                .with_timeout(std::time::Duration::from_secs(CLUSTER_FORWARD_TIMEOUT_SECS));
            let req = client.post(&url).json(&body);
            match crate::http_util::execute_request(req, &opts.headers, opts.timeout).await {
                Ok(resp) => {
                    let status = resp.status();
                    if status == StatusCode::NOT_FOUND {
                        return None;
                    }
                    let body_text = resp.text().await.ok()?;
                    Some((status, Json(body_text)).into_response())
                }
                Err(_) => None,
            }
        }));
    }

    for handle in handles {
        match handle.await {
            Ok(Some(resp)) => return Some(resp),
            Ok(None) => continue,
            Err(_) => continue,
        }
    }

    None
}

#[cfg(feature = "commerce")]
async fn query_session_from_peers(state: &ConsoleState, session_id: &str) -> Option<Response> {
    let peers = get_cluster_peers(state)?;
    if peers.is_empty() {
        return None;
    }

    let ami_path = get_ami_path(state);
    let client = reqwest::Client::new();
    let mut handles: Vec<tokio::task::JoinHandle<Option<Response>>> = Vec::new();

    for peer in &peers {
        let url = format!(
            "http://{}:{}{}/cluster/show_session/{}",
            peer.addr, peer.ami_port, ami_path, session_id
        );
        let client = client.clone();

        handles.push(tokio::spawn(async move {
            let opts = crate::http_util::HttpFetchOptions::new()
                .with_timeout(std::time::Duration::from_secs(CLUSTER_FORWARD_TIMEOUT_SECS));
            let req = client.get(&url);
            match crate::http_util::execute_request(req, &opts.headers, opts.timeout).await {
                Ok(resp) => {
                    let status = resp.status();
                    if status == StatusCode::NOT_FOUND {
                        return None;
                    }
                    let body_text = resp.text().await.ok()?;
                    Some((status, Json(body_text)).into_response())
                }
                Err(_) => None,
            }
        }));
    }

    for handle in handles {
        match handle.await {
            Ok(Some(resp)) => return Some(resp),
            Ok(None) => continue,
            Err(_) => continue,
        }
    }

    None
}

#[cfg(feature = "commerce")]
async fn fetch_peer_calls(state: &ConsoleState, limit: usize) -> Vec<serde_json::Value> {
    let peers = match get_cluster_peers(state) {
        Some(p) if !p.is_empty() => p,
        _ => return Vec::new(),
    };

    let ami_path = get_ami_path(state);
    let client = reqwest::Client::new();
    let mut handles: Vec<tokio::task::JoinHandle<Vec<serde_json::Value>>> = Vec::new();

    for peer in &peers {
        let url = format!(
            "http://{}:{}{}/cluster/list_calls?limit={}",
            peer.addr, peer.ami_port, ami_path, limit
        );
        let client = client.clone();
        let peer_label = format!("{}:{}", peer.addr, peer.sip_port);

        handles.push(tokio::spawn(async move {
            let opts = crate::http_util::HttpFetchOptions::new()
                .with_timeout(std::time::Duration::from_secs(CLUSTER_FORWARD_TIMEOUT_SECS));
            let req = client.get(&url);
            match crate::http_util::execute_request(req, &opts.headers, opts.timeout).await {
                Ok(resp) => {
                    if let Ok(body) = resp.json::<serde_json::Value>().await {
                        if let Some(data) = body.get("data").and_then(|d| d.as_array()) {
                            return data
                                .iter()
                                .map(|item| {
                                    let mut item = item.clone();
                                    if let Some(obj) = item.as_object_mut() {
                                        obj.insert(
                                            "node".to_string(),
                                            serde_json::Value::String(peer_label.clone()),
                                        );
                                    }
                                    item
                                })
                                .collect();
                        }
                    }
                    Vec::new()
                }
                Err(_) => Vec::new(),
            }
        }));
    }

    let mut results = Vec::new();
    for handle in handles {
        if let Ok(entries) = handle.await {
            results.extend(entries);
        }
    }
    results
}