LinkLeong 2 tahun lalu
induk
melakukan
eb31bf5586
7 mengubah file dengan 91 tambahan dan 2 penghapusan
  1. 3 1
      pkg/utils/common_err/e.go
  2. 11 0
      route/periodical.go
  3. 1 0
      route/route.go
  4. 6 1
      route/v1/docker.go
  5. 20 0
      route/v1/samba.go
  6. 27 0
      route/v1/system.go
  7. 23 0
      service/system.go

+ 3 - 1
pkg/utils/common_err/e.go

@@ -30,6 +30,7 @@ const (
 	Record_NOT_EXIST                = 20007
 	Record_ALREADY_EXIST            = 20008
 	SERVICE_NOT_RUNNING             = 20009
+	CHARACTER_LIMIT                 = 20010
 
 	//disk
 	NAME_NOT_AVAILABLE       = 40001
@@ -85,6 +86,7 @@ var MsgFlags = map[int]string{
 	Record_ALREADY_EXIST: "Record already exists",
 	Record_NOT_EXIST:     "Record does not exist",
 	SERVICE_NOT_RUNNING:  "Service is not running",
+	CHARACTER_LIMIT:      "Only uppercase letters, lowercase letters and numbers are allowed for username and password.",
 
 	//app
 	UNINSTALL_APP_ERROR:  "Error uninstalling app",
@@ -113,7 +115,7 @@ var MsgFlags = map[int]string{
 	COMMAND_ERROR_INVALID_OPERATION: "invalid operation",
 }
 
-//获取错误信息
+// 获取错误信息
 func GetMsg(code int) string {
 	msg, ok := MsgFlags[code]
 	if ok {

+ 11 - 0
route/periodical.go

@@ -70,12 +70,23 @@ func SendAllHardwareStatusBySocket() {
 		}
 	}
 	cpu := service.MyService.System().GetCpuPercent()
+
+	var cpuModel = "arm"
+	if cpu := service.MyService.System().GetCpuInfo(); len(cpu) > 0 {
+		if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "intel") > 0 {
+			cpuModel = "intel"
+		} else if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "amd") > 0 {
+			cpuModel = "amd"
+		}
+	}
+
 	num := service.MyService.System().GetCpuCoreNum()
 	cpuData := make(map[string]interface{})
 	cpuData["percent"] = cpu
 	cpuData["num"] = num
 	cpuData["temperature"] = service.MyService.System().GetCPUTemperature()
 	cpuData["power"] = service.MyService.System().GetCPUPower()
+	cpuData["model"] = cpuModel
 
 	memInfo := service.MyService.System().GetMemInfo()
 

+ 1 - 0
route/route.go

@@ -152,6 +152,7 @@ func InitRouter() *gin.Engine {
 			// v1SysGroup.GET("/port", v1.GetCasaOSPort)
 			// v1SysGroup.PUT("/port", v1.PutCasaOSPort)
 			v1SysGroup.GET("/proxy", v1.GetSystemProxy)
+			v1SysGroup.PUT("/state/:state", v1.PutSystemState)
 		}
 		v1PortGroup := v1Group.Group("/port")
 		v1PortGroup.Use()

+ 6 - 1
route/v1/docker.go

@@ -622,6 +622,11 @@ func UnInstallApp(c *gin.Context) {
 		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
 		return
 	}
+
+	j := make(map[string]string)
+	c.ShouldBind(&j)
+	isDelete := j["delete_config_folder"]
+
 	//info := service.MyService.App().GetUninstallInfo(appId)
 
 	info, err := service.MyService.Docker().DockerContainerInfo(appId)
@@ -646,7 +651,7 @@ func UnInstallApp(c *gin.Context) {
 	// step:remove image
 	service.MyService.Docker().DockerImageRemove(info.Config.Image)
 
-	if info.Config.Labels["origin"] != "custom" {
+	if info.Config.Labels["origin"] != "custom" && len(isDelete) > 0 {
 		//step: 删除文件夹
 		for _, v := range info.Mounts {
 			if strings.Contains(v.Source, info.Name) {

+ 20 - 0
route/v1/samba.go

@@ -14,12 +14,14 @@ import (
 	"fmt"
 	"os"
 	"path/filepath"
+	"regexp"
 	"strings"
 
 	"github.com/IceWhaleTech/CasaOS/model"
 	"github.com/IceWhaleTech/CasaOS/pkg/samba"
 	"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
 	"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
+	"github.com/IceWhaleTech/CasaOS/pkg/utils/ip_helper"
 	"github.com/IceWhaleTech/CasaOS/service"
 	model2 "github.com/IceWhaleTech/CasaOS/service/model"
 	"github.com/gin-gonic/gin"
@@ -125,9 +127,27 @@ func PostSambaConnectionsCreate(c *gin.Context) {
 		connection.Port = "445"
 	}
 	if connection.Username == "" || connection.Host == "" {
+		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CHARACTER_LIMIT, Message: common_err.GetMsg(common_err.CHARACTER_LIMIT)})
+		return
+	}
+
+	if ok, _ := regexp.MatchString("^[a-zA-Z0-9]{4,30}$", connection.Password); !ok {
+		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.CHARACTER_LIMIT, Message: common_err.GetMsg(common_err.CHARACTER_LIMIT)})
+		return
+	}
+	if ok, _ := regexp.MatchString("^[a-zA-Z0-9]{4,30}$", connection.Username); !ok {
+		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
+		return
+	}
+	if !ip_helper.IsIPv4(connection.Host) && !ip_helper.IsIPv6(connection.Host) {
+		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
+		return
+	}
+	if ok, _ := regexp.MatchString("^[0-9]{1,6}$", connection.Port); !ok {
 		c.JSON(common_err.CLIENT_ERROR, model.Result{Success: common_err.INVALID_PARAMS, Message: common_err.GetMsg(common_err.INVALID_PARAMS)})
 		return
 	}
+
 	connection.Host = strings.Split(connection.Host, "/")[0]
 	// check is exists
 	connections := service.MyService.Connections().GetConnectionByHost(connection.Host)

+ 27 - 0
route/v1/system.go

@@ -228,11 +228,20 @@ func GetSystemUtilization(c *gin.Context) {
 	data := make(map[string]interface{})
 	cpu := service.MyService.System().GetCpuPercent()
 	num := service.MyService.System().GetCpuCoreNum()
+	var cpuModel = "arm"
+	if cpu := service.MyService.System().GetCpuInfo(); len(cpu) > 0 {
+		if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "intel") > 0 {
+			cpuModel = "intel"
+		} else if strings.Count(strings.ToLower(strings.TrimSpace(cpu[0].ModelName)), "amd") > 0 {
+			cpuModel = "amd"
+		}
+	}
 	cpuData := make(map[string]interface{})
 	cpuData["percent"] = cpu
 	cpuData["num"] = num
 	cpuData["temperature"] = service.MyService.System().GetCPUTemperature()
 	cpuData["power"] = service.MyService.System().GetCPUPower()
+	cpuData["model"] = cpuModel
 
 	data["cpu"] = cpuData
 	data["mem"] = service.MyService.System().GetMemInfo()
@@ -358,3 +367,21 @@ func GetSystemProxy(c *gin.Context) {
 	// 复制转发的响应Body到响应Body
 	io.Copy(c.Writer, ioutil.NopCloser(bytes.NewBuffer(rda)))
 }
+
+func PutSystemState(c *gin.Context) {
+	state := c.Param("state")
+	if state == "off" {
+		go func() {
+			time.Sleep(30 * time.Second)
+			service.MyService.System().SystemShutdown()
+		}()
+
+	} else if state == "restart" {
+		go func() {
+			time.Sleep(30 * time.Second)
+			service.MyService.System().SystemReboot()
+		}()
+
+	}
+	c.JSON(http.StatusOK, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: "The operation will be executed after 30 seconds"})
+}

+ 23 - 0
service/system.go

@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	net2 "net"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"runtime"
 	"strconv"
@@ -52,6 +53,8 @@ type SystemService interface {
 	GetCPUTemperature() int
 	GetCPUPower() map[string]string
 	GetMacAddress() (string, error)
+	SystemReboot() error
+	SystemShutdown() error
 }
 type systemService struct{}
 
@@ -364,6 +367,26 @@ func (s *systemService) GetCPUPower() map[string]string {
 	return data
 }
 
+func (s *systemService) SystemReboot() error {
+	arg := []string{"6"}
+	cmd := exec.Command("init", arg...)
+	_, err := cmd.CombinedOutput()
+	if err != nil {
+		return err
+	}
+	return nil
+}
+func (s *systemService) SystemShutdown() error {
+	arg := []string{"0"}
+	cmd := exec.Command("init", arg...)
+	_, err := cmd.CombinedOutput()
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
 func NewSystemService() SystemService {
+
 	return &systemService{}
 }