一分钟搭建影视站,支持Docker等部署方式
feat: 提高安全性
developera2252bafa43f
diff --git a/functions/_middleware.js b/functions/_middleware.jsindex 8351aa7..a1ca7b8 100644--- a/functions/_middleware.js+++ b/functions/_middleware.js@@ -12,11 +12,12 @@ export async function onRequest(context) {// Get the original HTML contentlet html = await response.text();- // Replace the placeholder with actual environment variable value- // If PASSWORD is not set, replace with empty string+ // Hash the password before injecting itconst password = env.PASSWORD || "";+ const hashedPassword = password ? await sha256(password) : "";+html = html.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',- `window.__ENV__.PASSWORD = "${password}";`);+ `window.__ENV__.PASSWORD_HASH = "${hashedPassword}";`);// Create a new response with the modified HTMLreturn new Response(html, {@@ -28,4 +29,16 @@ export async function onRequest(context) {// Return the original response for non-HTML contentreturn response;+}++// Simple SHA-256 hash function+async function sha256(message) {+ // Encode as UTF-8+ const msgBuffer = new TextEncoder().encode(message);+ // Hash the message+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);+ // Convert to hex string+ return Array.from(new Uint8Array(hashBuffer))+ .map(b => b.toString(16).padStart(2, '0'))+ .join('');}\ No newline at end of filediff --git a/index.html b/index.htmlindex 5add310..ed198f1 100644--- a/index.html+++ b/index.html@@ -337,8 +337,8 @@window.__ENV__ = window.__ENV__ || {};// 注入服务器端环境变量 (将由服务器端替换)- // PASSWORD 变量将在这里被服务器端注入- window.__ENV__.PASSWORD = "{{PASSWORD}}";+ // PASSWORD 变量将在这里被服务器端注入,现在存储哈希值+ window.__ENV__.PASSWORD_HASH = "{{PASSWORD_HASH}}";</script><!-- 弹窗显示脚本 -->diff --git a/js/password.js b/js/password.jsindex 833cb34..98294de 100644--- a/js/password.js+++ b/js/password.js@@ -6,7 +6,7 @@*/function isPasswordProtected() {// 检查页面上嵌入的环境变量- const passwordRequired = window.__ENV__ && window.__ENV__.PASSWORD && window.__ENV__.PASSWORD.trim() !== '';+ const passwordRequired = window.__ENV__ && window.__ENV__.PASSWORD_HASH && window.__ENV__.PASSWORD_HASH.trim() !== '';return passwordRequired;}@@ -44,10 +44,17 @@ window.isPasswordVerified = isPasswordVerified;/*** 验证用户输入的密码是否正确*/-function verifyPassword(password) {- // 检查密码是否匹配环境变量中设置的密码- const correctPassword = window.__ENV__ && window.__ENV__.PASSWORD;- const isValid = password === correctPassword;+async function verifyPassword(password) {+ // 检查密码是否匹配环境变量中设置的哈希密码+ const correctPasswordHash = window.__ENV__ && window.__ENV__.PASSWORD_HASH;++ if (!correctPasswordHash) {+ return true; // 如果没有设置密码,认为验证成功+ }++ // 对输入的密码进行哈希处理+ const inputPasswordHash = await sha256(password);+ const isValid = inputPasswordHash === correctPasswordHash;if (isValid) {// 保存验证状态到localStorage@@ -61,6 +68,20 @@ function verifyPassword(password) {return isValid;}+/**+ * 将字符串转换为SHA-256哈希+ */+async function sha256(message) {+ // 编码为 UTF-8+ const msgBuffer = new TextEncoder().encode(message);+ // 哈希消息+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);+ // 转换为十六进制字符串+ return Array.from(new Uint8Array(hashBuffer))+ .map(b => b.toString(16).padStart(2, '0'))+ .join('');+}+/*** 显示密码验证弹窗*/@@ -112,11 +133,12 @@ function hidePasswordError() {/*** 处理密码提交事件*/-function handlePasswordSubmit() {+async function handlePasswordSubmit() {const passwordInput = document.getElementById('passwordInput');const password = passwordInput ? passwordInput.value.trim() : '';- if (verifyPassword(password)) {+ const isValid = await verifyPassword(password);+ if (isValid) {hidePasswordError();hidePasswordModal();} else {diff --git a/middleware.js b/middleware.jsindex 396be52..e3e6dc9 100644--- a/middleware.js+++ b/middleware.js@@ -21,12 +21,14 @@ export default async function middleware(request) {// Get the HTML contentconst originalHtml = await response.text();- // Replace the placeholder with actual environment variable- // If PASSWORD is not set, replace with empty string+ // Replace the placeholder with hashed environment variableconst password = process.env.PASSWORD || '';+ // Use a simple hash function for the password+ const hashedPassword = password ? await sha256(password) : '';+const modifiedHtml = originalHtml.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',- `window.__ENV__.PASSWORD = "${password}";`+ `window.__ENV__.PASSWORD_HASH = "${hashedPassword}";`);// Create a new response with the modified HTML@@ -37,6 +39,18 @@ export default async function middleware(request) {});}+// Simple SHA-256 hash function+async function sha256(message) {+ // Encode as UTF-8+ const msgBuffer = new TextEncoder().encode(message);+ // Hash the message+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);+ // Convert to hex string+ return Array.from(new Uint8Array(hashBuffer))+ .map(b => b.toString(16).padStart(2, '0'))+ .join('');+}+export const config = {matcher: ['/', '/((?!api|_next/static|_vercel|favicon.ico).*)'],};\ No newline at end of file