Skip to content

Prompt as Code | GPT Image 2 / 2.5 提示词与案例库,530+ 个案例、20+ 套工业级模板与可复用 Skills,新增 2.5 同提示词对比专区,附完整提示词与生成记录,持续更新。

chore: hotlink protection + CDN cache headers for images/cases.json

苍何911f2575b0f4
diff --git a/middleware.ts b/middleware.ts
new file mode 100644
index 0000000..eacb377
--- /dev/null
+++ b/middleware.ts
@@ -0,0 +1,61 @@
+// Hotlink protection for static assets served from this Vite site.
+// Runs before the filesystem/static cache on Vercel (Routing Middleware, Edge).
+// Blocks third-party sites from hotlinking images/JSON while allowing:
+// - empty/missing referer (direct visits, curl, download tools)
+// - same-site referers (gpt-image2.canghe.ai and its subdomains/preview)
+// Every blocked attempt is logged with client IP so abuse can be traced.
+
+const ALLOWED_REFERRER_HOSTS = [
+ 'gpt-image2.canghe.ai',
+ 'awesome-gpt-image-2.vercel.app',
+ 'localhost',
+ '127.0.0.1',
+];
+
+const PROTECTED_PREFIXES = ['/images/', '/cases.json', '/assets/', '/gpt-image-2-5/'];
+
+export default function middleware(request) {
+ const url = new URL(request.url);
+ const pathname = url.pathname;
+
+ if (!PROTECTED_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
+ return undefined;
+ }
+
+ const referer = request.headers.get('referer') || '';
+ if (!referer) {
+ return undefined;
+ }
+
+ let refererHostname = null;
+ try {
+ refererHostname = new URL(referer).hostname;
+ } catch (error) {
+ refererHostname = null;
+ }
+
+ const allowed =
+ refererHostname &&
+ ALLOWED_REFERRER_HOSTS.some(
+ (host) => refererHostname === host || refererHostname.endsWith('.' + host)
+ );
+
+ if (allowed) {
+ return undefined;
+ }
+
+ const ip = (request.headers.get('x-forwarded-for') || '').split(',')[0].trim() || 'unknown';
+ const ua = (request.headers.get('user-agent') || '').slice(0, 100);
+ console.log(
+ `[hotlink-block] ip=${ip} path=${pathname} referer=${refererHostname || '(invalid)'} ua=${ua}`
+ );
+
+ return new Response('403 Forbidden', {
+ status: 403,
+ headers: { 'Cache-Control': 'no-store' },
+ });
+}
+
+export const config = {
+ matcher: ['/images/:path*', '/cases.json', '/assets/:path*', '/gpt-image-2-5/:path*'],
+};
diff --git a/vercel.json b/vercel.json
index 1d35ab8..f3f6887 100644
--- a/vercel.json
+++ b/vercel.json
@@ -20,5 +20,43 @@
"source": "/community/result/",
"destination": "/"
}
+ ],
+ "headers": [
+ {
+ "source": "/images/(.*)",
+ "headers": [
+ {
+ "key": "Cache-Control",
+ "value": "public, max-age=604800, s-maxage=31536000, stale-while-revalidate=2592000"
+ }
+ ]
+ },
+ {
+ "source": "/cases.json",
+ "headers": [
+ {
+ "key": "Cache-Control",
+ "value": "public, max-age=300, s-maxage=900, stale-while-revalidate=3600"
+ }
+ ]
+ },
+ {
+ "source": "/assets/(.*)",
+ "headers": [
+ {
+ "key": "Cache-Control",
+ "value": "public, max-age=31536000, immutable"
+ }
+ ]
+ },
+ {
+ "source": "/gpt-image-2-5/(.*)",
+ "headers": [
+ {
+ "key": "Cache-Control",
+ "value": "public, max-age=31536000, immutable"
+ }
+ ]
+ }
]
}