forked from cf-pages/Telegraph-Image
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update upload.js 允许CORS请求 * chore: Add Sentry CDN script for error tracking * chore: Update .gitignore and package.json * chore: Add error telemetry logging for request headers and Cloudflare data * chore: Refactoring middleware into separate files to minimize function triggers * chore: use transaction * chore: Update middleware to use dynamic tracesSampleRate
- Loading branch information
Showing
1,295 changed files
with
66,969 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package-lock.json | ||
_nuxt/b46e75617207ae5013d0.js | ||
node_modules | ||
package.json | ||
node_modules/.bin/* | ||
node_modules/.mf/* | ||
.wrangler/* | ||
data/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
<!-- Import CSS --> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/lib/theme-chalk/index.css"> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"> | ||
<script src="https://js.sentry-cdn.com/219f636ac7bde5edab2c3e16885cb535.min.js" crossorigin="anonymous"></script> | ||
<style> | ||
body { | ||
background: linear-gradient(90deg, #ffd7e4 0%, #c8f1ff 100%); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { errorHandling, telemetryData } from '../utils/middleware'; | ||
|
||
export const onRequest = [errorHandling, telemetryData]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,111 @@ | ||
export async function onRequest(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); | ||
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 url = new URL(request.url); | ||
|
||
const response = fetch('https://telegra.ph/' + url.pathname + url.search, { | ||
method: request.method, | ||
headers: request.headers, | ||
body: request.body, | ||
}).then(async (response) => { | ||
method: request.method, | ||
headers: request.headers, | ||
body: request.body, | ||
}).then(async (response) => { | ||
console.log(response.ok); // true if the response status is 2xx | ||
console.log(response.status); // 200 | ||
if(response.ok){ | ||
if (response.ok) { | ||
// Referer header equal to the admin page | ||
console.log(url.origin+"/admin") | ||
if (request.headers.get('Referer') == url.origin+"/admin") { | ||
console.log(url.origin + "/admin") | ||
if (request.headers.get('Referer') == url.origin + "/admin") { | ||
//show the image | ||
return response; | ||
} | ||
|
||
if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == ""){}else{ | ||
//check the record from kv | ||
const record = await env.img_url.getWithMetadata(params.id); | ||
console.log("record") | ||
console.log(record) | ||
if (record.metadata === null) { | ||
if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == "") { } else { | ||
//check the record from kv | ||
const record = await env.img_url.getWithMetadata(params.id); | ||
console.log("record") | ||
console.log(record) | ||
if (record.metadata === null) { | ||
|
||
}else{ | ||
|
||
//if the record is not null, redirect to the image | ||
if (record.metadata.ListType=="White"){ | ||
return response; | ||
}else if (record.metadata.ListType=="Block"){ | ||
console.log("Referer") | ||
console.log(request.headers.get('Referer')) | ||
if(typeof request.headers.get('Referer') == "undefined" ||request.headers.get('Referer') == null || request.headers.get('Referer') == ""){ | ||
return Response.redirect(url.origin+"/block-img.html", 302) | ||
}else{ | ||
return Response.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) | ||
} | ||
} else { | ||
|
||
}else if (record.metadata.Label=="adult"){ | ||
if(typeof request.headers.get('Referer') == "undefined" ||request.headers.get('Referer') == null || request.headers.get('Referer') == ""){ | ||
return Response.redirect(url.origin+"/block-img.html", 302) | ||
}else{ | ||
return Response.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) | ||
//if the record is not null, redirect to the image | ||
if (record.metadata.ListType == "White") { | ||
return response; | ||
} else if (record.metadata.ListType == "Block") { | ||
console.log("Referer") | ||
console.log(request.headers.get('Referer')) | ||
if (typeof request.headers.get('Referer') == "undefined" || request.headers.get('Referer') == null || request.headers.get('Referer') == "") { | ||
return Response.redirect(url.origin + "/block-img.html", 302) | ||
} else { | ||
return Response.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) | ||
} | ||
|
||
} else if (record.metadata.Label == "adult") { | ||
if (typeof request.headers.get('Referer') == "undefined" || request.headers.get('Referer') == null || request.headers.get('Referer') == "") { | ||
return Response.redirect(url.origin + "/block-img.html", 302) | ||
} else { | ||
return Response.redirect("https://static-res.pages.dev/teleimage/img-block-compressed.png", 302) | ||
} | ||
} | ||
//check if the env variables WhiteList_Mode are set | ||
console.log("env.WhiteList_Mode:", env.WhiteList_Mode) | ||
if (env.WhiteList_Mode == "true") { | ||
//if the env variables WhiteList_Mode are set, redirect to the image | ||
return Response.redirect(url.origin + "/whitelist-on.html", 302); | ||
} else { | ||
//if the env variables WhiteList_Mode are not set, redirect to the image | ||
return response; | ||
} | ||
} | ||
//check if the env variables WhiteList_Mode are set | ||
console.log("env.WhiteList_Mode:",env.WhiteList_Mode) | ||
if (env.WhiteList_Mode=="true"){ | ||
//if the env variables WhiteList_Mode are set, redirect to the image | ||
return Response.redirect(url.origin+"/whitelist-on.html", 302); | ||
}else{ | ||
//if the env variables WhiteList_Mode are not set, redirect to the image | ||
return response; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
//get time | ||
let time = new Date().getTime(); | ||
let apikey=env.ModerateContentApiKey | ||
if(typeof apikey == "undefined" || apikey == null || apikey == ""){ | ||
if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == ""){ | ||
//get time | ||
let time = new Date().getTime(); | ||
|
||
let apikey = env.ModerateContentApiKey | ||
|
||
if (typeof apikey == "undefined" || apikey == null || apikey == "") { | ||
|
||
if (typeof env.img_url == "undefined" || env.img_url == null || env.img_url == "") { | ||
console.log("Not enbaled KV") | ||
}else{ | ||
|
||
} else { | ||
//add image to kv | ||
await env.img_url.put(params.id, "",{ | ||
metadata: { ListType: "None", Label: "None",TimeStamp: time }, | ||
await env.img_url.put(params.id, "", { | ||
metadata: { ListType: "None", Label: "None", TimeStamp: time }, | ||
}); | ||
|
||
} | ||
}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) | ||
}}); | ||
|
||
} 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; | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { errorHandling, telemetryData } from '../utils/middleware'; | ||
|
||
export const onRequest = [errorHandling, telemetryData]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }, | ||
); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.