summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-01-22 10:01:20 -0800
committerFuwn <[email protected]>2024-01-22 10:02:07 -0800
commit4bb0f5b017e2d33c6e4d728f97f5f5e322012f61 (patch)
tree8cc3cc7cd0adbd9a24f9d5b103efde7323a16324
downloadcdn.due.moe-4bb0f5b017e2d33c6e4d728f97f5f5e322012f61.tar.xz
cdn.due.moe-4bb0f5b017e2d33c6e4d728f97f5f5e322012f61.zip
feat: initial release
-rw-r--r--.gitignore5
-rw-r--r--LICENSE22
-rw-r--r--index.ts106
-rw-r--r--package.json7
-rw-r--r--wrangler.toml4
5 files changed, 144 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ae33762
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+# Wrangler
+.wrangler
+
+# Node.js
+node_modules
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..89a6a97
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+MIT License
+
+Copyright (c) 2019 soruly
+Copyright (c) 2024 Fuwn
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/index.ts b/index.ts
new file mode 100644
index 0000000..ae11e69
--- /dev/null
+++ b/index.ts
@@ -0,0 +1,106 @@
+// <https://github.com/soruly/trace.moe-www/blob/master/image-proxy.js>
+
+addEventListener("fetch", async (event) => {
+ event.respondWith(handleRequest(event.request));
+});
+
+const errorResponse = (errorMessage) =>
+ new Response(errorMessage, {
+ status: 400,
+ statusText: "Bad Request",
+ });
+
+const handleRequest = async (originalRequest) => {
+ let originalURL = new URL(originalRequest.url);
+ if (!originalURL.searchParams.get("url")) {
+ return errorResponse("Error: Cannot get url from param");
+ }
+
+ let imageURL = null;
+ try {
+ imageURL = new URL(originalURL.searchParams.get("url"));
+ } catch (e) {}
+ if (!imageURL) {
+ return errorResponse("Error: Invalid URL string");
+ }
+
+ let imageRequest = new Request(imageURL, {
+ redirect: "follow",
+ headers: {
+ referer: imageURL.origin,
+ },
+ });
+
+ let response = await fetch(imageRequest, {
+ cf: {
+ polish: "lossy",
+ },
+ });
+
+ if (response.status >= 400) {
+ return new Response(response.body, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+ }
+
+ // if (
+ // response.headers.get("Content-Type").toLowerCase() !==
+ // "application/octet-stream" &&
+ // !["image", "video"].includes(
+ // response.headers.get("Content-Type").split("/")[0].toLowerCase()
+ // )
+ // ) {
+ // // retry as bot to get og:image
+ // let webResponse = await fetch(
+ // new Request(imageURL, {
+ // redirect: "follow",
+ // headers: {
+ // referer: imageURL.origin,
+ // "User-Agent": "googlebot",
+ // },
+ // })
+ // );
+ // if (response.status === 200) {
+ // const ogImageURL = (await webResponse.text())
+ // ?.match(/<[^<]+?"og:image".*?>/, "$1")?.[0]
+ // ?.match(/content="(.*?)"/, "$1")?.[1];
+
+ // if (ogImageURL && ogImageURL.match(/^https?:\/\//)) {
+ // response = await fetch(
+ // new Request(ogImageURL, {
+ // redirect: "follow",
+ // headers: {
+ // referer: imageURL.origin,
+ // },
+ // })
+ // );
+ // if (response.status >= 400) {
+ // return new Response(response.body, {
+ // status: response.status,
+ // statusText: response.statusText,
+ // headers: response.headers,
+ // });
+ // }
+ // }
+ // }
+ // }
+ // if (
+ // response.headers.get("Content-Type").toLowerCase() !==
+ // "application/octet-stream" &&
+ // !["image", "video"].includes(
+ // response.headers.get("Content-Type").split("/")[0].toLowerCase()
+ // )
+ // ) {
+ // return errorResponse(
+ // "Error: Content-Type is not image or video or application/octet-stream"
+ // );
+ // }
+
+ return new Response(response.body, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..33970b9
--- /dev/null
+++ b/package.json
@@ -0,0 +1,7 @@
+{
+ "private": true,
+ "devDependencies": {
+ "@types/node": "^17.0.42",
+ "typescript": "^4.7.3"
+ }
+}
diff --git a/wrangler.toml b/wrangler.toml
new file mode 100644
index 0000000..adb3e07
--- /dev/null
+++ b/wrangler.toml
@@ -0,0 +1,4 @@
+name = "cdn"
+main = "./index.ts"
+compatibility_date = "2023-10-30"
+