一分钟搭建影视站,支持Docker等部署方式
sha256
developer1467bbdb6ec9
diff --git a/functions/_middleware.js b/functions/_middleware.jsindex f4f5544..71604f9 100644--- a/functions/_middleware.js+++ b/functions/_middleware.js@@ -1,3 +1,5 @@+import { sha256 } from '../js/sha256.js'; // 需新建或引入SHA-256实现+// Cloudflare Pages Middleware to inject environment variablesexport async function onRequest(context) {const { request, env, next } = context;@@ -12,14 +14,15 @@ export async function onRequest(context) {// Get the original HTML contentlet html = await response.text();- // Get the password from environment variables+ // Replace the placeholder with actual environment variable value+ // If PASSWORD is not set, replace with empty stringconst password = env.PASSWORD || "";-- // 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`- );+ let passwordHash = "";+ if (password) {+ passwordHash = await sha256(password);+ }+ html = html.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',+ `window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`);// Create a new response with the modified HTMLreturn new Response(html, {@@ -31,16 +34,4 @@ 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 ed198f1..5add310 100644--- a/index.html+++ b/index.html@@ -337,8 +337,8 @@window.__ENV__ = window.__ENV__ || {};// 注入服务器端环境变量 (将由服务器端替换)- // PASSWORD 变量将在这里被服务器端注入,现在存储哈希值- window.__ENV__.PASSWORD_HASH = "{{PASSWORD_HASH}}";+ // PASSWORD 变量将在这里被服务器端注入+ window.__ENV__.PASSWORD = "{{PASSWORD}}";</script><!-- 弹窗显示脚本 -->diff --git a/js/password.js b/js/password.jsindex 1ad5098..2f99ea1 100644--- a/js/password.js+++ b/js/password.js@@ -1,20 +1,3 @@-// 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);- }-}-// 密码保护功能/**@@ -59,41 +42,29 @@ window.isPasswordProtected = isPasswordProtected;window.isPasswordVerified = isPasswordVerified;/**- * 验证用户输入的密码是否正确+ * 验证用户输入的密码是否正确(异步,使用SHA-256哈希)*/async function verifyPassword(password) {- if (!hashedStoredPassword) {- return true; // 如果没有设置密码,认为验证成功- }-- // 对输入的密码进行哈希处理- const inputPasswordHash = await sha256(password);- const isValid = inputPasswordHash === hashedStoredPassword;-+ const correctHash = window.__ENV__ && window.__ENV__.PASSWORD;+ if (!correctHash) return false;+ const inputHash = await sha256(password);+ const isValid = inputHash === correctHash;if (isValid) {- // 保存验证状态到localStorageconst verificationData = {verified: true,timestamp: Date.now()};localStorage.setItem(PASSWORD_CONFIG.localStorageKey, JSON.stringify(verificationData));}-return isValid;}-/**- * 将字符串转换为SHA-256哈希- */+// SHA-256实现,可用Web Crypto APIasync function sha256(message) {- // 编码为 UTF-8- const msgBuffer = new TextEncoder().encode(message);- // 哈希消息+ 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('');+ const hashArray = Array.from(new Uint8Array(hashBuffer));+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');}/**@@ -145,19 +116,16 @@ function hidePasswordError() {}/**- * 处理密码提交事件+ * 处理密码提交事件(异步)*/async function handlePasswordSubmit() {const passwordInput = document.getElementById('passwordInput');const password = passwordInput ? passwordInput.value.trim() : '';-- const isValid = await verifyPassword(password);- if (isValid) {+ if (await verifyPassword(password)) {hidePasswordError();hidePasswordModal();} else {showPasswordError();- // 清空密码输入框if (passwordInput) {passwordInput.value = '';passwordInput.focus();@@ -166,12 +134,9 @@ async function handlePasswordSubmit() {}/**- * 初始化密码验证系统+ * 初始化密码验证系统(需适配异步事件)*/-async function initPasswordProtection() {- // 首先初始化哈希密码- await initializeHashedPassword();-+function initPasswordProtection() {if (!isPasswordProtected()) {return; // 如果未设置密码保护,则不进行任何操作}diff --git a/js/sha256.js b/js/sha256.jsnew file mode 100644index 0000000..1bf5816--- /dev/null+++ b/js/sha256.js@@ -0,0 +1,6 @@+export async function sha256(message) {+ const msgBuffer = new TextEncoder().encode(message);+ const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);+ const hashArray = Array.from(new Uint8Array(hashBuffer));+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');+}diff --git a/middleware.js b/middleware.jsindex e07eb60..ddec704 100644--- a/middleware.js+++ b/middleware.js@@ -1,3 +1,5 @@+import { sha256 } from './js/sha256.js'; // 需新建或引入SHA-256实现+// Vercel Middleware to inject environment variablesexport default async function middleware(request) {// Get the URL from the request@@ -21,37 +23,26 @@ export default async function middleware(request) {// Get the HTML contentconst originalHtml = await response.text();- // Hash the password before injecting it+ // Replace the placeholder with actual environment variable+ // If PASSWORD is not set, replace with empty stringconst password = process.env.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 comparison+ let passwordHash = '';+ if (password) {+ passwordHash = await sha256(password);+ }const modifiedHtml = originalHtml.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',- `window.__ENV__.PASSWORD = "${password}"; // This will be hashed client-side before comparison`+ `window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`);- // Return the new response+ // Create a new response with the modified HTMLreturn new Response(modifiedHtml, {- headers: response.headers,status: response.status,statusText: response.statusText,+ headers: response.headers});}-// 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