Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkLeong committed Nov 29, 2022
1 parent 60608c5 commit eb31bf5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pkg/utils/common_err/e.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions route/periodical.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 6 additions & 1 deletion route/v1/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions route/v1/samba.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions route/v1/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 changes: 23 additions & 0 deletions service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
net2 "net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -52,6 +53,8 @@ type SystemService interface {
GetCPUTemperature() int
GetCPUPower() map[string]string
GetMacAddress() (string, error)
SystemReboot() error
SystemShutdown() error
}
type systemService struct{}

Expand Down Expand Up @@ -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{}
}

0 comments on commit eb31bf5

Please sign in to comment.