Skip to content

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

refactor: remove ADMINPASSWORD support and update related documentation

developerd1303fce1d78
diff --git a/.env.example b/.env.example
index 5c9e08c..08a7854 100644
--- a/.env.example
+++ b/.env.example
@@ -1,7 +1,6 @@
# 基本配置
PORT=8080
-PASSWORD=
-ADMINPASSWORD=
+PASSWORD=111111
DEBUG=false
# CORS 配置
diff --git a/README.md b/README.md
index 2b0a744..4375dad 100644
--- a/README.md
+++ b/README.md
@@ -75,19 +75,16 @@ Pull Bot 会反复触发无效的 PR 和垃圾邮件,严重干扰项目维护
4. 使用以下设置:
- 构建命令:留空(无需构建)
- 输出目录:留空(默认为根目录)
-5. **⚠️ 重要:在"设置" > "环境变量"中添加 `PASSWORD` 变量**
-6. **可选:在"Settings" > "Environment Variables"中添加 `ADMINPASSWORD` 变量**
-7. 点击"保存并部署"
+5. **⚠️ 重要:在"设置" > "环境变量"中添加 `PASSWORD` 变量(必须设置)**
+6. 点击"保存并部署"
### Vercel
1. Fork 或克隆本仓库到您的 GitHub/GitLab 账户
2. 登录 [Vercel](https://vercel.com/),点击"New Project"
3. 导入您的仓库,使用默认设置
-4. **⚠️ 重要:在"Settings" > "Environment Variables"中添加 `PASSWORD` 变量**
-5. **可选:在"Settings" > "Environment Variables"中添加 `ADMINPASSWORD` 变量**
-6. 点击"Deploy"
-7. 可选:在"Settings" > "Environment Variables"中配置密码保护和设置按钮密码保护
+4. **⚠️ 重要:在"Settings" > "Environment Variables"中添加 `PASSWORD` 变量(必须设置)**
+5. 点击"Deploy"
### Render
@@ -98,7 +95,7 @@ Pull Bot 会反复触发无效的 PR 和垃圾邮件,严重干扰项目维护
5. 点击 "Create Web Service",等待部署完成
6. 部署成功后即可访问您的 LibreTV 实例
-> 如需启用密码保护,可在 Render 控制台的环境变量中手动添加 `PASSWORD` 和/或 `ADMINPASSWORD`。
+> 如需启用密码保护,必须在 Render 控制台的环境变量中添加 `PASSWORD`。
### Docker
```
@@ -107,7 +104,6 @@ docker run -d \
--restart unless-stopped \
-p 8899:8080 \
-e PASSWORD=your_password \
- -e ADMINPASSWORD=your_adminpassword \
bestzwei/libretv:latest
```
@@ -124,7 +120,6 @@ services:
- "8899:8080" # 将内部 8080 端口映射到主机的 8899 端口
environment:
- PASSWORD=${PASSWORD:-your_password} # 可将 your_password 修改为你想要的密码,默认为 your_password
- - ADMINPASSWORD=${PASSWORD:-your_adminpassword} # 可将 your_adminpassword 修改为你想要的密码,默认为 your_adminpassword
restart: unless-stopped
```
启动 LibreTV:
@@ -157,13 +152,12 @@ npm run dev
### 密码保护
-要为您的 LibreTV 实例添加密码保护,可以在部署平台上设置环境变量:
+**重要提示**: 为确保安全,所有部署都必须设置 PASSWORD 环境变量,否则用户将看到设置密码的提示。
-**环境变量名**: `PASSWORD`
-**值**: 您想设置的密码
+要为您的 LibreTV 实例添加密码保护,在部署平台上设置环境变量:
-**环境变量名**: `ADMINPASSWORD`
-**值**: 您想设置的密码
+**环境变量名**: `PASSWORD`
+**值**: 您想设置的密码(强制必须设置)
各平台设置方法:
diff --git a/functions/_middleware.js b/functions/_middleware.js
index ec21453..4fa1a14 100644
--- a/functions/_middleware.js
+++ b/functions/_middleware.js
@@ -17,14 +17,9 @@ export async function onRequest(context) {
html = html.replace('window.__ENV__.PASSWORD = "{{PASSWORD}}";',
`window.__ENV__.PASSWORD = "${passwordHash}";`);
- // 处理管理员密码 - 确保这部分代码被执行
- const adminPassword = env.ADMINPASSWORD || "";
- let adminPasswordHash = "";
- if (adminPassword) {
- adminPasswordHash = await sha256(adminPassword);
- }
+ // 移除 ADMINPASSWORD 占位符
html = html.replace('window.__ENV__.ADMINPASSWORD = "{{ADMINPASSWORD}}";',
- `window.__ENV__.ADMINPASSWORD = "${adminPasswordHash}";`);
+ 'window.__ENV__.ADMINPASSWORD = "";');
return new Response(html, {
headers: response.headers,
diff --git a/js/app.js b/js/app.js
index 0d68e00..5dabefa 100644
--- a/js/app.js
+++ b/js/app.js
@@ -484,21 +484,9 @@ function toggleSettings(e) {
const settingsPanel = document.getElementById('settingsPanel');
if (!settingsPanel) return;
- // 检查是否有管理员密码
- const hasAdminPassword = window.__ENV__?.ADMINPASSWORD &&
- window.__ENV__.ADMINPASSWORD.length === 64 &&
- !/^0+$/.test(window.__ENV__.ADMINPASSWORD);
-
if (settingsPanel.classList.contains('show')) {
settingsPanel.classList.remove('show');
} else {
- // 只有设置了管理员密码且未验证时才拦截
- if (hasAdminPassword && !isAdminVerified()) {
- e.preventDefault();
- e.stopPropagation();
- showAdminPasswordModal();
- return;
- }
settingsPanel.classList.add('show');
}
diff --git a/js/config.js b/js/config.js
index 337e19d..cbcc6c9 100644
--- a/js/config.js
+++ b/js/config.js
@@ -7,8 +7,7 @@ const MAX_HISTORY_ITEMS = 5;
// 密码保护配置
const PASSWORD_CONFIG = {
localStorageKey: 'passwordVerified', // 存储验证状态的键名
- verificationTTL: 90 * 24 * 60 * 60 * 1000, // 验证有效期(90天,约3个月)
- adminLocalStorageKey: 'adminPasswordVerified' // 新增的管理员验证状态的键名
+ verificationTTL: 90 * 24 * 60 * 60 * 1000 // 验证有效期(90天,约3个月)
};
// 网站信息配置
diff --git a/js/password.js b/js/password.js
index 98d21c9..07a3c0c 100644
--- a/js/password.js
+++ b/js/password.js
@@ -5,38 +5,37 @@
* 通过读取页面上嵌入的环境变量来检查
*/
function isPasswordProtected() {
- // 检查页面上嵌入的环境变量
+ // 只检查普通密码
const pwd = window.__ENV__ && window.__ENV__.PASSWORD;
- const adminPwd = window.__ENV__ && window.__ENV__.ADMINPASSWORD;
-
- // 检查普通密码或管理员密码是否有效
- const isPwdValid = typeof pwd === 'string' && pwd.length === 64 && !/^0+$/.test(pwd);
- const isAdminPwdValid = typeof adminPwd === 'string' && adminPwd.length === 64 && !/^0+$/.test(adminPwd);
+
+ // 检查普通密码是否有效
+ return typeof pwd === 'string' && pwd.length === 64 && !/^0+$/.test(pwd);
+}
- // 任意一个密码有效即认为启用了密码保护
- return isPwdValid || isAdminPwdValid;
+/**
+ * 检查是否强制要求设置密码
+ * 如果没有设置有效的 PASSWORD,则认为需要强制设置密码
+ */
+function isPasswordRequired() {
+ return !isPasswordProtected();
}
window.isPasswordProtected = isPasswordProtected;
+window.isPasswordRequired = isPasswordRequired;
/**
* 验证用户输入的密码是否正确(异步,使用SHA-256哈希)
*/
-// 统一验证函数
-async function verifyPassword(password, passwordType = 'PASSWORD') {
+async function verifyPassword(password) {
try {
- const correctHash = window.__ENV__?.[passwordType];
+ const correctHash = window.__ENV__?.PASSWORD;
if (!correctHash) return false;
const inputHash = await sha256(password);
const isValid = inputHash === correctHash;
if (isValid) {
- const storageKey = passwordType === 'PASSWORD'
- ? PASSWORD_CONFIG.localStorageKey
- : PASSWORD_CONFIG.adminLocalStorageKey;
-
- localStorage.setItem(storageKey, JSON.stringify({
+ localStorage.setItem(PASSWORD_CONFIG.localStorageKey, JSON.stringify({
verified: true,
timestamp: Date.now(),
passwordHash: correctHash
@@ -44,38 +43,34 @@ async function verifyPassword(password, passwordType = 'PASSWORD') {
}
return isValid;
} catch (error) {
- console.error(`验证${passwordType}密码时出错:`, error);
+ console.error('验证密码时出错:', error);
return false;
}
}
-// 统一验证状态检查
-function isVerified(passwordType = 'PASSWORD') {
+// 验证状态检查
+function isPasswordVerified() {
try {
if (!isPasswordProtected()) return true;
- const storageKey = passwordType === 'PASSWORD'
- ? PASSWORD_CONFIG.localStorageKey
- : PASSWORD_CONFIG.adminLocalStorageKey;
-
- const stored = localStorage.getItem(storageKey);
+ const stored = localStorage.getItem(PASSWORD_CONFIG.localStorageKey);
if (!stored) return false;
const { timestamp, passwordHash } = JSON.parse(stored);
- const currentHash = window.__ENV__?.[passwordType];
+ const currentHash = window.__ENV__?.PASSWORD;
return timestamp && passwordHash === currentHash &&
Date.now() - timestamp < PASSWORD_CONFIG.verificationTTL;
} catch (error) {
- console.error(`检查${passwordType}验证状态时出错:`, error);
+ console.error('检查密码验证状态时出错:', error);
return false;
}
}
// 更新全局导出
window.isPasswordProtected = isPasswordProtected;
-window.isPasswordVerified = () => isVerified('PASSWORD');
-window.isAdminVerified = () => isVerified('ADMINPASSWORD');
+window.isPasswordRequired = isPasswordRequired;
+window.isPasswordVerified = isPasswordVerified;
window.verifyPassword = verifyPassword;
// SHA-256实现,可用Web Crypto API
@@ -103,15 +98,46 @@ function showPasswordModal() {
document.getElementById('doubanArea').classList.add('hidden');
document.getElementById('passwordCancelBtn').classList.add('hidden');
+ // 检查是否需要强制设置密码
+ if (isPasswordRequired()) {
+ // 修改弹窗内容提示用户需要先设置密码
+ const title = passwordModal.querySelector('h2');
+ const description = passwordModal.querySelector('p');
+ if (title) title.textContent = '需要设置密码';
+ if (description) description.textContent = '请先在部署平台设置 PASSWORD 环境变量来保护您的实例';
+
+ // 隐藏密码输入框和提交按钮,只显示提示信息
+ const form = passwordModal.querySelector('form');
+ const errorMsg = document.getElementById('passwordError');
+ if (form) form.style.display = 'none';
+ if (errorMsg) {
+ errorMsg.textContent = '为确保安全,必须设置 PASSWORD 环境变量才能使用本服务,请联系管理员进行配置';
+ errorMsg.classList.remove('hidden');
+ errorMsg.className = 'text-red-500 mt-2 font-medium'; // 改为更醒目的红色
+ }
+ } else {
+ // 正常的密码验证模式
+ const title = passwordModal.querySelector('h2');
+ const description = passwordModal.querySelector('p');
+ if (title) title.textContent = '访问验证';
+ if (description) description.textContent = '请输入密码继续访问';
+
+ const form = passwordModal.querySelector('form');
+ if (form) form.style.display = 'block';
+ }
+
passwordModal.style.display = 'flex';
- // 确保输入框获取焦点
- setTimeout(() => {
- const passwordInput = document.getElementById('passwordInput');
- if (passwordInput) {
- passwordInput.focus();
- }
- }, 100);
+ // 只有在非强制设置密码模式下才聚焦输入框
+ if (!isPasswordRequired()) {
+ // 确保输入框获取焦点
+ setTimeout(() => {
+ const passwordInput = document.getElementById('passwordInput');
+ if (passwordInput) {
+ passwordInput.focus();
+ }
+ }, 100);
+ }
}
}
@@ -179,75 +205,23 @@ async function handlePasswordSubmit() {
}
/**
- * 初始化密码验证系统(需适配异步事件)
+ * 初始化密码验证系统
*/
-// 修改initPasswordProtection函数
function initPasswordProtection() {
- if (!isPasswordProtected()) {
+ // 如果需要强制设置密码,显示警告弹窗
+ if (isPasswordRequired()) {
+ showPasswordModal();
return;
}
- // 检查是否有普通密码
- const hasNormalPassword = window.__ENV__?.PASSWORD &&
- window.__ENV__.PASSWORD.length === 64 &&
- !/^0+$/.test(window.__ENV__.PASSWORD);
-
- // 只有当设置了普通密码且未验证时才显示密码框
- if (hasNormalPassword && !isPasswordVerified()) {
+ // 如果设置了密码但用户未验证,显示密码输入框
+ if (isPasswordProtected() && !isPasswordVerified()) {
showPasswordModal();
- }
-
- // 设置按钮事件监听
- const settingsBtn = document.querySelector('[onclick="toggleSettings(event)"]');
- if (settingsBtn) {
- settingsBtn.addEventListener('click', function(e) {
- // 只有当设置了普通密码且未验证时才拦截点击
- if (hasNormalPassword && !isPasswordVerified()) {
- e.preventDefault();
- e.stopPropagation();
- showPasswordModal();
- return;
- }
-
- });
- }
-}
-
-// 设置按钮密码框验证
-function showAdminPasswordModal() {
- const passwordModal = document.getElementById('passwordModal');
- if (!passwordModal) return;
-
- // 清空密码输入框
- const passwordInput = document.getElementById('passwordInput');
- if (passwordInput) passwordInput.value = '';
-
- // 修改标题为管理员验证
- const title = passwordModal.querySelector('h2');
- if (title) title.textContent = '管理员验证';
-
- document.getElementById('passwordCancelBtn').classList.remove('hidden');
- passwordModal.style.display = 'flex';
-
- // 设置表单提交处理
- const form = document.getElementById('passwordForm');
- if (form) {
- form.onsubmit = async function (e) {
- e.preventDefault();
- const password = document.getElementById('passwordInput').value.trim();
- if (await verifyPassword(password, 'ADMINPASSWORD')) {
- passwordModal.style.display = 'none';
- document.getElementById('settingsPanel').classList.add('show');
- } else {
- showPasswordError();
- }
- };
+ return;
}
}
// 在页面加载完成后初始化密码保护
document.addEventListener('DOMContentLoaded', function () {
initPasswordProtection();
-});
-
-
+});
\ No newline at end of file
diff --git a/middleware.js b/middleware.js
index f490ea0..897df6b 100644
--- a/middleware.js
+++ b/middleware.js
@@ -30,14 +30,8 @@ export default async function middleware(request) {
if (password) {
passwordHash = await sha256(password);
}
-
- const adminpassword = process.env.ADMINPASSWORD || '';
- let adminpasswordHash = '';
- if (adminpassword) {
- adminpasswordHash = await sha256(adminpassword); // 修复变量名
- }
- // 合并两次替换为一次操作
+ // 替换密码占位符
let modifiedHtml = originalHtml
.replace(
'window.__ENV__.PASSWORD = "{{PASSWORD}}";',
@@ -45,7 +39,7 @@ export default async function middleware(request) {
)
.replace(
'window.__ENV__.ADMINPASSWORD = "{{ADMINPASSWORD}}";',
- `window.__ENV__.ADMINPASSWORD = "${adminpasswordHash}"; // SHA-256 hash`
+ 'window.__ENV__.ADMINPASSWORD = ""; // ADMINPASSWORD 功能已移除'
);
// 修复Response构造
diff --git a/netlify/edge-functions/inject-env.js b/netlify/edge-functions/inject-env.js
index 15e67cf..20f34f1 100644
--- a/netlify/edge-functions/inject-env.js
+++ b/netlify/edge-functions/inject-env.js
@@ -35,10 +35,15 @@ export default async (request, context) => {
passwordHash = await sha256(password);
}
- const modifiedHtml = originalHtml.replace(
- 'window.__ENV__.PASSWORD = "{{PASSWORD}}";',
- `window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`
- );
+ const modifiedHtml = originalHtml
+ .replace(
+ 'window.__ENV__.PASSWORD = "{{PASSWORD}}";',
+ `window.__ENV__.PASSWORD = "${passwordHash}"; // SHA-256 hash`
+ )
+ .replace(
+ 'window.__ENV__.ADMINPASSWORD = "{{ADMINPASSWORD}}";',
+ 'window.__ENV__.ADMINPASSWORD = ""; // ADMINPASSWORD 功能已移除'
+ );
// Create a new response with the modified HTML
return new Response(modifiedHtml, {
diff --git a/server.mjs b/server.mjs
index e7552b7..19bf67e 100644
--- a/server.mjs
+++ b/server.mjs
@@ -15,7 +15,6 @@ const __dirname = path.dirname(__filename);
const config = {
port: process.env.PORT || 8080,
password: process.env.PASSWORD || '',
- adminpassword: process.env.ADMINPASSWORD || '',
corsOrigin: process.env.CORS_ORIGIN || '*',
timeout: parseInt(process.env.REQUEST_TIMEOUT || '5000'),
maxRetries: parseInt(process.env.MAX_RETRIES || '2'),
@@ -58,12 +57,11 @@ async function renderPage(filePath, password) {
if (password !== '') {
const sha256 = await sha256Hash(password);
content = content.replace('{{PASSWORD}}', sha256);
+ } else {
+ content = content.replace('{{PASSWORD}}', '');
}
- // 添加ADMINPASSWORD注入
- if (config.adminpassword !== '') {
- const adminSha256 = await sha256Hash(config.adminpassword);
- content = content.replace('{{ADMINPASSWORD}}', adminSha256);
- }
+ // 移除 ADMINPASSWORD 占位符
+ content = content.replace('{{ADMINPASSWORD}}', '');
return content;
}
@@ -210,12 +208,11 @@ app.listen(config.port, () => {
console.log(`服务器运行在 http://localhost:${config.port}`);
if (config.password !== '') {
console.log('用户登录密码已设置');
- }
- if (config.adminpassword !== '') {
- console.log('管理员登录密码已设置');
+ } else {
+ console.log('警告: 未设置 PASSWORD 环境变量,用户将被要求设置密码');
}
if (config.debug) {
console.log('调试模式已启用');
- console.log('配置:', { ...config, password: config.password ? '******' : '', adminpassword: config.adminpassword? '******' : '' });
+ console.log('配置:', { ...config, password: config.password ? '******' : '' });
}
});