Skip to content

一分钟搭建影视站,支持Docker等部署方式

fix: netlify password auth

旋律旋律你在干什么8cfde4df6133
diff --git a/netlify.toml b/netlify.toml
index b69c629..6b12e17 100644
--- a/netlify.toml
+++ b/netlify.toml
@@ -5,6 +5,11 @@
# publish = "." # 假设你的 HTML/CSS/JS 文件在根目录
functions = "netlify/functions" # 指定 Netlify 函数目录
+# 配置 Edge Functions
+[[edge_functions]]
+ function = "inject-env"
+ path = "/*"
+
# 配置重写规则,将 /proxy/* 的请求路由到 proxy 函数
# 这样前端的 PROXY_URL 仍然可以是 '/proxy/'
[[redirects]]
diff --git a/netlify/edge-functions/inject-env.js b/netlify/edge-functions/inject-env.js
new file mode 100644
index 0000000..15e67cf
--- /dev/null
+++ b/netlify/edge-functions/inject-env.js
@@ -0,0 +1,53 @@
+// Netlify Edge Function to inject environment variables into HTML
+export default async (request, context) => {
+ const url = new URL(request.url);
+
+ // Only process HTML pages
+ const isHtmlPage = url.pathname.endsWith('.html') || url.pathname === '/';
+ if (!isHtmlPage) {
+ return; // Let the request pass through unchanged
+ }
+
+ // Get the original response
+ const response = await context.next();
+
+ // Check if it's an HTML response
+ const contentType = response.headers.get('content-type') || '';
+ if (!contentType.includes('text/html')) {
+ return response; // Return the original response if not HTML
+ }
+
+ // Get the HTML content
+ const originalHtml = await response.text();
+
+ // Simple SHA-256 implementation for Netlify Edge Functions
+ async function sha256(message) {
+ const msgUint8 = new TextEncoder().encode(message);
+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
+ }
+
+ // Replace the placeholder with actual environment variable
+ const password = Netlify.env.get('PASSWORD') || '';
+ let passwordHash = '';
+ if (password) {
+ passwordHash = await sha256(password);
+ }
+
+ const modifiedHtml = originalHtml.replace(
+ 'window.__ENV__.PASSWORD = "{{PASSWORD}}";',
+ `window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`
+ );
+
+ // Create a new response with the modified HTML
+ return new Response(modifiedHtml, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers
+ });
+};
+
+export const config = {
+ path: ["/*"]
+};