mirror of
https://github.com/halfwaystudent/douyin-sparkflow.git
synced 2026-09-05 15:38:58 +08:00
fix: harden scheduling and deployment flow
This commit is contained in:
@@ -58,6 +58,56 @@ function Get-EnvValue {
|
||||
return $DefaultValue
|
||||
}
|
||||
|
||||
|
||||
function Set-YamlScalar {
|
||||
param(
|
||||
[string]$Path,
|
||||
[string]$Key,
|
||||
[string]$Value
|
||||
)
|
||||
$content = if (Test-Path $Path) { @(Get-Content -Path $Path) } else { @() }
|
||||
$escapedKey = [regex]::Escape($Key)
|
||||
$found = $false
|
||||
$updated = foreach ($line in $content) {
|
||||
if ($line -match "^${escapedKey}:") {
|
||||
$found = $true
|
||||
"${Key}: ${Value}"
|
||||
} else {
|
||||
$line
|
||||
}
|
||||
}
|
||||
if (-not $found) {
|
||||
$updated = @($updated) + "${Key}: ${Value}"
|
||||
}
|
||||
Set-Content -Path $Path -Value $updated -Encoding utf8
|
||||
}
|
||||
|
||||
function Initialize-ProxyConfig {
|
||||
$configPath = "proxy/config.yaml"
|
||||
$examplePath = "proxy/config.example.yaml"
|
||||
$subscription = Get-EnvValue -Path ".env" -Key "PROXY_SUB_URL" -DefaultValue ""
|
||||
$userAgent = Get-EnvValue -Path ".env" -Key "PROXY_USER_AGENT" -DefaultValue "clash-verge/1.7.7"
|
||||
|
||||
if ($subscription) {
|
||||
$tempPath = "$configPath.tmp"
|
||||
try {
|
||||
Invoke-WebRequest -Uri $subscription -Headers @{ "User-Agent" = $userAgent } -OutFile $tempPath -UseBasicParsing
|
||||
Move-Item -Force $tempPath $configPath
|
||||
Write-Host "Proxy subscription refreshed: $configPath"
|
||||
} finally {
|
||||
Remove-Item -Force $tempPath -ErrorAction SilentlyContinue
|
||||
}
|
||||
} elseif (-not (Test-Path $configPath)) {
|
||||
Copy-Item $examplePath $configPath
|
||||
Write-Host "PROXY_SUB_URL is empty. Created a DIRECT-only proxy config."
|
||||
}
|
||||
|
||||
Set-YamlScalar -Path $configPath -Key "mixed-port" -Value "7890"
|
||||
Set-YamlScalar -Path $configPath -Key "allow-lan" -Value "true"
|
||||
Set-YamlScalar -Path $configPath -Key "bind-address" -Value "'*'"
|
||||
Set-YamlScalar -Path $configPath -Key "external-controller" -Value "'0.0.0.0:9090'"
|
||||
}
|
||||
|
||||
Require-Command docker
|
||||
docker compose version | Out-Null
|
||||
|
||||
@@ -69,7 +119,7 @@ if ($ProxySubUrl) {
|
||||
Set-EnvValue -Path ".env" -Key "PROXY_SUB_URL" -Value $ProxySubUrl
|
||||
}
|
||||
|
||||
New-Item -ItemType Directory -Force -Path "proxy", "state/cron", "state/login-profile", "DouYinSparkFlow/logs" | Out-Null
|
||||
New-Item -ItemType Directory -Force -Path "proxy", "state/cron", "state/login-profile", "state/browser-profiles", "DouYinSparkFlow/logs" | Out-Null
|
||||
if (-not (Test-Path "proxy/config.yaml")) {
|
||||
Copy-Item "proxy/config.example.yaml" "proxy/config.yaml"
|
||||
}
|
||||
@@ -81,7 +131,7 @@ if (-not (Test-Path "state/cron/root") -or (Get-Item "state/cron/root").Length -
|
||||
) | Set-Content -Path "state/cron/root" -Encoding utf8
|
||||
}
|
||||
|
||||
bash ./refresh_proxy.sh
|
||||
Initialize-ProxyConfig
|
||||
|
||||
docker compose up -d --build
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ if [ -n "$proxy_sub_url" ]; then
|
||||
set_env_value ".env" "PROXY_SUB_URL" "$proxy_sub_url"
|
||||
fi
|
||||
|
||||
mkdir -p proxy state/cron state/login-profile DouYinSparkFlow/logs
|
||||
mkdir -p proxy state/cron state/login-profile state/browser-profiles DouYinSparkFlow/logs
|
||||
if [ ! -f "proxy/config.yaml" ]; then
|
||||
cp "proxy/config.example.yaml" "proxy/config.yaml"
|
||||
fi
|
||||
|
||||
@@ -73,19 +73,32 @@ ensure_docker() {
|
||||
|
||||
prepare_repo() {
|
||||
run_root mkdir -p "$(dirname "$APP_ROOT")"
|
||||
local runtime_config_backup=""
|
||||
if [ -f "$APP_ROOT/DouYinSparkFlow/config.json" ]; then
|
||||
runtime_config_backup="$(mktemp)"
|
||||
run_root cp "$APP_ROOT/DouYinSparkFlow/config.json" "$runtime_config_backup"
|
||||
fi
|
||||
|
||||
if [ -d "$APP_ROOT/.git" ]; then
|
||||
log "Updating existing repository at $APP_ROOT"
|
||||
run_root git -C "$APP_ROOT" fetch origin "$BRANCH"
|
||||
run_root git -C "$APP_ROOT" checkout -B "$BRANCH" "origin/$BRANCH"
|
||||
run_root git -C "$APP_ROOT" reset --hard "origin/$BRANCH"
|
||||
else
|
||||
if [ -e "$APP_ROOT" ] && [ "$ACTION" = "install" ]; then
|
||||
echo "$APP_ROOT exists but is not a git checkout. Move it aside or set ACTION=update after fixing it." >&2
|
||||
if [ -e "$APP_ROOT" ]; then
|
||||
echo "$APP_ROOT exists but is not a git checkout. Back up runtime data and move the directory aside before installing." >&2
|
||||
exit 1
|
||||
fi
|
||||
log "Cloning $REPO_URL#$BRANCH into $APP_ROOT"
|
||||
run_root git clone --branch "$BRANCH" "$REPO_URL" "$APP_ROOT"
|
||||
fi
|
||||
|
||||
if [ -n "$runtime_config_backup" ]; then
|
||||
run_root mkdir -p "$APP_ROOT/DouYinSparkFlow"
|
||||
run_root cp "$runtime_config_backup" "$APP_ROOT/DouYinSparkFlow/config.json"
|
||||
rm -f "$runtime_config_backup"
|
||||
log "Restored runtime config.json after repository update"
|
||||
fi
|
||||
}
|
||||
|
||||
set_env_value() {
|
||||
@@ -143,7 +156,7 @@ prepare_runtime_files() {
|
||||
set_env_value "$env_file" "APP_ROOT" "$APP_ROOT"
|
||||
set_env_value "$env_file" "DEFAULT_SCHEDULE" "$DEFAULT_SCHEDULE"
|
||||
|
||||
for key in TZ WEB_PORT LOGIN_DESKTOP_WEB_PORT PROXY_HTTP_PORT PROXY_CONTROLLER_PORT PROXY_SUB_URL PROXY_USER_AGENT PLAYWRIGHT_BASE_IMAGE HTTP_PROXY_BUILD HTTPS_PROXY_BUILD ALL_PROXY_BUILD PIP_INDEX_URL PIP_TRUSTED_HOST; do
|
||||
for key in TZ WEB_BIND_ADDRESS WEB_PORT SPARKFLOW_SESSION_COOKIE_SECURE LOGIN_DESKTOP_BIND_ADDRESS LOGIN_DESKTOP_WEB_PORT LOGIN_DESKTOP_PUBLIC_URL PROXY_BIND_ADDRESS PROXY_HTTP_PORT PROXY_CONTROLLER_PORT PROXY_SUB_URL PROXY_USER_AGENT PLAYWRIGHT_BASE_IMAGE HTTP_PROXY_BUILD HTTPS_PROXY_BUILD ALL_PROXY_BUILD PIP_INDEX_URL PIP_TRUSTED_HOST; do
|
||||
if [ -n "${!key:-}" ]; then
|
||||
set_env_value "$env_file" "$key" "${!key}"
|
||||
fi
|
||||
@@ -164,6 +177,7 @@ prepare_runtime_files() {
|
||||
"$APP_ROOT/proxy" \
|
||||
"$APP_ROOT/state/cron" \
|
||||
"$APP_ROOT/state/login-profile" \
|
||||
"$APP_ROOT/state/browser-profiles" \
|
||||
"$APP_ROOT/DouYinSparkFlow/logs"
|
||||
|
||||
if [ ! -f "$APP_ROOT/proxy/config.yaml" ]; then
|
||||
@@ -189,7 +203,7 @@ compose_up() {
|
||||
sleep 2
|
||||
done
|
||||
if [ "$tries" -ge 30 ]; then
|
||||
echo "Proxy did not become reachable on 127.0.0.1:7890; the build may fail downloading docker/compose binaries." >&2
|
||||
echo "Proxy did not become reachable on 127.0.0.1:7890; the build may fail downloading external packages." >&2
|
||||
fi
|
||||
log "Building and starting remaining containers"
|
||||
run_root env DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1 \
|
||||
@@ -201,15 +215,22 @@ compose_up() {
|
||||
|
||||
print_summary() {
|
||||
local env_file="$APP_ROOT/.env"
|
||||
local web_port login_port host_ip
|
||||
local web_port login_port login_bind host_ip
|
||||
web_port="$(read_env_value "$env_file" WEB_PORT)"
|
||||
login_port="$(read_env_value "$env_file" LOGIN_DESKTOP_WEB_PORT)"
|
||||
login_bind="$(read_env_value "$env_file" LOGIN_DESKTOP_BIND_ADDRESS)"
|
||||
host_ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
|
||||
host_ip="${host_ip:-127.0.0.1}"
|
||||
echo
|
||||
echo "Douyin SparkFlow is running."
|
||||
echo "Web UI: http://${host_ip}:${web_port:-8787}"
|
||||
echo "Login desktop: http://${host_ip}:${login_port:-8788}/vnc.html?autoconnect=1&resize=scale&view_only=0"
|
||||
if [ "${login_bind:-127.0.0.1}" = "127.0.0.1" ]; then
|
||||
echo "Login desktop is local-only: http://127.0.0.1:${login_port:-8788}/vnc.html?autoconnect=1&resize=scale&view_only=0"
|
||||
echo "For remote access, create an SSH tunnel: ssh -L ${login_port:-8788}:127.0.0.1:${login_port:-8788} <user>@${host_ip}"
|
||||
else
|
||||
echo "Login desktop: http://${host_ip}:${login_port:-8788}/vnc.html?autoconnect=1&resize=scale&view_only=0"
|
||||
echo "Warning: public noVNC access should be protected by a firewall or VPN."
|
||||
fi
|
||||
echo
|
||||
echo "Runtime files preserved outside git: .env, state/, proxy/config.yaml, DouYinSparkFlow/logs/, usersData.json, webui_settings.json."
|
||||
echo "Update later with: ACTION=update bash $APP_ROOT/deploy/install-server.sh"
|
||||
|
||||
Reference in New Issue
Block a user