Support outbound call
This commit is contained in:
@@ -29,6 +29,7 @@
|
|||||||
<node type="allow" cidr="66.234.186.78/32"/>
|
<node type="allow" cidr="66.234.186.78/32"/>
|
||||||
<node type="allow" cidr="66.234.186.87/32"/>
|
<node type="allow" cidr="66.234.186.87/32"/>
|
||||||
<node type="allow" cidr="66.234.186.88/32"/>
|
<node type="allow" cidr="66.234.186.88/32"/>
|
||||||
|
<node type="allow" cidr="172.16.0.213/24"/>
|
||||||
</list>
|
</list>
|
||||||
|
|
||||||
</network-lists>
|
</network-lists>
|
||||||
|
|||||||
@@ -127,3 +127,32 @@ pub fn list_did() ->Result<Vec<Did>, Error> {
|
|||||||
|
|
||||||
Ok(res)
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ pub fn no_service(_did: Did) -> XMLElement {
|
|||||||
let no_service_path = format!("{}/no_service.wav", sound_path);
|
let no_service_path = format!("{}/no_service.wav", sound_path);
|
||||||
let mut work = XMLElement::new("work");
|
let mut work = XMLElement::new("work");
|
||||||
|
|
||||||
add_playback(&mut work, &no_service_path);
|
add_playback_element(&mut work, &no_service_path);
|
||||||
|
|
||||||
work
|
work
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ pub fn night_mode(_did: Did) -> XMLElement {
|
|||||||
|
|
||||||
let mut work = XMLElement::new("work");
|
let mut work = XMLElement::new("work");
|
||||||
|
|
||||||
add_playback(&mut work, &night_mode_path);
|
add_playback_element(&mut work, &night_mode_path);
|
||||||
|
|
||||||
work
|
work
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,19 @@ pub fn custom_message(_did: Did) -> XMLElement {
|
|||||||
work
|
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");
|
let mut answer = XMLElement::new("execute");
|
||||||
answer.add_attribute("application", "answer");
|
answer.add_attribute("application", "answer");
|
||||||
work.add_child(answer).unwrap();
|
work.add_child(answer).unwrap();
|
||||||
@@ -78,9 +90,37 @@ pub fn add_playback(work: &mut XMLElement ,file_path: &str) {
|
|||||||
work.add_child(playback).unwrap();
|
work.add_child(playback).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn route_call(did: String) -> Vec<u8> {
|
pub fn build_work_element(did: &str, caller_ipaddr: &str) -> XMLElement {
|
||||||
let d = db::get_did_by(&did).unwrap();
|
if db::from_pbx(&caller_ipaddr).unwrap() == true {
|
||||||
let did_active = d.active;
|
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()
|
let mut xml = XMLBuilder::new()
|
||||||
.version(XMLVersion::XML1_1)
|
.version(XMLVersion::XML1_1)
|
||||||
.encoding("UTF-8".into())
|
.encoding("UTF-8".into())
|
||||||
@@ -90,35 +130,24 @@ pub fn route_call(did: String) -> Vec<u8> {
|
|||||||
doc.add_attribute("type", "xml/freeswitch-httapi");
|
doc.add_attribute("type", "xml/freeswitch-httapi");
|
||||||
|
|
||||||
let params = XMLElement::new("params");
|
let params = XMLElement::new("params");
|
||||||
|
let mut work = build_work_element(&did, &caller_ipaddr);
|
||||||
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 hangup = XMLElement::new("execute");
|
let mut hangup = XMLElement::new("execute");
|
||||||
hangup.add_attribute("application", "hangup");
|
hangup.add_attribute("application", "hangup");
|
||||||
work.add_child(hangup).unwrap();
|
work.add_child(hangup).unwrap();
|
||||||
|
|
||||||
doc.add_child(params).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);
|
xml.set_root_element(doc);
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ use database::DidTargetType;
|
|||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct RouteData {
|
struct RouteData {
|
||||||
#[serde(rename = "Caller-Destination-Number")]
|
#[serde(rename = "Caller-Destination-Number")]
|
||||||
dest_did: String
|
dest_did: String,
|
||||||
|
#[serde(rename = "Caller-Network-Addr")]
|
||||||
|
caller_ipaddr: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -28,9 +30,10 @@ struct JsonDid {
|
|||||||
active: bool
|
active: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn route_did(data: web::Form<RouteData>) -> impl Responder {
|
async fn route_did(form_data: web::Form<RouteData>) -> impl Responder {
|
||||||
let did = data.dest_did.clone();
|
let did = form_data.dest_did.clone();
|
||||||
let xml = httapi::route_call(did);
|
let caller_ipaddr = form_data.caller_ipaddr.clone();
|
||||||
|
let xml = httapi::route_call(did, caller_ipaddr);
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.content_type(ContentType::xml())
|
.content_type(ContentType::xml())
|
||||||
.body(xml)
|
.body(xml)
|
||||||
|
|||||||
Reference in New Issue
Block a user