added pjsip generation
This commit is contained in:
+69
-9
@@ -1,21 +1,32 @@
|
||||
use serde;
|
||||
use worker::*;
|
||||
|
||||
#[event(fetch)]
|
||||
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
|
||||
|
||||
// Create an instance of the Router, which can use parameters (/user/:name) or wildcard values
|
||||
// (/file/*pathname). Alternatively, use `Router::with_data(D)` and pass in arbitrary data for
|
||||
// routes to access and share using the `ctx.data()` method.
|
||||
let router = Router::new();
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct UserInfo {
|
||||
ext: String,
|
||||
passwd: String,
|
||||
comment: String,
|
||||
created_at: String,
|
||||
}
|
||||
|
||||
router
|
||||
.get_async("/asterisk/:hostname", |_req, ctx| async move {
|
||||
if let Some(hostname) = ctx.param("hostname") {
|
||||
let kv = ctx.kv("GONK_NODES")?;
|
||||
|
||||
return match kv.get("asterisk_base").cache_ttl(300).text().await? {
|
||||
Some(a) => Response::ok(&a),
|
||||
None => Response::error(format!("Could not find asterisk.conf for {}", hostname), 404),
|
||||
return match kv.get("base_asterisk").cache_ttl(300).text().await? {
|
||||
Some(a) => Response::ok(&a),
|
||||
None => Response::error(
|
||||
format!("Could not find asterisk.conf for {}", hostname),
|
||||
404,
|
||||
),
|
||||
};
|
||||
}
|
||||
Response::error("Bad Request", 400)
|
||||
@@ -24,12 +35,61 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
|
||||
if let Some(hostname) = ctx.param("hostname") {
|
||||
let kv = ctx.kv("GONK_NODES")?;
|
||||
|
||||
return match kv.get("pjspi_base").cache_ttl(300).text().await? {
|
||||
Some(a) => Response::ok(&a),
|
||||
None => Response::error(format!("Could not find pjsip.conf for {}", hostname), 404),
|
||||
return match kv.get("base_pjsip").cache_ttl(300).text().await? {
|
||||
Some(base) => {
|
||||
let mut conf = base + "";
|
||||
let remote_ip =
|
||||
match _req.headers().get("CF-Connecting-IP").unwrap_or_default() {
|
||||
Some(ip) => ip,
|
||||
None => return Response::error("Could not find remote IP", 500),
|
||||
};
|
||||
|
||||
conf = conf.replace("##EXTERNAL_IP##", &remote_ip) + "\nxx";
|
||||
|
||||
match kv.get("users").cache_ttl(300).text().await? {
|
||||
Some(users) => {
|
||||
let users = users.split(",");
|
||||
let base_user =
|
||||
match kv.get("base_user").cache_ttl(300).text().await? {
|
||||
Some(base) => base,
|
||||
None => {
|
||||
return Response::error("Could not find user_base", 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);
|
||||
user = user.replace("##PASSWD##", &info.passwd);
|
||||
|
||||
conf = conf + &user;
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Response::error(format!("Could not find gonk users"), 404);
|
||||
}
|
||||
}
|
||||
Response::ok(&conf)
|
||||
}
|
||||
None => {
|
||||
Response::error(format!("Could not find pjsip.conf for {}", hostname), 404)
|
||||
}
|
||||
};
|
||||
}
|
||||
Response::error("Bad Request", 400)
|
||||
})
|
||||
.run(req, env).await
|
||||
}
|
||||
.run(req, env)
|
||||
.await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user