Prechádzať zdrojové kódy

Pushing labtop work to pc

tobychui 9 mesiacov pred
rodič
commit
f47d0bc048
2 zmenil súbory, kde vykonal 40 pridanie a 8 odobranie
  1. 8 8
      main.go
  2. 32 0
      mod/update/update.go

+ 8 - 8
main.go

@@ -53,7 +53,7 @@ var enableHighSpeedGeoIPLookup = flag.Bool("fastgeoip", false, "Enable high spee
 var staticWebServerRoot = flag.String("webroot", "./www", "Static web server root folder. Only allow chnage in start paramters")
 var allowWebFileManager = flag.Bool("webfm", true, "Enable web file manager for static web server root folder")
 var logOutputToFile = flag.Bool("log", true, "Log terminal output to file")
-var updateMode = flag.Int("update", 0, "Version number (usually the version before you update Zoraxy) to start accumulation update. To update v3.0.7 to latest, use -update=307")
+var enableAutoUpdate = flag.Bool("cfgupgrade", true, "Enable auto config upgrade if breaking change is detected")
 
 var (
 	name        = "Zoraxy"
@@ -123,8 +123,9 @@ func ShutdownSeq() {
 		// Stop the mdns service
 		mdnsTickerStop <- true
 	}
-
 	mdnsScanner.Close()
+	fmt.Println("- Shutting down load balancer")
+	loadBalancer.Close()
 	fmt.Println("- Closing Certificates Auto Renewer")
 	acmeAutoRenewer.Close()
 	//Remove the tmp folder
@@ -147,17 +148,16 @@ func main() {
 		os.Exit(0)
 	}
 
-	if *updateMode > 306 {
-		fmt.Println("Entering Update Mode")
-		update.RunConfigUpdate(*updateMode, update.GetVersionIntFromVersionNumber(version))
-		os.Exit(0)
-	}
-
 	if !utils.ValidateListeningAddress(*webUIPort) {
 		fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
 		os.Exit(0)
 	}
 
+	if *enableAutoUpdate {
+		log.Println("[INFO] Checking required config update")
+		update.RunConfigUpdate(0, update.GetVersionIntFromVersionNumber(version))
+	}
+
 	SetupCloseHandler()
 
 	//Read or create the system uuid

+ 32 - 0
mod/update/update.go

@@ -9,21 +9,52 @@ package update
 
 import (
 	"fmt"
+	"os"
 	"strconv"
 	"strings"
 
 	v308 "imuslab.com/zoraxy/mod/update/v308"
+	"imuslab.com/zoraxy/mod/utils"
 )
 
 // Run config update. Version numbers are int. For example
 // to update 3.0.7 to 3.0.8, use RunConfigUpdate(307, 308)
 // This function support cross versions updates (e.g. 307 -> 310)
 func RunConfigUpdate(fromVersion int, toVersion int) {
+	versionFile := "./conf/version"
+	if fromVersion == 0 {
+		//Run auto previous version detection
+		fromVersion = 307
+		if utils.FileExists(versionFile) {
+			//Read the version file
+			previousVersionText, err := os.ReadFile(versionFile)
+			if err != nil {
+				panic("Unable to read version file at " + versionFile)
+			}
+
+			//Convert the version to int
+			versionInt, err := strconv.Atoi(strings.TrimSpace(string(previousVersionText)))
+			if err != nil {
+				panic("Unable to read version file at " + versionFile)
+			}
+
+			fromVersion = versionInt
+		}
+
+		if fromVersion == toVersion {
+			//No need to update
+			return
+		}
+	}
+
+	//Do iterate update
 	for i := fromVersion; i < toVersion; i++ {
 		oldVersion := fromVersion
 		newVersion := fromVersion + 1
 		fmt.Println("Updating from v", oldVersion, " to v", newVersion)
 		runUpdateRoutineWithVersion(oldVersion, newVersion)
+		//Write the updated version to file
+		os.WriteFile(versionFile, []byte(strconv.Itoa(newVersion)), 0775)
 	}
 	fmt.Println("Update completed")
 }
@@ -36,6 +67,7 @@ func GetVersionIntFromVersionNumber(version string) int {
 
 func runUpdateRoutineWithVersion(fromVersion int, toVersion int) {
 	if fromVersion == 307 && toVersion == 308 {
+		//Updating from v3.0.7 to v3.0.8
 		err := v308.UpdateFrom307To308()
 		if err != nil {
 			panic(err)