Skip to content

Instantly share code, notes, and snippets.

@ChenYFan
Last active March 24, 2021 11:39
Show Gist options
  • Save ChenYFan/3389cb0331415e23cfca985f33f2140a to your computer and use it in GitHub Desktop.
Save ChenYFan/3389cb0331415e23cfca985f33f2140a to your computer and use it in GitHub Desktop.
将WAF当作KV来存
const WAFKV = {
GET: async function (key) {
let item;
let filters = await WAFKV.FILTERS()
let result = (await filters.json()).result
for (let i = 0; i < result.length; i++) {
if (result[i].id && result[i].id == RULEID) {
item = result[i]
break
}
}
try {
return JSON.parse(unescape(item["expression"].split('$')[1]))[key]
} catch (e) {
return null
}
},
LIST: async function () {
let item;
let filters = await WAFKV.FILTERS()
let result = (await filters.json()).result
for (let i = 0; i < result.length; i++) {
if (result[i].id && result[i].id == RULEID) {
item = result[i]
break
}
}
try {
return (unescape(item["expression"].split('$')[1]))
} catch (e) {
return null
}
},
PUT: async function (key, value) {
let item;
let filters = await WAFKV.FILTERS()
let result = (await filters.json()).result
for (let i = 0; i < result.length; i++) {
if (result[i].id && result[i].id == RULEID) {
item = result[i]
break
}
}
let on = (function () { try { return JSON.parse(item["expression"].split('$')[1]) } catch (p) { return {} } })()
on[key] = value
let expression = `(http.cookie eq "$${escape(await JSON.stringify(on))}$")`
item.expression = expression
const eo = await (await fetch(new Request(`https://api.cloudflare.com/client/v4/zones/${ZONEID}/filters`, {
method: "PUT",
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 Edg/88.0.100.0",
"X-Auth-Email": AUTHEMAIL,
"X-Auth-Key": AUTHKEY,
"Content-Type": "application/json",
},
body: JSON.stringify([item])
}))).json()
return eo.success
},
DELETE: async function (key) {
let item;
let filters = await WAFKV.FILTERS()
let result = (await filters.json()).result
for (let i = 0; i < result.length; i++) {
if (result[i].id && result[i].id == RULEID) {
item = result[i]
break
}
}
let on = (function () { try { return JSON.parse(item["expression"].split('$')[1]) } catch (p) { return {} } })()
delete on[key]
let expression = `(http.cookie eq "$${btoa(await JSON.stringify(on))}$")`
item.expression = expression
const eo = await (await fetch(new Request(`https://api.cloudflare.com/client/v4/zones/${ZONEID}/filters`, {
method: "PUT",
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 Edg/88.0.100.0",
"X-Auth-Email": AUTHEMAIL,
"X-Auth-Key": AUTHKEY,
"Content-Type": "application/json",
},
body: JSON.stringify([item])
}))).json()
return eo.success
},
FILTERS: async function () {
return fetch(new Request(`https://api.cloudflare.com/client/v4/zones/${ZONEID}/filters`, {
method: "GET",
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36 Edg/88.0.100.0",
"X-Auth-Email": AUTHEMAIL,
"X-Auth-Key": AUTHKEY,
"Content-Type": "application/json",
},
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment