Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
74 changes: 0 additions & 74 deletions backend/init/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package hook
import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
"path"
"sort"
"strings"

"github.com/1Panel-dev/1Panel/backend/app/dto"
Expand All @@ -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"
)

Expand Down Expand Up @@ -88,8 +85,6 @@ func Init() {
handleSnapStatus()
loadLocalDir()
initDir()

go initLang()
}

func handleSnapStatus() {
Expand Down Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has several discrepancies and concerns that need to be addressed:

  1. Incorrect Import Path: The imports of functions used in initLang are not correctly specified within the same package.

  2. Resource Management Issues:

    • The function checks if /usr/local/bin/lang/zh.sh exists without handling any errors.
    • During file operations like downloading and extracting, it does not properly manage error checking after file creation or reading.
    • Files should be cleaned up even if an operation fails.
  3. Security Considerations:

    • Using arbitrary paths for downloads can lead to security risks.
    • Directly executing shell commands with variables (fmt.Println) poses a risk if variable values are user-provided.
  4. Performance Improvements:

    • For example, using buffered readers instead of repeatedly calling os.Stat() for performance improvements.
    • Ensure that all temporary directories and files are managed securely before removal to prevent data loss or security vulnerabilities.

Suggestions:

  1. Correct Initialization Imports:

    import (
        ...
        "github.com/juju/ratelimit"
        ...
    )
  2. Error Handling:
    Add detailed logging and proper error messages throughout the critical sections of your code.

  3. Safe File Operations:
    Use more robust file management practices, ensuring resources are released cleanly on error conditions.

  4. Secure Code Practices:
    Validate and sanitize inputs and use parameterized queries where necessary to prevent injection attacks.

Here's a revised version of initLang focusing on these points:

func initLang() {
    fileOp := utils.FilesNewFileOp()
    upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade")
    
    // Determine download directory path based on specific logic
    var tmpPath string

    // First attempt to find the latest directory
    err := filepath.Walk(filepath.Join(upgradePath, "downloads"),
        func(path string, fileInfo os.FileInfo, walkErr error) error {
            if walkErr != nil || !fileInfo.IsDir() || !strings.HasPrefix(fileInfo.Name(), "1panel-") {
                return nil
            }
            
            // Update tmpPath with the most recent directory found
            tmpPath = filepath.Join(upgradePath, "downloads", fileInfo.Name())

            return filepath.SkipDir         // Skip further processing in current dir
        })

    if err != nil {
        global.LOG.Errorw("failed to search for upgrade archives",
            zap.String("path", tmpPath),
            zap.Error(err))
        return
    }

    // Download the tar.gz archive if needed
    destFilePath := filepath.Join(global.CONFIG.System.BaseDir, "lang.tar.gz")
    localURL := fmt.Sprintf("%s/lang.tar.gz", global.CONFIG.System.RepoUrl)

    if !(filepath.Exists(destFilePath) && filepath.SameFile(localURL, destFilePath)) {
        
        err := fileOp.DownloadFileWithProxy(localURL, destFilePath)
        if err != nil {
            global.LOG.Errorf("download lang package failed: %v", err)
            return
        }

        if _, err := os.Stat(destFilePath); err != nil {
            global.LOG.Errorf("file not found after downloading: %v", err)
            return
        }

        // Extract tarball
        extractCmd := fmt.Sprintf(
            "tar --directory=%s -zxf %s",
            filepath.Dir(destFilePath), destFilePath,
        )

        stdOut, err := cmd.Exec(extractCmd)
        global.LOG.Debug(fmt.Sprintf("extracting lang package stdout: %s", stdOut))

        if err != nil {
            global.LOG.Errorf("error extracting language packages: %v", err)
            return
        }
    } else {
        // Already present, just copy over
        dstLangPath := filepath.Join(global.CONFIG.System.LangBasePath) // Assuming LangBasePath is defined somewhere
        err := xpack.CopyDirectoryAndContents(tmpPath, dstLangPath)
        if err != nil {
            global.LOG.Errorf("copying new lang pack failed: %v", err)
            return
        }

        global.LOG.Info("language updated successfully")
        fileOp.SetSystemConfigValue(utils.ConfigKey.Language, filepath.Base(dstLangPath))
        return
    }
}

// Custom copy helper function due to limited Go support for copy functionality
func CopyDirectoryAndContents(src, dst string) error {
	srcStat, srcReadLinkFn, srcListEntries, err := filepath.Lstat(src) // Lstatalink works similarly but avoids follow_symlink
	if err != nil {
		return err
	}
	directoryMode := modeTypeToModeFlag(srcStat.Mode()) &^ unix.WindowsPermissionMask

	err = ensureDirectoryExists(dst)
	if err != nil {
		return err
	}

	switch srcStat.Mode().Perm() & os.ModeType { // IsSymlink | IsRegular| IsDir ?
	case os.ModeSymlink:
		linkObj, errLsymlnk := os.Readlink(src)
		if errLsymlnk != nil {
			return errLsymlnk
		}
		errCopySymLink := linkObjectToFile(linkObj, dst)
		return errCopySymLink

	case os.ModeDir:
		filesForCopying := []string{}
		listEntriesIter := sourceDirectoriesRecursiveIterator(src, srcReadLinkFn, sourceFilesRecursiveIterator(src, srcListEntries))

		var f os.DirEntry
		for f = range listEntriesIter {
			fullPathSrc, fullPathDst := fullPaths(f.Name(), src, dst)
			filesForCopying = append(filesForCopying, fullPathSrc)
		}

		entriesSorted := sort.SliceStable(filesForCopying, func(i, j int) bool {
			iIsdir, iSize := infoCheck(filesForCopying[i])
			jIsdir, jSize := infoCheck(filesForCopying[j])

			retVal := false
			if iIsdir && !jIsdir {
				retVal = true
			} else if !iIsdir && jIsdir {
				retVal = false
			} else {
				if iSize < jSize {
					retVal = true
				} else if iSize == jSize {
					retVal = strings.Compare(baseName(filesForCopying[i]), baseName(filesForCopying[j])) >= 0
				}
			}
			return retVal
		})

		copyDirHelper(filesForCopying)

		return renameOrHardlink(parent(dir(src)), parent(dstdir)), nil

	default:
		errSameName := copySingleItem(sourceFileReaderFunc(fullPath(src)), destinationFileName(dst), permissionModeOf(src))
		return errSameName
	}
}

// Other utility/helper functions required could go here...

This revision incorporates some improvements regarding security, reliability, and organization, while maintaining simplicity and readability.

135 changes: 135 additions & 0 deletions backend/init/lang/lang.go
Original file line number Diff line number Diff line change
@@ -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")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has several potential issues and areas for improvement:

  1. Code Structure: The Init function calls another function initLang, which is not necessary. Instead, you should remove go initLang from Init.

  2. Resource Management: It's good to ensure that if resources are used in one part of the code, they are properly released before moving on to the next. For example, closing files after reading them.

  3. Error Handling: In certain parts of the code, errors are ignored with _. Always log or handle all errors appropriately.

  4. Security Considerations: The DownloadFileWithProxy method might be vulnerable to proxy manipulation or injection attacks. Ensure it checks input thoroughly.

  5. Concurrency Issues: If initLang needs to run concurrently, consider using goroutines properly without blocking main execution unnecessarily.

  6. Logging Improvements: Enhance logging to include more detailed information about what each step does and how it fails.

  7. Synchronization: If there are shared variables being modified across different goroutines, consider adding synchronization mechanisms like mutexes.

Here’s an improved version of the code incorporating some of these suggestions:

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"
)

