Browse Source

Fixed typo in upload UI

Toby Chui 3 years ago
parent
commit
2b1710da5b
5 changed files with 51 additions and 22 deletions
  1. 1 1
      mod/apt/apt.go
  2. 32 0
      mod/updates/updates.go
  3. 1 1
      module.go
  4. 16 19
      system.info.go
  5. 1 1
      web/SystemAO/updates/index.html

+ 1 - 1
mod/apt/apt.go

@@ -56,7 +56,7 @@ func (a *AptPackageManager) InstallIfNotExists(pkgname string, mustComply bool)
 			if mustComply {
 				//Panic and terminate server process
 				log.Println("Installation failed on package: " + pkgname)
-				os.Exit(0)
+				os.Exit(1)
 			} else {
 				log.Println("Installation failed on package: " + pkgname)
 			}

+ 32 - 0
mod/updates/updates.go

@@ -2,6 +2,8 @@ package updates
 
 import (
 	"errors"
+	"io/ioutil"
+	"net/http"
 	"os"
 	"path/filepath"
 	"time"
@@ -117,3 +119,33 @@ func GetUpdateSizes(binaryURL string, webpackURL string) (int, int, error) {
 
 	return bps, wps, nil
 }
+
+func GetLauncherVersion() (string, error) {
+	//Check if there is a launcher listening to port 25576
+	client := http.Client{
+		Timeout: 3 * time.Second,
+	}
+	resp, err := client.Get("http://127.0.0.1:25576/chk")
+	if err != nil {
+		return "", errors.New("No launcher found. Unable to restart")
+	}
+
+	content, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return "", errors.New("Read launcher response failed")
+	}
+
+	return string(content), nil
+}
+
+func CheckLauncherPortResponsive() bool {
+	client := http.Client{
+		Timeout: 3 * time.Second,
+	}
+	_, err := client.Get("http://127.0.0.1:25576/chk")
+	if err != nil {
+		return false
+	}
+
+	return true
+}

+ 1 - 1
module.go

@@ -108,7 +108,7 @@ func ModuleServiceInit() {
 	err := sysdb.NewTable("module")
 	if err != nil {
 		log.Fatal(err)
-		os.Exit(0)
+		os.Exit(1)
 	}
 
 }

+ 16 - 19
system.info.go

@@ -2,7 +2,6 @@ package main
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"log"
 	"net/http"
 	"runtime"
@@ -113,13 +112,18 @@ func SystemInfoInit() {
 	//ArOZ Info do not need permission router
 	http.HandleFunc("/system/info/getArOZInfo", infoServer.GetArOZInfo)
 
-	registerSetting(settingModule{
-		Name:     "Updates",
-		Desc:     "Perform ArozOS Updates",
-		IconPath: "SystemAO/updates/img/update.png",
-		Group:    "Info",
-		StartDir: "SystemAO/updates/index.html",
-	})
+	go func() {
+		if updates.CheckLauncherPortResponsive() {
+			//Launcher port is responsive. Assume launcher exists
+			registerSetting(settingModule{
+				Name:     "Updates",
+				Desc:     "Perform ArozOS Updates",
+				IconPath: "SystemAO/updates/img/update.png",
+				Group:    "Info",
+				StartDir: "SystemAO/updates/index.html",
+			})
+		}
+	}()
 
 	//Handle updates
 	adminRouter.HandleFunc("/system/update/download", updates.HandleUpdateDownloadRequest)
@@ -127,19 +131,11 @@ func SystemInfoInit() {
 
 	//Special function for handling launcher restart, must be in this scope
 	adminRouter.HandleFunc("/system/update/restart", func(w http.ResponseWriter, r *http.Request) {
-		//Check if there is a launcher listening to port 25576
-		resp, err := http.Get("http://127.0.0.1:25576/chk")
+		launcherVersion, err := updates.GetLauncherVersion()
 		if err != nil {
-			common.SendErrorResponse(w, "No launcher found. Unable to restart")
+			sendErrorResponse(w, err.Error())
 			return
 		}
-
-		content, err := ioutil.ReadAll(resp.Body)
-		if err != nil {
-			common.SendErrorResponse(w, "Read launcher response failed")
-			return
-		}
-
 		execute, _ := common.Mv(r, "exec", true)
 		if execute == "true" && r.Method == http.MethodPost {
 			//Do the update
@@ -147,11 +143,12 @@ func SystemInfoInit() {
 			executeShutdownSequence()
 			common.SendOK(w)
 		} else if execute == "true" {
+			//Prevent redirection attack
 			w.WriteHeader(http.StatusMethodNotAllowed)
 			w.Write([]byte("405 - Method Not Allowed"))
 		} else {
 			//Return the launcher message
-			common.SendTextResponse(w, string(content))
+			common.SendTextResponse(w, string(launcherVersion))
 		}
 
 	})

+ 1 - 1
web/SystemAO/updates/index.html

@@ -34,7 +34,7 @@
                 <i class="notched circle loading icon"></i>
                 <div class="content">
                     <div class="header">
-                    Connectiong to Download Server
+                        Connecting to Download Server
                     </div>
                     <p>We're fetching that content for you.</p>
                 </div>