added modules.conf support

This commit is contained in:
Alex 2022-08-27 23:34:18 -04:00
parent cf1982a2bd
commit 0ceef499bb
Signed by: technically
GPG Key ID: 46E0A080F69E83F1
1 changed files with 34 additions and 8 deletions

View File

@ -18,18 +18,25 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
async fn check_endpoint_auth(token: &String, kv: KvStore) -> bool {
// Check if the token provided by the endpoint is valid.
match kv.get("authorized_tokens").cache_ttl(300).text().await.unwrap() {
Some(t) => t.split(",")
.any(|authorized_token| authorized_token == token),
None => false,
}
match kv
.get("authorized_tokens")
.cache_ttl(300)
.text()
.await
.unwrap()
{
Some(t) => t
.split(",")
.any(|authorized_token| authorized_token == token),
None => false,
}
}
router
.get_async("/asterisk/: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);
return Response::error("Unauthorized", 401);
}
}
@ -49,7 +56,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
.get_async("/pjsip/: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);
return Response::error("Unauthorized", 401);
}
}
if let Some(hostname) = ctx.param("hostname") {
@ -64,7 +71,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
None => return Response::error("Could not find remote IP", 500),
};
conf = conf.replace("##EXTERNAL_IP##", &remote_ip) + "\nxx";
conf = conf.replace("##EXTERNAL_IP##", &remote_ip) + "\n";
match kv.get("users").cache_ttl(300).text().await? {
Some(users) => {
@ -110,6 +117,25 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
}
Response::error("Bad Request", 400)
})
.get_async("/modules/: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_modules").cache_ttl(300).text().await? {
Some(base) => Response::ok(&base),
None => Response::error(
format!("Could not find modules.conf for {}", hostname),
404,
),
};
}
Response::error("Bad Request", 400)
})
.run(req, env)
.await
}