Reorganized API endpoints and KV keys,

added extensions.conf support
This commit is contained in:
Alex 2022-09-13 21:16:18 -04:00
parent 3878032d80
commit 621d59f55b
Signed by: technically
GPG Key ID: 46E0A080F69E83F1
2 changed files with 93 additions and 7 deletions

20
examples/extensions.conf Normal file
View File

@ -0,0 +1,20 @@
[internal]
exten => _465ZZZXXX,1,Goto(gonk,${EXTEN:3},1)
include => gonk
[external]
[static]
exten => 999999,1,Answer(500)
same => n,Send_MF(4665)
same => n,Playback(silence/1)
same => n,HangUp
[gonk]
include => static
exten => ##EXT##,1,Dial(pjsip/##call##)

View File

@ -54,7 +54,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
} }
Response::error("Bad Request", 400) Response::error("Bad Request", 400)
}) })
.get_async("/pjsip/:hostname/:token", |_req, ctx| async move { .get_async("/asterisk/pjsip/:hostname/:token", |_req, ctx| async move {
if let Some(token) = ctx.param("token") { if let Some(token) = ctx.param("token") {
if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await { if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await {
return Response::error("Unauthorized", 401); return Response::error("Unauthorized", 401);
@ -63,7 +63,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
if let Some(hostname) = ctx.param("hostname") { if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?; let kv = ctx.kv("GONK_NODES")?;
return match kv.get("base_pjsip").cache_ttl(300).text().await? { return match kv.get("base_asterisk_pjsip").cache_ttl(300).text().await? {
Some(base) => { Some(base) => {
let mut conf = base + ""; let mut conf = base + "";
// let remote_ip = // let remote_ip =
@ -78,7 +78,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
Some(users) => { Some(users) => {
let users = users.split(","); let users = users.split(",");
let base_user = let base_user =
match kv.get("base_user").cache_ttl(300).text().await? { match kv.get("base_asterisk_pjsip_user").cache_ttl(300).text().await? {
Some(base) => base, Some(base) => base,
None => { None => {
return Response::error("Could not find user_base", 500) return Response::error("Could not find user_base", 500)
@ -126,7 +126,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
} }
Response::error("Bad Request", 400) Response::error("Bad Request", 400)
}) })
.get_async("/modules/:hostname/:token", |_req, ctx| async move { .get_async("/asterisk/modules/:hostname/:token", |_req, ctx| async move {
if let Some(token) = ctx.param("token") { if let Some(token) = ctx.param("token") {
if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await { if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await {
return Response::error("Unauthorized", 401); return Response::error("Unauthorized", 401);
@ -135,7 +135,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
if let Some(hostname) = ctx.param("hostname") { if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?; let kv = ctx.kv("GONK_NODES")?;
return match kv.get("base_modules").cache_ttl(300).text().await? { return match kv.get("base_asterisk_modules").cache_ttl(300).text().await? {
Some(base) => Response::ok(&base), Some(base) => Response::ok(&base),
None => Response::error( None => Response::error(
format!("Could not find modules.conf for {}", hostname), format!("Could not find modules.conf for {}", hostname),
@ -145,7 +145,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
} }
Response::error("Bad Request", 400) Response::error("Bad Request", 400)
}) })
.get_async("/logger/:hostname/:token", |_req, ctx| async move { .get_async("/asterisk/logger/:hostname/:token", |_req, ctx| async move {
if let Some(token) = ctx.param("token") { if let Some(token) = ctx.param("token") {
if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await { if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await {
return Response::error("Unauthorized", 401); return Response::error("Unauthorized", 401);
@ -154,7 +154,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
if let Some(hostname) = ctx.param("hostname") { if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?; let kv = ctx.kv("GONK_NODES")?;
return match kv.get("base_logger").cache_ttl(300).text().await? { return match kv.get("base_asterisk_logger").cache_ttl(300).text().await? {
Some(base) => Response::ok(&base), Some(base) => Response::ok(&base),
None => { None => {
Response::error(format!("Could not find logger.conf for {}", hostname), 404) Response::error(format!("Could not find logger.conf for {}", hostname), 404)
@ -201,6 +201,72 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
} }
Response::error("Bad Request", 400) Response::error("Bad Request", 400)
}) })
.get_async(
"/asterisk/extensions/:hostname/:token",
|_req, ctx| async move {
if let Some(token) = ctx.param("token") {
if !check_endpoint_auth(token, ctx.kv("GONK_NODES")?).await {
return Response::error("Unauthorized", 401);
}
}
if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?;
return match kv.get("base_asterisk_extensions").cache_ttl(300).text().await? {
Some(base) => {
let mut conf = base + "";
match kv.get("users").cache_ttl(300).text().await? {
Some(users) => {
let users = users.split(",");
let base_user =
match kv.get("base_asterisk_extensions_user").cache_ttl(300).text().await? {
Some(base) => base,
None => {
return Response::error(
"Could not find the per user configuration for extensions",
500,
)
}
};
for u in users {
let mut user = base_user.clone();
let info: UserInfo =
match kv.get(u).cache_ttl(300).json().await? {
Some(info) => info,
None => {
return Response::error(
format!("Could not find user {}", u),
500,
)
}
};
user = user.replace("##CALL##", u);
user = user.replace("##EXT##", &info.ext);
conf = conf + &user;
}
}
None => {
return Response::error(
format!("Could not find gonk users"),
404,
);
}
}
Response::ok(&conf)
}
None => Response::error(
format!("Could not find extensions.conf for {}", hostname),
404,
),
};
}
Response::error("Bad Request", 400)
},
)
.run(req, env) .run(req, env)
.await .await
} }