From eb31bf5586a82791f76b623f2a5eb5f8686c15b1 Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Tue, 29 Nov 2022 02:25:26 +0000 Subject: [PATCH] wip --- pkg/utils/common_err/e.go | 4 +++- route/periodical.go | 11 +++++++++++ route/route.go | 1 + route/v1/docker.go | 7 ++++++- route/v1/samba.go | 20 ++++++++++++++++++++ route/v1/system.go | 27 +++++++++++++++++++++++++++ service/system.go | 23 +++++++++++++++++++++++ 7 files changed, 91 insertions(+), 2 deletions(-) diff --git a/pkg/utils/common_err/e.go b/pkg/utils/common_err/e.go index cc2a272..605b9b8 100644 --- a/pkg/utils/common_err/e.go +++ b/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 { diff --git a/route/periodical.go b/route/periodical.go index 7d12ca2..a14d163 100644 --- a/route/periodical.go +++ b/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() diff --git a/route/route.go b/route/route.go index 19f2b3b..7e388a7 100644 --- a/route/route.go +++ b/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() diff --git a/route/v1/docker.go b/route/v1/docker.go index 4b7d4a4..db252f1 100644 --- a/route/v1/docker.go +++ b/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) { diff --git a/route/v1/samba.go b/route/v1/samba.go index b7e88b2..bf3cdac 100644 --- a/route/v1/samba.go +++ b/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) diff --git a/route/v1/system.go b/route/v1/system.go index 8f274f6..207eb4f 100644 --- a/route/v1/system.go +++ b/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"}) +} diff --git a/service/system.go b/service/system.go index 04de249..f810fd7 100644 --- a/service/system.go +++ b/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{} }