diff --git a/agent/app/service/agents_utils.go b/agent/app/service/agents_utils.go index 3659fab616f1..274f71666c7a 100644 --- a/agent/app/service/agents_utils.go +++ b/agent/app/service/agents_utils.go @@ -21,7 +21,6 @@ import ( "github.com/1Panel-dev/1Panel/agent/global" "github.com/1Panel-dev/1Panel/agent/utils/common" "github.com/1Panel-dev/1Panel/agent/utils/files" - openclawutil "github.com/1Panel-dev/1Panel/agent/utils/openclaw" "github.com/1Panel-dev/1Panel/agent/utils/req_helper" "gorm.io/gorm" ) @@ -388,9 +387,6 @@ func migrateOpenclawHTTPSUpgradeWithSystemIP(install *model.AppInstall, fromVers return nil } migrateOpenclawInstallPorts(install) - if err := openclawutil.WriteCatchAllCaddyfile(install.GetPath()); err != nil { - return err - } configPath := path.Join(install.GetPath(), "data", "conf", "openclaw.json") var allowedOrigins []string if conf, err := readOpenclawConfig(configPath); err == nil { @@ -417,6 +413,35 @@ func migrateOpenclawHTTPSUpgradeWithSystemIP(install *model.AppInstall, fromVers return migrateOpenclawInstallEnv(install, allowedOrigins) } +func shouldRewriteOpenclawBundledCaddyfileOnUpgrade(install *model.AppInstall, fromVersion, toVersion string) bool { + if install == nil || install.App.Key != constant.AppOpenclaw { + return false + } + if strings.TrimSpace(fromVersion) != openclawHTTPSVersion { + return false + } + return common.CompareVersion(strings.TrimSpace(toVersion), openclawHTTPSVersion) +} + +func rewriteOpenclawBundledCaddyfileOnUpgrade(install *model.AppInstall, fromVersion, toVersion, sourceAppDir string) error { + if !shouldRewriteOpenclawBundledCaddyfileOnUpgrade(install, fromVersion, toVersion) { + return nil + } + return copyOpenclawBundledCaddyfile(sourceAppDir, install.GetPath()) +} + +func copyOpenclawBundledCaddyfile(sourceAppDir, installPath string) error { + sourcePath := path.Join(sourceAppDir, "data", "caddy", "Caddyfile") + targetDir := path.Join(installPath, "data", "caddy") + fileOp := files.NewFileOp() + if !fileOp.Stat(targetDir) { + if err := fileOp.CreateDir(targetDir, constant.DirPerm); err != nil { + return err + } + } + return fileOp.CopyFile(sourcePath, targetDir) +} + func migrateOpenclawInstallPorts(install *model.AppInstall) { if install == nil { return diff --git a/agent/app/service/app_utils.go b/agent/app/service/app_utils.go index d7c82541fb6d..436b8b1b05f3 100644 --- a/agent/app/service/app_utils.go +++ b/agent/app/service/app_utils.go @@ -805,6 +805,9 @@ func upgradeInstall(req request.AppInstallUpgrade) error { if err = migrateOpenclawHTTPSUpgrade(&install, oldVersion, detail.Version); err != nil { return err } + if err = rewriteOpenclawBundledCaddyfileOnUpgrade(&install, oldVersion, detail.Version, detailDir); err != nil { + return err + } if req.DockerCompose == "" { newCompose, err = getUpgradeCompose(install, detail) if err != nil { diff --git a/agent/init/migration/migrate.go b/agent/init/migration/migrate.go index b28386e47adb..b1c785f333e1 100644 --- a/agent/init/migration/migrate.go +++ b/agent/init/migration/migrate.go @@ -72,7 +72,6 @@ func InitAgentDB() { migrations.AddAgentTypeForAgents, migrations.NormalizeAgentAccountVerifiedStatus, migrations.NormalizeOllamaAccountAPIType, - migrations.RewriteOpenclawBundledCaddyfile, migrations.InitAgentAccountModelPool, migrations.AddHostTable, migrations.AddAITerminalSettings, diff --git a/agent/init/migration/migrations/init.go b/agent/init/migration/migrations/init.go index 0654432ea8bc..2b7268239645 100644 --- a/agent/init/migration/migrations/init.go +++ b/agent/init/migration/migrations/init.go @@ -989,13 +989,6 @@ var NormalizeOllamaAccountAPIType = &gormigrate.Migration{ }, } -var RewriteOpenclawBundledCaddyfile = &gormigrate.Migration{ - ID: "20260318-rewrite-openclaw-bundled-caddyfile", - Migrate: func(tx *gorm.DB) error { - return migrationutils.RewriteOpenclawBundledCaddyfile(tx) - }, -} - var InitAgentAccountModelPool = &gormigrate.Migration{ ID: "20260319-init-agent-account-model-pool", Migrate: func(tx *gorm.DB) error { diff --git a/agent/init/migration/migrations/utils/openclaw_caddyfile.go b/agent/init/migration/migrations/utils/openclaw_caddyfile.go deleted file mode 100644 index 75079a678063..000000000000 --- a/agent/init/migration/migrations/utils/openclaw_caddyfile.go +++ /dev/null @@ -1,42 +0,0 @@ -package utils - -import ( - "strings" - - "github.com/1Panel-dev/1Panel/agent/app/model" - "github.com/1Panel-dev/1Panel/agent/constant" - openclawutil "github.com/1Panel-dev/1Panel/agent/utils/openclaw" - - "gorm.io/gorm" -) - -const ( - openclawVersionWithBundledCaddyMigration = "2026.3.13" -) - -func RewriteOpenclawCaddyfileForVersion(tx *gorm.DB, version string) error { - targetVersion := strings.TrimSpace(version) - if targetVersion == "" { - return nil - } - var installs []model.AppInstall - if err := tx.Preload("App").Find(&installs).Error; err != nil { - return err - } - for _, install := range installs { - if install.App.Key != constant.AppOpenclaw { - continue - } - if strings.TrimSpace(install.Version) != targetVersion { - continue - } - if err := openclawutil.WriteCatchAllCaddyfile(install.GetPath()); err != nil { - return err - } - } - return nil -} - -func RewriteOpenclawBundledCaddyfile(tx *gorm.DB) error { - return RewriteOpenclawCaddyfileForVersion(tx, openclawVersionWithBundledCaddyMigration) -} diff --git a/agent/utils/openclaw/caddyfile.go b/agent/utils/openclaw/caddyfile.go deleted file mode 100644 index 3342c03cbf43..000000000000 --- a/agent/utils/openclaw/caddyfile.go +++ /dev/null @@ -1,47 +0,0 @@ -package openclaw - -import ( - "fmt" - "os" - "path" - - "github.com/1Panel-dev/1Panel/agent/constant" -) - -const ( - gatewayPort = 18789 - caddyPort = 8443 - caddyDataPerm = 0o777 -) - -func BuildCatchAllCaddyfile() string { - return fmt.Sprintf(`{ - admin off - auto_https disable_redirects - skip_install_trust - storage file_system { - root /data/caddy - } -} - -https://:%d { - bind 0.0.0.0 - tls internal { - on_demand - } - reverse_proxy 127.0.0.1:%d -} -`, caddyPort, gatewayPort) -} - -func WriteCatchAllCaddyfile(installPath string) error { - caddyDir := path.Join(installPath, "data", "caddy") - caddyDataDir := path.Join(caddyDir, "data") - if err := os.MkdirAll(caddyDataDir, constant.DirPerm); err != nil { - return err - } - if err := os.Chmod(caddyDataDir, caddyDataPerm); err != nil { - return err - } - return os.WriteFile(path.Join(caddyDir, "Caddyfile"), []byte(BuildCatchAllCaddyfile()), constant.FilePerm) -}