Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch exception when reading/writing from/to KV #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions pkg/cloudflare/worker/dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6572,6 +6572,24 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
}
}

const getFromKV = async (kv, key) => {
try {
const value = await kv.get(key);
return value;
} catch (e) {
console.log(e)
return null
}
}

const writeToKV = async (kv, key, value) => {
try {
await kv.put(key, value);
} catch (e) {
console.log(e)
}
}

// request ->
// <-captcha
// solved_captcha ->
Expand All @@ -6581,7 +6599,7 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
async fetch(request, env, ctx) {

const doBan = async () => {
return new Response(await env.CROWDSECCFBOUNCERNS.get("BAN_TEMPLATE"), {
return new Response(await getFromKV(env.CROWDSECCFBOUNCERNS, "BAN_TEMPLATE"), {
status: 403,
headers: { "Content-Type": "text/html" }
});
Expand All @@ -6594,15 +6612,15 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
// If it's captcha submission, do the validation and issue a JWT token as a cookie.
// Else return the captcha HTML
const ip = request.headers.get('CF-Connecting-IP');
let turnstileCfg = await env.CROWDSECCFBOUNCERNS.get("TURNSTILE_CONFIG")
let turnstileCfg = await getFromKV(env.CROWDSECCFBOUNCERNS, "TURNSTILE_CONFIG")
if (turnstileCfg == null) {
console.log("No turnstile config found for zone")
return fetch(request)
}
if (typeof turnstileCfg === "string") {
console.log("Converting turnstile config to JSON")
turnstileCfg = JSON.parse(turnstileCfg)
env.CROWDSECCFBOUNCERNS.put("TURNSTILE_CONFIG", turnstileCfg)
writeToKV(env.CROWDSECCFBOUNCERNS, "TURNSTILE_CONFIG", turnstileCfg)
}

if (!turnstileCfg[zoneForThisRequest]) {
Expand Down Expand Up @@ -6702,13 +6720,13 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
const getRemediationForRequest = async (request, env) => {
console.log("Checking for decision against the IP")
const clientIP = request.headers.get("CF-Connecting-IP");
let value = await env.CROWDSECCFBOUNCERNS.get(clientIP);
let value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientIP);
if (value !== null) {
return value
}

console.log("Checking for decision against the IP ranges")
let actionByIPRange = await env.CROWDSECCFBOUNCERNS.get("IP_RANGES");
let actionByIPRange = await getFromKV(env.CROWDSECCFBOUNCERNS, "IP_RANGES");
if (typeof actionByIPRange === "string") {
actionByIPRange = JSON.parse(actionByIPRange)
}
Expand All @@ -6722,15 +6740,15 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
}
// Check for decision against the AS
const clientASN = request.cf.asn.toString();
value = await env.CROWDSECCFBOUNCERNS.get(clientASN);
value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientASN);
if (value !== null) {
return value
}

// Check for decision against the country of the request
const clientCountry = request.cf.country.toLowerCase();
if (clientCountry !== null) {
value = await env.CROWDSECCFBOUNCERNS.get(clientCountry);
value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientCountry);
if (value !== null) {
return value
}
Expand Down
32 changes: 25 additions & 7 deletions pkg/cloudflare/worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ const handleTurnstilePost = async (request, body, turnstile_secret, zoneForThisR
}
}

const getFromKV = async (kv, key) => {
try {
const value = await kv.get(key);
return value;
} catch (e) {
console.log(e)
return null
}
}

const writeToKV = async (kv, key, value) => {
try {
await kv.put(key, value);
} catch (e) {
console.log(e)
}
}

// request ->
// <-captcha
// solved_captcha ->
Expand All @@ -68,7 +86,7 @@ export default {
async fetch(request, env, ctx) {

const doBan = async () => {
return new Response(await env.CROWDSECCFBOUNCERNS.get("BAN_TEMPLATE"), {
return new Response(await getFromKV(env.CROWDSECCFBOUNCERNS, "BAN_TEMPLATE"), {
status: 403,
headers: { "Content-Type": "text/html" }
});
Expand All @@ -81,15 +99,15 @@ export default {
// If it's captcha submission, do the validation and issue a JWT token as a cookie.
// Else return the captcha HTML
const ip = request.headers.get('CF-Connecting-IP');
let turnstileCfg = await env.CROWDSECCFBOUNCERNS.get("TURNSTILE_CONFIG")
let turnstileCfg = await getFromKV(env.CROWDSECCFBOUNCERNS, "TURNSTILE_CONFIG")
if (turnstileCfg == null) {
console.log("No turnstile config found for zone")
return fetch(request)
}
if (typeof turnstileCfg === "string") {
console.log("Converting turnstile config to JSON")
turnstileCfg = JSON.parse(turnstileCfg)
env.CROWDSECCFBOUNCERNS.put("TURNSTILE_CONFIG", turnstileCfg)
writeToKV(env.CROWDSECCFBOUNCERNS, "TURNSTILE_CONFIG", turnstileCfg)
}

if (!turnstileCfg[zoneForThisRequest]) {
Expand Down Expand Up @@ -189,13 +207,13 @@ export default {
const getRemediationForRequest = async (request, env) => {
console.log("Checking for decision against the IP")
const clientIP = request.headers.get("CF-Connecting-IP");
let value = await env.CROWDSECCFBOUNCERNS.get(clientIP);
let value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientIP);
if (value !== null) {
return value
}

console.log("Checking for decision against the IP ranges")
let actionByIPRange = await env.CROWDSECCFBOUNCERNS.get("IP_RANGES");
let actionByIPRange = await getFromKV(env.CROWDSECCFBOUNCERNS, "IP_RANGES");
if (typeof actionByIPRange === "string") {
actionByIPRange = JSON.parse(actionByIPRange)
}
Expand All @@ -209,15 +227,15 @@ export default {
}
// Check for decision against the AS
const clientASN = request.cf.asn.toString();
value = await env.CROWDSECCFBOUNCERNS.get(clientASN);
value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientASN);
if (value !== null) {
return value
}

// Check for decision against the country of the request
const clientCountry = request.cf.country.toLowerCase();
if (clientCountry !== null) {
value = await env.CROWDSECCFBOUNCERNS.get(clientCountry);
value = await getFromKV(env.CROWDSECCFBOUNCERNS, clientCountry);
if (value !== null) {
return value
}
Expand Down
Loading