diff --git a/backend/app/service/upgrade.go b/backend/app/service/upgrade.go index 2b357ce2d..378632e79 100644 --- a/backend/app/service/upgrade.go +++ b/backend/app/service/upgrade.go @@ -144,6 +144,9 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error { return } _, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(tmpDir, "lang")) + geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo") + _, _ = cmd.Execf("mkdir %s && cp %s %s/", geoPath, path.Join(tmpDir, "GeoIP.mmdb"), geoPath) + if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin/1pctl", global.CONF.System.BaseDir); err != nil { global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err) u.handleRollback(originalDir, 2) @@ -177,6 +180,7 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e return err } _, _ = cmd.Execf("cp -r /usr/local/bin/lang %s", originalDir) + _, _ = cmd.Execf("cp %s %s", path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb"), originalDir) checkPointOfWal() if err := handleTar(path.Join(global.CONF.System.BaseDir, "1panel/db"), originalDir, "db.tar.gz", "db/1Panel.db-*", ""); err != nil { return err @@ -208,6 +212,7 @@ func (u *UpgradeService) handleRollback(originalDir string, errStep int) { global.LOG.Errorf("rollback 1panel failed, err: %v", err) } _, _ = cmd.Execf("cp -r %s /usr/local/bin", path.Join(originalDir, "lang")) + _, _ = cmd.Execf("cp %s %s", path.Join(originalDir, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/")) if errStep == 2 { return diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index 857bfda1b..b0334966f 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -3,10 +3,8 @@ package hook import ( "encoding/base64" "encoding/json" - "fmt" "os" "path" - "sort" "strings" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -17,7 +15,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/encrypt" - "github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/xpack" ) @@ -88,8 +85,6 @@ func Init() { handleSnapStatus() loadLocalDir() initDir() - - go initLang() } func handleSnapStatus() { @@ -265,72 +260,3 @@ func handleCronJobAlert(cronjob *model.Cronjob) { return } } - -func initLang() { - fileOp := files.NewFileOp() - upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") - if fileOp.Stat("/usr/local/bin/lang/zh.sh") { - return - } - tmpPath, err := loadRestorePath(upgradePath) - files, _ := os.ReadDir(path.Join(upgradePath, tmpPath, "downloads")) - if len(files) == 0 { - tmpPath = "no such file" - } else { - for _, item := range files { - if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") { - tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name(), "lang") - break - } - } - } - - if err != nil || tmpPath == "no such file" || !fileOp.Stat(tmpPath) { - path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl) - if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil { - global.LOG.Errorf("download lang.tar.gz failed, err: %v", err) - return - } - if !fileOp.Stat("/usr/local/bin/lang.tar.gz") { - global.LOG.Errorf("download lang.tar.gz failed, no such file, err: %v", err) - return - } - std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/") - if err != nil { - fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err) - return - } - _ = os.Remove("/usr/local/bin/lang.tar.gz") - global.LOG.Info("init lang for 1pctl successful") - return - } - std, err := cmd.Execf("cp -r %s %s", tmpPath, "/usr/local/bin/") - if err != nil { - fmt.Printf("load lang from package failed, std: %s, err: %v", std, err) - return - } - global.LOG.Info("init lang for 1pctl successful") -} - -func loadRestorePath(upgradeDir string) (string, error) { - if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) { - return "no such file", nil - } - files, err := os.ReadDir(upgradeDir) - if err != nil { - return "", err - } - var folders []string - for _, file := range files { - if file.IsDir() { - folders = append(folders, file.Name()) - } - } - if len(folders) == 0 { - return "no such file", nil - } - sort.Slice(folders, func(i, j int) bool { - return folders[i] > folders[j] - }) - return folders[0], nil -} diff --git a/backend/init/lang/lang.go b/backend/init/lang/lang.go new file mode 100644 index 000000000..132c5f504 --- /dev/null +++ b/backend/init/lang/lang.go @@ -0,0 +1,135 @@ +package lang + +import ( + "fmt" + "os" + "path" + "sort" + "strings" + + "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/1Panel-dev/1Panel/backend/utils/files" +) + +func Init() { + go initLang() +} + +func initLang() { + fileOp := files.NewFileOp() + geoPath := path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb") + isLangExist := fileOp.Stat("/usr/local/bin/lang/zh.sh") + isGeoExist := fileOp.Stat(geoPath) + if isLangExist && isGeoExist { + return + } + upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") + tmpPath, err := loadRestorePath(upgradePath) + upgradeDir := path.Join(upgradePath, tmpPath, "downloads") + if err != nil || len(tmpPath) == 0 || !fileOp.Stat(upgradeDir) { + if !isLangExist { + downloadLangFromRemote(fileOp) + } + if !isGeoExist { + downloadGeoFromRemote(fileOp, geoPath) + } + return + } + + files, _ := os.ReadDir(upgradeDir) + if len(files) == 0 { + tmpPath = "no such file" + } else { + for _, item := range files { + if item.IsDir() && strings.HasPrefix(item.Name(), "1panel-") { + tmpPath = path.Join(upgradePath, tmpPath, "downloads", item.Name()) + break + } + } + } + if tmpPath == "no such file" || !fileOp.Stat(tmpPath) { + if !isLangExist { + downloadLangFromRemote(fileOp) + } + if !isGeoExist { + downloadGeoFromRemote(fileOp, geoPath) + } + return + } + if !isLangExist { + if !fileOp.Stat(path.Join(tmpPath, "lang")) { + downloadLangFromRemote(fileOp) + return + } + std, err := cmd.Execf("cp -r %s %s", path.Join(tmpPath, "lang"), "/usr/local/bin/") + if err != nil { + global.LOG.Errorf("load lang from package failed, std: %s, err: %v", std, err) + return + } + global.LOG.Info("init lang successful") + } + if !isGeoExist { + if !fileOp.Stat(path.Join(tmpPath, "GeoIP.mmdb")) { + downloadGeoFromRemote(fileOp, geoPath) + return + } + std, err := cmd.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Dir(geoPath)) + if err != nil { + global.LOG.Errorf("load geo ip from package failed, std: %s, err: %v", std, err) + return + } + global.LOG.Info("init geo ip successful") + } +} + +func loadRestorePath(upgradeDir string) (string, error) { + if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) { + return "no such file", nil + } + files, err := os.ReadDir(upgradeDir) + if err != nil { + return "", err + } + var folders []string + for _, file := range files { + if file.IsDir() { + folders = append(folders, file.Name()) + } + } + if len(folders) == 0 { + return "no such file", nil + } + sort.Slice(folders, func(i, j int) bool { + return folders[i] > folders[j] + }) + return folders[0], nil +} + +func downloadLangFromRemote(fileOp files.FileOp) { + path := fmt.Sprintf("%s/language/lang.tar.gz", global.CONF.System.RepoUrl) + if err := fileOp.DownloadFileWithProxy(path, "/usr/local/bin/lang.tar.gz"); err != nil { + global.LOG.Errorf("download lang.tar.gz failed, err: %v", err) + return + } + if !fileOp.Stat("/usr/local/bin/lang.tar.gz") { + global.LOG.Error("download lang.tar.gz failed, no such file") + return + } + std, err := cmd.Execf("tar zxvfC %s %s", "/usr/local/bin/lang.tar.gz", "/usr/local/bin/") + if err != nil { + fmt.Printf("decompress lang.tar.gz failed, std: %s, err: %v", std, err) + return + } + _ = os.Remove("/usr/local/bin/lang.tar.gz") + global.LOG.Info("download lang successful") +} +func downloadGeoFromRemote(fileOp files.FileOp, targetPath string) { + _ = os.MkdirAll(path.Dir(targetPath), os.ModePerm) + pathItem := fmt.Sprintf("%s/geo/GeoIP.mmdb", global.CONF.System.RepoUrl) + if err := fileOp.DownloadFileWithProxy(pathItem, targetPath); err != nil { + global.LOG.Errorf("download geo ip failed, err: %v", err) + return + } + global.LOG.Info("download geo ip successful") +} diff --git a/backend/server/server.go b/backend/server/server.go index eb5a4973b..c065bd0e7 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -4,15 +4,17 @@ import ( "crypto/tls" "encoding/gob" "fmt" - "github.com/1Panel-dev/1Panel/backend/constant" - "github.com/1Panel-dev/1Panel/backend/i18n" "net" "net/http" "os" "path" + "github.com/1Panel-dev/1Panel/backend/constant" + "github.com/1Panel-dev/1Panel/backend/i18n" + "github.com/1Panel-dev/1Panel/backend/init/app" "github.com/1Panel-dev/1Panel/backend/init/business" + "github.com/1Panel-dev/1Panel/backend/init/lang" "github.com/1Panel-dev/1Panel/backend/cron" "github.com/1Panel-dev/1Panel/backend/init/cache" @@ -38,6 +40,7 @@ func Start() { db.Init() migration.Init() app.Init() + lang.Init() validator.Init() gob.Register(psession.SessionUser{}) cache.Init() diff --git a/cmd/server/cmd/restore.go b/cmd/server/cmd/restore.go index 391a00094..449a58d22 100644 --- a/cmd/server/cmd/restore.go +++ b/cmd/server/cmd/restore.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/i18n" cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" @@ -54,6 +55,7 @@ var restoreCmd = &cobra.Command{ return err } _, _ = cmdUtils.Execf("cp -r %s /usr/local/bin", path.Join(tmpPath, "lang")) + _, _ = cmdUtils.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), path.Join(global.CONF.System.BaseDir, "1panel/geo/")) fmt.Println(i18n.GetMsgByKeyForCmd("RestoreStep3")) if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil { return err