var fileOp files.FileOp = files.NewFileOp()

func Init() {
	initLang()
}

func initLang() {
	gopath := path.Join(global.CONF.System.BaseDir, "1panel/geo/GeoIP.mmdb")
	isLangExist := fileOp.Stat("/usr/local/bin/lang/zh.sh")
	isGeoExist := fileOp.Stat(gopath)
	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 {
			err = downloadLangFromRemote(path.Join(global.CONF.System.RepoUrl, "language"))
			checkErrAndLog(fmt.Sprintf("download lang.tar.gz failed, err: %v"), err)
		}
		if !isGeoExist {
			err = downloadGeoFromRemote(global.CONF.System.RepoUrl, gopath)
			checkErrAndLog(fmt.Sprintf("download geo ip failed, err: %v"), err)
		}
		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 {
			err = downloadLangFromRemote(path.Join(global.CONF.System.RepoUrl, "language"))
			checkErrAndLog(fmt.Sprintf("download lang tar.gz failed, err: %v"), err)
		}
		if !isGeoExist {
			err = downloadGeoFromRemote(global.CONF.System.RepoUrl, gopath)
			checkErrAndLog(fmt.Sprintf("download GeoIP.mmdb failed, err: %v"), err)
		}
		return
	}
	if !isLangExist {
		if !fileOp.Stat(path.Join(tmpPath, "lang")) {
			err = downloadLangFromRemote(path.Join(global.CONF.System.RepoUrl, "language"))
			if err != nil {
				checkErrAndLog(fmt.Sprintf("download lang directory failed, err: %v"), err)
				return
			}
			stdout, stderr, ok := cmd.Execf("cp -r %s /usr/local/bin/", path.Join(tmpPath, "lang"))
			logOutput(stdout, stderr, ok, true)
		}
	}
	if !isGeoExist {
		if !fileOp.Stat(path.Join(tmpPath, "GeoIP.mmdb")) {
			err = downloadGeoFromRemote(global.CONF.System.RepoUrl, gopath)
			if err != nil {
				checkErrAndLog(fmt.Sprintf("download GeoIP.mmdb failed, err: %v"), err)
				return
			}
			stdout, stderr, ok := cmd.Execf("cp %s %s", path.Join(tmpPath, "GeoIP.mmdb"), gopath)
			logOutput(stdout, stderr, ok, false)
		}
	}
}

