feat: add MongoDB installation && Migration

This commit is contained in:
Whispering Wind
2025-09-07 23:05:55 +03:30
committed by GitHub
parent 45e3e101d5
commit b9576cc3f0
3 changed files with 129 additions and 10 deletions

View File

@ -159,7 +159,7 @@ async def restore_api(file: UploadFile = File(...)):
detail=f"Backup is missing required configuration files: {', '.join(missing_files)}"
)
db_dump_prefix = "hysteria_panel/"
db_dump_prefix = "blitz_panel/"
if not any(name.startswith(db_dump_prefix) for name in namelist):
raise HTTPException(
status_code=400,

View File

@ -75,14 +75,49 @@ check_os_version() {
fi
}
install_mongodb() {
log_info "Installing MongoDB..."
if command -v mongod &> /dev/null; then
log_success "MongoDB is already installed"
return 0
fi
local os_name
os_name=$(grep '^ID=' /etc/os-release | cut -d= -f2)
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
if [[ "$os_name" == "ubuntu" ]]; then
local ubuntu_codename
ubuntu_codename=$(lsb_release -cs 2>/dev/null || echo "jammy")
echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.org/apt/ubuntu ${ubuntu_codename}/mongodb-org/7.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list > /dev/null
elif [[ "$os_name" == "debian" ]]; then
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" | tee /etc/apt/sources.list.d/mongodb-org-7.0.list > /dev/null
fi
apt update -qq
apt install -y -qq mongodb-org
systemctl enable mongod
systemctl start mongod
if systemctl is-active --quiet mongod; then
log_success "MongoDB installed and started successfully"
else
log_error "MongoDB installation failed or service not running"
exit 1
fi
}
install_packages() {
local REQUIRED_PACKAGES=("jq" "curl" "pwgen" "python3" "python3-pip" "python3-venv" "git" "bc" "zip" "cron" "lsof" "golang-go")
local REQUIRED_PACKAGES=("jq" "curl" "pwgen" "python3" "python3-pip" "python3-venv" "git" "bc" "zip" "cron" "lsof" "golang-go" "gnupg" "lsb-release")
local MISSING_PACKAGES=()
log_info "Checking required packages..."
for package in "${REQUIRED_PACKAGES[@]}"; do
if ! command -v "$package" &> /dev/null; then
if ! command -v "$package" &> /dev/null && ! dpkg -l | grep -q "^ii.*$package "; then
MISSING_PACKAGES+=("$package")
else
log_success "Package $package is already installed"
@ -106,6 +141,8 @@ install_packages() {
else
log_success "All required packages are already installed."
fi
install_mongodb
}
clone_repository() {

View File

@ -11,6 +11,7 @@ REPO_URL="https://github.com/ReturnFI/Blitz"
REPO_BRANCH="main"
GEOSITE_URL="https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geosite.dat"
GEOIP_URL="https://raw.githubusercontent.com/Chocolate4U/Iran-v2ray-rules/release/geoip.dat"
USERS_FILE="$HYSTERIA_INSTALL_DIR/users.json"
# ========== Color Setup ==========
GREEN=$(tput setaf 2)
@ -46,14 +47,93 @@ for SERVICE in "${ALL_SERVICES[@]}"; do
fi
done
# ========== New Function to Install MongoDB ==========
install_mongodb() {
info "Checking for MongoDB..."
if ! command -v mongod &>/dev/null; then
warn "MongoDB not found. Attempting to install from official repository..."
apt-get update -qq >/dev/null
apt-get install -y gnupg curl >/dev/null
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/7.0 main" > /etc/apt/sources.list.d/mongodb-org-7.0.list
apt-get update -qq >/dev/null
apt-get install -y mongodb-org >/dev/null
systemctl start mongod
systemctl enable mongod
success "MongoDB installed and started successfully."
else
success "MongoDB is already installed."
fi
}
# ========== New Function to Install Go and Compile Auth Binary ==========
# ========== New Function to Migrate users.json to MongoDB ==========
migrate_users_to_mongodb() {
info "Checking for user data to migrate..."
if [ ! -f "$USERS_FILE" ]; then
warn "users.json not found. No data to migrate."
return
fi
info "Starting user data migration from users.json to MongoDB..."
python3 - <<EOF
import json
import os
import pymongo
users_json_path = "$USERS_FILE"
db_name = "blitz_panel"
collection_name = "users"
try:
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client[db_name]
collection = db[collection_name]
client.server_info()
except Exception as e:
print(f"Error connecting to MongoDB: {e}")
exit(1)
with open(users_json_path, 'r') as f:
users_data = json.load(f)
migrated_count = 0
for username, data in users_data.items():
user_doc = {
"password": data.get("password"),
"max_download_bytes": data.get("max_download_bytes", 0),
"expiration_days": data.get("expiration_days", 0),
"account_creation_date": data.get("account_creation_date"),
"blocked": data.get("blocked", False),
"unlimited_user": data.get("unlimited_user", False),
"status": data.get("status", "Offline"),
"upload_bytes": data.get("upload_bytes", 0),
"download_bytes": data.get("download_bytes", 0)
}
user_doc = {k: v for k, v in user_doc.items() if v is not None}
collection.update_one(
{"_id": username.lower()},
{"$set": user_doc},
upsert=True
)
migrated_count += 1
print(f"Successfully migrated {migrated_count} users to MongoDB.")
EOF
mv "$USERS_FILE" "${USERS_FILE}.migrated"
success "users.json has been migrated and renamed to users.json.migrated."
}
# ========== Install Go and Compile Auth Binary ==========
install_go_and_compile_auth() {
info "Checking for Go and compiling authentication binary..."
if ! command -v go &>/dev/null; then
warn "Go is not installed. Attempting to install..."
#apt-get update -qq >/dev/null
apt install golang-go -y
apt-get install golang-go -y >/dev/null
success "Go installed successfully."
else
success "Go is already installed."
@ -149,7 +229,8 @@ chmod 640 "$HYSTERIA_INSTALL_DIR/ca.key" "$HYSTERIA_INSTALL_DIR/ca.crt"
chown -R hysteria:hysteria "$HYSTERIA_INSTALL_DIR/core/scripts/telegrambot"
chmod +x "$HYSTERIA_INSTALL_DIR/core/scripts/hysteria2/kick.py"
# ========== Virtual Environment ==========
# ========== Install Dependencies ==========
install_mongodb
info "Setting up virtual environment and installing dependencies..."
cd "$HYSTERIA_INSTALL_DIR"
python3 -m venv "$HYSTERIA_VENV_DIR"
@ -158,7 +239,8 @@ pip install --upgrade pip >/dev/null
pip install -r requirements.txt >/dev/null
success "Python environment ready."
# ========== Compile Go Binary ==========
# ========== Migrate Data and Compile Go Binary ==========
migrate_users_to_mongodb
install_go_and_compile_auth
# ========== Systemd Services ==========