added modules.conf support
This commit is contained in:
parent
cf1982a2bd
commit
0ceef499bb
34
src/lib.rs
34
src/lib.rs
@ -18,11 +18,18 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
|
|||||||
|
|
||||||
async fn check_endpoint_auth(token: &String, kv: KvStore) -> bool {
|
async fn check_endpoint_auth(token: &String, kv: KvStore) -> bool {
|
||||||
// Check if the token provided by the endpoint is valid.
|
// Check if the token provided by the endpoint is valid.
|
||||||
match kv.get("authorized_tokens").cache_ttl(300).text().await.unwrap() {
|
match kv
|
||||||
Some(t) => t.split(",")
|
.get("authorized_tokens")
|
||||||
|
.cache_ttl(300)
|
||||||
|
.text()
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
{
|
||||||
|
Some(t) => t
|
||||||
|
.split(",")
|
||||||
.any(|authorized_token| authorized_token == token),
|
.any(|authorized_token| authorized_token == token),
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
router
|
router
|
||||||
@ -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),
|
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? {
|
match kv.get("users").cache_ttl(300).text().await? {
|
||||||
Some(users) => {
|
Some(users) => {
|
||||||
@ -110,6 +117,25 @@ 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 {
|
||||||
|
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)
|
.run(req, env)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user