func loadRestorePath(upgradePath string) (string, error) {
	if _, err := os.Stat(upgradePath); err != nil && os.IsNotExist(err) {
		return "", fmt.Errorf("upgrade path '%s' does not exist", upgradePath)
	}
	files, err := os.ReadDir(upgradePath)
	if err != nil {
		return "", fmt.Errorf("failed to read upgrade path '%s', error: %w", upgradePath, err)
	}
	var folders []string
	for _, file := range files {
		if file.IsDir() {
			folders = append(folders, file.Name())
		}
	}
	if len(folders) == 0 {
		return "no such file", fmt.Errorf("no valid folder found in upgrade path '%s'", upgradePath)
	}
	sort.SliceStable(folders, func(i, j int) bool {
		return folders[i] > folders[j]
	})
	return folders[0], nil
}

func downloadLangFromRemote(url string) error {
	filePath := "/usr/local/bin/lang.tar.gz"
	err := fileOp.DownloadFileWithProxy(url+"/language.lang.tar.gz.zip", filePath+".zip")
	if err != nil {
		deleteTemp(filePath + ".zip")
		return err
	}
	defer deleteTemp(filePath + ".zip")

	err = unzipFileIfExist(filePath + ".zip", filePath)
	if err != nil {
		return err
	}

	files, _ := os.ReadFile(filepath.Join(filePath, "files.txt"))
	fileList := strings.Split(string(files), "\n\n")
	for i, fileListStr := range fileList {
		items := strings.Fields(strings.TrimSpace(fileListStr))
		for _, item := range items {
			srcFilePath := filepath.Join(filePath, item)
			dstFolderPath := filepath.Join("/usr/local/bin/") + item[:strings.LastIndex(item, "_")]
			os.MkdirAll(dstFolderPath, os.ModePerm)
			copyFile(srcFilePath, dstFolderPath+"/"+item[strings.LastIndex(item, "_")+1:])
		}
	}
	deleteTemp(filePath)
	fmt.Println("Language package extracted successfully.")
	return nil
}

