fix: 首次启动自动设置默认密码
新服务器无 .env 文件时,get_or_set_password() 自动: 1. 设置默认密码 admin123 2. 写入 .env 文件 3. 控制台打印提示 解决新服务器无法登录的问题。
This commit is contained in:
@@ -13,6 +13,10 @@ from services.backup_service import set_backup_dir
|
|||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
|
# 确保密码已设置(首次启动自动设置默认密码)
|
||||||
|
from auth import get_or_set_password
|
||||||
|
get_or_set_password()
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config.from_object(Config)
|
app.config.from_object(Config)
|
||||||
app.config["SECRET_KEY"] = os.environ.get(
|
app.config["SECRET_KEY"] = os.environ.get(
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ bp = Blueprint("auth", __name__)
|
|||||||
|
|
||||||
def _get_admin_creds():
|
def _get_admin_creds():
|
||||||
import os
|
import os
|
||||||
_ENV = "/root/socks-manager/.env"
|
_ENV = os.environ.get("SM_ENV_PATH", os.path.join(os.path.dirname(__file__), ".env"))
|
||||||
pwd = os.environ.get("SM_ADMIN_PASSWORD")
|
pwd = os.environ.get("SM_ADMIN_PASSWORD")
|
||||||
user = os.environ.get("SM_ADMIN_USER", "admin")
|
user = os.environ.get("SM_ADMIN_USER", "admin")
|
||||||
if not pwd and os.path.exists(_ENV):
|
if not pwd and os.path.exists(_ENV):
|
||||||
@@ -26,6 +26,29 @@ def _get_admin_creds():
|
|||||||
return (user, pwd or "")
|
return (user, pwd or "")
|
||||||
|
|
||||||
|
|
||||||
|
def get_or_set_password():
|
||||||
|
"""启动时检查密码,如果为空则设置默认值并打印提示。"""
|
||||||
|
import os
|
||||||
|
u, p = _get_admin_creds()
|
||||||
|
if not p:
|
||||||
|
# 没有密码,设置默认值
|
||||||
|
default_pwd = "admin123"
|
||||||
|
os.environ["SM_ADMIN_PASSWORD"] = default_pwd
|
||||||
|
os.environ["SM_ADMIN_USER"] = u
|
||||||
|
# 写入 .env 文件
|
||||||
|
_ENV = os.environ.get("SM_ENV_PATH", os.path.join(os.path.dirname(__file__), ".env"))
|
||||||
|
try:
|
||||||
|
with open(_ENV, "w") as f:
|
||||||
|
f.write(f"SM_ADMIN_USER={u}\nSM_ADMIN_PASSWORD={default_pwd}\n")
|
||||||
|
print(f"⚠️ 密码未设置,已使用默认密码: {default_pwd}")
|
||||||
|
print(f" 配置文件: {_ENV}")
|
||||||
|
print(f" 请立即修改密码!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"⚠️ 密码未设置,默认: {default_pwd} (无法写入 .env: {e})")
|
||||||
|
return (u, default_pwd)
|
||||||
|
return (u, p)
|
||||||
|
|
||||||
|
|
||||||
def login_required(f):
|
def login_required(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user