一分钟搭建影视站,支持Docker等部署方式
feat: update password handling to inject raw password for client-side hashing
developerfc3b583dc948
diff --git a/functions/_middleware.js b/functions/_middleware.jsindex a1ca7b8..f4f5544 100644--- a/functions/_middleware.js+++ b/functions/_middleware.js@@ -12,12 +12,14 @@ export async function onRequest(context) {// Get the original HTML contentlet html = await response.text();- // Hash the password before injecting it+ // Get the password from environment variablesconst password = env.PASSWORD || "";- const hashedPassword = password ? await sha256(password) : "";- html = html.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',- `window.__ENV__.PASSWORD_HASH = "${hashedPassword}";`);+ // Inject the password directly to be hashed client-side+ html = html.replace(+ 'window.__ENV__.PASSWORD = "{{PASSWORD}}";',+ `window.__ENV__.PASSWORD = "${password}"; // This will be hashed client-side before comparison`+ );// Create a new response with the modified HTMLreturn new Response(html, {diff --git a/js/password.js b/js/password.jsindex 98294de..1ad5098 100644--- a/js/password.js+++ b/js/password.js@@ -1,3 +1,20 @@+// Password protection configuration+const PASSWORD_CONFIG = {+ localStorageKey: 'libretv_password_verification',+ verificationTTL: 90 * 24 * 60 * 60 * 1000, // 90 days in milliseconds+};++// Store the hashed password+let hashedStoredPassword = '';++// Initialize the hashed password+async function initializeHashedPassword() {+ const password = window.__ENV__ && window.__ENV__.PASSWORD;+ if (password && password.trim() !== '') {+ hashedStoredPassword = await sha256(password);+ }+}+// 密码保护功能/**@@ -6,7 +23,7 @@*/function isPasswordProtected() {// 检查页面上嵌入的环境变量- const passwordRequired = window.__ENV__ && window.__ENV__.PASSWORD_HASH && window.__ENV__.PASSWORD_HASH.trim() !== '';+ const passwordRequired = window.__ENV__ && window.__ENV__.PASSWORD && window.__ENV__.PASSWORD.trim() !== '';return passwordRequired;}@@ -45,16 +62,13 @@ window.isPasswordVerified = isPasswordVerified;* 验证用户输入的密码是否正确*/async function verifyPassword(password) {- // 检查密码是否匹配环境变量中设置的哈希密码- const correctPasswordHash = window.__ENV__ && window.__ENV__.PASSWORD_HASH;-- if (!correctPasswordHash) {+ if (!hashedStoredPassword) {return true; // 如果没有设置密码,认为验证成功}// 对输入的密码进行哈希处理const inputPasswordHash = await sha256(password);- const isValid = inputPasswordHash === correctPasswordHash;+ const isValid = inputPasswordHash === hashedStoredPassword;if (isValid) {// 保存验证状态到localStorage@@ -154,7 +168,10 @@ async function handlePasswordSubmit() {/*** 初始化密码验证系统*/-function initPasswordProtection() {+async function initPasswordProtection() {+ // 首先初始化哈希密码+ await initializeHashedPassword();+if (!isPasswordProtected()) {return; // 如果未设置密码保护,则不进行任何操作}diff --git a/middleware.js b/middleware.jsindex e3e6dc9..e07eb60 100644--- a/middleware.js+++ b/middleware.js@@ -21,21 +21,22 @@ export default async function middleware(request) {// Get the HTML contentconst originalHtml = await response.text();- // Replace the placeholder with hashed environment variable+ // Hash the password before injecting itconst password = process.env.PASSWORD || '';- // Use a simple hash function for the password- const hashedPassword = password ? await sha256(password) : '';+ // Create a new response with the modified HTML+ // If password is set, inject it directly but with a comment indicating it's for hashing+ // Client-side code will hash it before comparisonconst modifiedHtml = originalHtml.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',- `window.__ENV__.PASSWORD_HASH = "${hashedPassword}";`+ `window.__ENV__.PASSWORD = "${password}"; // This will be hashed client-side before comparison`);- // Create a new response with the modified HTML+ // Return the new responsereturn new Response(modifiedHtml, {+ headers: response.headers,status: response.status,statusText: response.statusText,- headers: response.headers});}