diff --git a/.gitignore b/.gitignore index dedc45971..ee0f87c07 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -package-lock.json -_nuxt/b46e75617207ae5013d0.js -node_modules -package.json +node_modules/.bin/* +node_modules/.mf/* +.wrangler/* +data/* \ No newline at end of file diff --git a/admin-ImgTC.html b/admin-ImgTC.html index 9732e4009..9aefb5811 100644 --- a/admin-ImgTC.html +++ b/admin-ImgTC.html @@ -7,6 +7,7 @@ + +
diff --git a/block-img.html b/block-img.html index b7872eb49..427fdf5ba 100644 --- a/block-img.html +++ b/block-img.html @@ -10,8 +10,7 @@ - - + { - let moderate_data = await response.json(); - console.log(moderate_data) - console.log("---env.img_url---") - console.log(env.img_url=="true") - if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == ""){}else{ - //add image to kv - await env.img_url.put(params.id, "",{ - metadata: { ListType: "None", Label: moderate_data.rating_label,TimeStamp: time }, - }); - } - if (moderate_data.rating_label=="adult"){ - return Response.redirect(url.origin+"/block-img.html", 302) - }}); - + } else { + await fetch(`https://api.moderatecontent.com/moderate/?key=` + apikey + `&url=https://telegra.ph/` + url.pathname + url.search). + then(async (response) => { + let moderate_data = await response.json(); + console.log(moderate_data) + console.log("---env.img_url---") + console.log(env.img_url == "true") + if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == "") { } else { + //add image to kv + await env.img_url.put(params.id, "", { + metadata: { ListType: "None", Label: moderate_data.rating_label, TimeStamp: time }, + }); + } + if (moderate_data.rating_label == "adult") { + return Response.redirect(url.origin + "/block-img.html", 302) + } + }); + } } return response; - }); + }); return response; - - } - \ No newline at end of file + +} diff --git a/functions/file/_middleware.js b/functions/file/_middleware.js new file mode 100644 index 000000000..ac4dfbfea --- /dev/null +++ b/functions/file/_middleware.js @@ -0,0 +1,3 @@ +import { errorHandling, telemetryData } from '../utils/middleware'; + +export const onRequest = [errorHandling, telemetryData]; \ No newline at end of file diff --git a/functions/upload.js b/functions/upload.js index 82bc4c747..3c6c6ad01 100644 --- a/functions/upload.js +++ b/functions/upload.js @@ -1,19 +1,21 @@ +import { errorHandling, telemetryData } from "./utils/middleware"; export async function onRequestPost(context) { // Contents of context object - const { + const { request, // same as existing Worker API - env, // same as existing Worker API - params, // if filename includes [id] or [[path]] - waitUntil, // same as ctx.waitUntil in existing Worker API - next, // used for middleware or to fetch assets - data, // arbitrary space for passing data between middlewares - } = context; - context.request - const url = new URL(request.url); - const response = fetch('https://telegra.ph/' + url.pathname + url.search, { - method: request.method, - headers: request.headers, - body: request.body, - }); + env, // same as existing Worker API + params, // if filename includes [id] or [[path]] + waitUntil, // same as ctx.waitUntil in existing Worker API + next, // used for middleware or to fetch assets + data, // arbitrary space for passing data between middlewares + } = context; + const clonedRequest = request.clone(); + errorHandling(context); + telemetryData(context); + const url = new URL(clonedRequest.url); + const response = fetch('https://telegra.ph/' + url.pathname + url.search, { + method: clonedRequest.method, + headers: clonedRequest.headers, + body: clonedRequest.body, + }); return response; - } - \ No newline at end of file +} diff --git a/functions/utils/middleware.js b/functions/utils/middleware.js new file mode 100644 index 000000000..0e10036df --- /dev/null +++ b/functions/utils/middleware.js @@ -0,0 +1,83 @@ +import sentryPlugin from "@cloudflare/pages-plugin-sentry"; +import '@sentry/tracing'; + +export function errorHandling(context) { + const env = context.env; + const sampleRate = env.sampleRate || 0.001; + console.log("sampleRate", sampleRate) + if (typeof env.disable_telemetry == "undefined" || env.disable_telemetry == null || env.disable_telemetry == "") { + context.data.telemetry = true; + return sentryPlugin({ + dsn: "https://219f636ac7bde5edab2c3e16885cb535@o4507041519108096.ingest.us.sentry.io/4507541492727808", + tracesSampleRate: sampleRate, + })(context);; + } + return context.next(); +} + +export function telemetryData(context) { + const env = context.env; + if (typeof env.disable_telemetry == "undefined" || env.disable_telemetry == null || env.disable_telemetry == "") { + try { + const parsedHeaders = {}; + context.request.headers.forEach((value, key) => { + parsedHeaders[key] = value + //check if the value is empty + if (value.length > 0) { + context.data.sentry.setTag(key, value); + } + }); + const CF = JSON.parse(JSON.stringify(context.request.cf)); + const parsedCF = {}; + for (const key in CF) { + if (typeof CF[key] == "object") { + parsedCF[key] = JSON.stringify(CF[key]); + } else { + parsedCF[key] = CF[key]; + if (CF[key].length > 0) { + context.data.sentry.setTag(key, CF[key]); + } + } + } + const data = { + headers: parsedHeaders, + cf: parsedCF, + url: context.request.url, + method: context.request.method, + redirect: context.request.redirect, + } + //get the url path + const urlPath = new URL(context.request.url).pathname; + const hostname = new URL(context.request.url).hostname; + context.data.sentry.setTag("path", urlPath); + context.data.sentry.setTag("url", data.url); + context.data.sentry.setTag("method", context.request.method); + context.data.sentry.setTag("redirect", context.request.redirect); + context.data.sentry.setContext("request", data); + const transaction = context.data.sentry.startTransaction({ name: `${context.request.method} ${hostname}` }); + //add the transaction to the context + context.data.transaction = transaction; + return context.next(); + } catch (e) { + console.log(e); + } finally { + context.data.transaction.finish(); + } + } + return context.next(); +} + +export async function traceData(context, span, op, name) { + const data = context.data + if (data.telemetry) { + if (span) { + console.log("span finish") + span.finish(); + } else { + console.log("span start") + span = await context.data.transaction.startChild( + { op: op, name: name }, + ); + } + } +} \ No newline at end of file diff --git a/index.html b/index.html index 1cc390bba..3eab6e12c 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,7 @@ + Telegraph-Image|免费图床