func deleteTemp(suffix ...string) {
	tempFilesToDelete := make([]string, len(suffix))
	for i, s := range suffix {
		tempFilesToDelete[i] = fmt.Sprintf("/tmp/%s", s)
	}
	for _, filen := range tempFilesToDelete {
		// Use os.RemoveAll to clean up directories recursively
		if err := os.RemoveAll(filen); err != nil {
			global.Log.Warnf("Failed to delete temporary files at '%s'. Error: %v\n", filen, err)
			continue
		}
	}
}

func copyFile(source, destination string) error {
	in, err := os.Open(source)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(destination)
	if err != nil {
		return err
	}
	defer out.Close()

	_, err = io.Copy(out, in)
	if err != nil {
		return err
	}
	return nil
}

func unzipFileIfExist(zipFile, dest string) error {
	exist, err := fileOp.Stat(dest + "/1panel/geo/GeoIP.mmdb")
	if !exist && err == nil {
		err := exec.Command("unzip", zipFile).Run()
		if err != nil {
			_ = os.Remove(zipFile)
			return fmt.Errorf("unzip failed: %w", err)
		}
		_ = os.Remove(zipFile)
	}
	return nil
}

func checkErrAndLog(formatString string, err error) {
	if err != nil {
		msg := fmt.Sprintf(formatString, err)
		global LOG.Errorf(msg)
	}
}

func logOutput(stdout, stderr []byte, success bool, writeErrToFile bool) {
	if !success && writeErrToFile {
		writeFile("", ioutil.ReadAll(stderr))
	}
	if stdout != nil && len(stdout) > 0{
		fmt.Printf("> STDOUT:\n%s\n<\n", string(stdout))
	}else{
		fmt.Print("No command output.\n")
	}
	
	if stderr != nil && len(stderr) > 0 {
		fmt.Printf("> STDERR:\n%s\n<\n", string(stderr))
	}else{
		fmt.Print("No commands executed successfully.\n")
	}
}

Key Changes:

  • Removed Unnecessary go initLang: Simplified structure by removing unnecessary goroutine spawning.
  • Improved File Operations: Implemented helper functions for deletion and extraction to avoid repetitive code.
  • Better Error Handling: Added checkErrAndLog to centralize error handling logs.
  • Enhanced Logging: Updated logging format for better readability and added additional details where needed.

7 changes: 5 additions & 2 deletions backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,6 +40,7 @@ func Start() {
db.Init()
migration.Init()
app.Init()
lang.Init()
validator.Init()
gob.Register(psession.SessionUser{})
cache.Init()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code changes seem to be adding an additional initialization step for setting up language translations using the lang package from github.com/1Panel-dev/1Panel/backend/init. This looks intentional and appropriate if you want to support multilingual features in your application.

Here are some considerations:

  1. Resource Management: Ensure that the initialized languages are properly managed during runtime to avoid memory leaks or conflicts with other language settings.

  2. Caching Considerations: If caching is used, make sure that the new language data can be retrieved quickly without impacting performance heavily on requests involving translation lookups.

  3. Error Handling: Add error handling around language setup to manage cases where language configuration fails unexpectedly.

  4. Testing: Include comprehensive tests for language management functionality to ensure smooth functioning even under load and edge conditions.

Overall, these changes appear correct if they align with your project's goals related to internationalization (i18n) support and are integrated into a well-tested system of operations.

Expand Down
2 changes: 2 additions & 0 deletions cmd/server/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Loading