Support outbound call

This commit is contained in:
2026-03-29 02:07:39 -04:00
parent 06c8bd9919
commit 0483588af5
4 changed files with 95 additions and 33 deletions

View File

@@ -127,3 +127,32 @@ pub fn list_did() ->Result<Vec<Did>, Error> {
Ok(res)
}
pub fn from_pbx(ipaddr: &str) -> Result<bool, Error> {
use crate::schema::dids::dsl::*;
let pattern = format!("%{}%", ipaddr);
let mut conn = connect();
let count = dids
.filter(target.is_not_null().and(target.ilike(&pattern)))
.count()
.get_result::<i64>(&mut conn)?;
Ok(count > 0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_from_pbx() {
assert_eq!(Ok(true), from_pbx("172.16.0.213"));
assert_eq!(Ok(true), from_pbx("172.16.0.215"));
}
#[test]
fn validate_not_from_pbx() {
assert_eq!(Ok(false), from_pbx("192.168.1.1"));
}
}

View File

@@ -46,7 +46,7 @@ pub fn no_service(_did: Did) -> XMLElement {
let no_service_path = format!("{}/no_service.wav", sound_path);
let mut work = XMLElement::new("work");
add_playback(&mut work, &no_service_path);
add_playback_element(&mut work, &no_service_path);
work
}
@@ -58,7 +58,7 @@ pub fn night_mode(_did: Did) -> XMLElement {
let mut work = XMLElement::new("work");
add_playback(&mut work, &night_mode_path);
add_playback_element(&mut work, &night_mode_path);
work
}
@@ -68,7 +68,19 @@ pub fn custom_message(_did: Did) -> XMLElement {
work
}
pub fn add_playback(work: &mut XMLElement ,file_path: &str) {
pub fn outbound(d: &str) -> XMLElement {
let mut work = XMLElement::new("work");
let mut bridge = XMLElement::new("execute");
bridge.add_attribute("application", "bridge");
bridge.add_attribute("data", format!("sofia/gateway/powernet_1/{}", d).as_str());
work.add_child(bridge).unwrap();
work
}
pub fn add_playback_element(work: &mut XMLElement ,file_path: &str) {
let mut answer = XMLElement::new("execute");
answer.add_attribute("application", "answer");
work.add_child(answer).unwrap();
@@ -78,9 +90,37 @@ pub fn add_playback(work: &mut XMLElement ,file_path: &str) {
work.add_child(playback).unwrap();
}
pub fn route_call(did: String) -> Vec<u8> {
let d = db::get_did_by(&did).unwrap();
let did_active = d.active;
pub fn build_work_element(did: &str, caller_ipaddr: &str) -> XMLElement {
if db::from_pbx(&caller_ipaddr).unwrap() == true {
outbound(did)
} else {
let d = db::get_did_by(&did).unwrap();
let work = match d.target_type {
DidTargetType::Url => {
url(d)
},
DidTargetType::Moh => {
moh(d)
},
DidTargetType::ExternalNumber => {
external_number(d)
},
DidTargetType::NoService => {
no_service(d)
},
DidTargetType::NightMode => {
night_mode(d)
},
DidTargetType::CustomMessage => {
custom_message(d)
}
};
work
}
}
pub fn route_call(did: String, caller_ipaddr: String) -> Vec<u8> {
let mut xml = XMLBuilder::new()
.version(XMLVersion::XML1_1)
.encoding("UTF-8".into())
@@ -90,35 +130,24 @@ pub fn route_call(did: String) -> Vec<u8> {
doc.add_attribute("type", "xml/freeswitch-httapi");
let params = XMLElement::new("params");
let mut work = match d.target_type {
DidTargetType::Url => {
url(d)
},
DidTargetType::Moh => {
moh(d)
},
DidTargetType::ExternalNumber => {
external_number(d)
},
DidTargetType::NoService => {
no_service(d)
},
DidTargetType::NightMode => {
night_mode(d)
},
DidTargetType::CustomMessage => {
custom_message(d)
}
};
let mut work = build_work_element(&did, &caller_ipaddr);
let mut hangup = XMLElement::new("execute");
hangup.add_attribute("application", "hangup");
work.add_child(hangup).unwrap();
doc.add_child(params).unwrap();
if did_active == true {
doc.add_child(work).unwrap();
let d = db::get_did_by(&did);
match d {
Ok(d) => {
if d.active == true {
doc.add_child(work).unwrap();
}
},
Err(_) => {
doc.add_child(work).unwrap();
}
}
xml.set_root_element(doc);

View File

@@ -17,7 +17,9 @@ use database::DidTargetType;
#[derive(Debug, Deserialize)]
struct RouteData {
#[serde(rename = "Caller-Destination-Number")]
dest_did: String
dest_did: String,
#[serde(rename = "Caller-Network-Addr")]
caller_ipaddr: String
}
#[derive(Debug, Deserialize)]
@@ -28,9 +30,10 @@ struct JsonDid {
active: bool
}
async fn route_did(data: web::Form<RouteData>) -> impl Responder {
let did = data.dest_did.clone();
let xml = httapi::route_call(did);
async fn route_did(form_data: web::Form<RouteData>) -> impl Responder {
let did = form_data.dest_did.clone();
let caller_ipaddr = form_data.caller_ipaddr.clone();
let xml = httapi::route_call(did, caller_ipaddr);
HttpResponse::Ok()
.content_type(ContentType::xml())
.body(xml)