2021-09-26 10:35:02 +08:00
package v1
import (
2022-09-06 14:28:49 +08:00
"bytes"
2021-09-26 10:35:02 +08:00
"fmt"
2022-09-06 14:28:49 +08:00
"io"
"io/ioutil"
2021-10-15 11:43:41 +08:00
"net/http"
2021-12-06 17:08:36 +08:00
"os"
2021-10-15 11:43:41 +08:00
"strconv"
2021-12-29 16:42:20 +08:00
"strings"
2021-10-15 11:43:41 +08:00
"time"
2021-12-29 16:42:20 +08:00
"unsafe"
2021-10-15 11:43:41 +08:00
2022-11-03 18:07:49 -04:00
http2 "github.com/IceWhaleTech/CasaOS-Common/utils/http"
2021-09-27 14:17:36 +08:00
"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
2022-06-29 11:09:58 +08:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/common_err"
2021-12-06 17:08:36 +08:00
port2 "github.com/IceWhaleTech/CasaOS/pkg/utils/port"
2021-09-27 14:17:36 +08:00
"github.com/IceWhaleTech/CasaOS/pkg/utils/version"
"github.com/IceWhaleTech/CasaOS/service"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
"github.com/IceWhaleTech/CasaOS/types"
2021-09-26 10:35:02 +08:00
"github.com/gin-gonic/gin"
)
2022-05-05 13:46:55 +08:00
// @Summary check version
2021-09-26 10:35:02 +08:00
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-06-13 20:43:19 +08:00
// @Router /sys/version/check [get]
func GetSystemCheckVersion ( c * gin . Context ) {
2022-06-29 11:09:58 +08:00
need , version := version . IsNeedUpdate ( service . MyService . Casa ( ) . GetCasaosVersion ( ) )
2021-09-26 10:35:02 +08:00
if need {
installLog := model2 . AppNotify { }
installLog . State = 0
installLog . Message = "New version " + version . Version + " is ready, ready to upgrade"
installLog . Type = types . NOTIFY_TYPE_NEED_CONFIRM
installLog . CreatedAt = strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 )
installLog . UpdatedAt = strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 )
2021-10-29 18:37:27 +08:00
installLog . Name = "CasaOS System"
2021-09-26 10:35:02 +08:00
service . MyService . Notify ( ) . AddLog ( installLog )
}
2022-06-29 11:09:58 +08:00
data := make ( map [ string ] interface { } , 3 )
2022-07-22 11:02:11 +08:00
data [ "need_update" ] = need
2021-09-26 10:35:02 +08:00
data [ "version" ] = version
data [ "current_version" ] = types . CURRENTVERSION
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : data } )
2021-09-26 10:35:02 +08:00
}
// @Summary 系统信息
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/update [post]
func SystemUpdate ( c * gin . Context ) {
2022-06-29 11:09:58 +08:00
need , version := version . IsNeedUpdate ( service . MyService . Casa ( ) . GetCasaosVersion ( ) )
2021-09-26 10:35:02 +08:00
if need {
service . MyService . System ( ) . UpdateSystemVersion ( version . Version )
}
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) } )
2021-09-26 10:35:02 +08:00
}
2021-10-27 17:27:46 +08:00
// @Summary get logs
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/error/logs [get]
func GetCasaOSErrorLogs ( c * gin . Context ) {
line , _ := strconv . Atoi ( c . DefaultQuery ( "line" , "100" ) )
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : service . MyService . System ( ) . GetCasaOSLogs ( line ) } )
2021-09-26 10:35:02 +08:00
}
2022-10-20 02:25:26 -04:00
// 系统配置
2021-09-26 10:35:02 +08:00
func GetSystemConfigDebug ( c * gin . Context ) {
array := service . MyService . System ( ) . GetSystemConfigDebug ( )
2022-06-29 11:09:58 +08:00
disk := service . MyService . System ( ) . GetDiskInfo ( )
sys := service . MyService . System ( ) . GetSysInfo ( )
2022-07-22 11:02:11 +08:00
version := service . MyService . Casa ( ) . GetCasaosVersion ( )
2022-01-20 18:38:59 +08:00
var bugContent string = fmt . Sprintf ( `
- OS : % s
- CasaOS Version : % s
- Disk Total : % v
- Disk Used : % v
- System Info : % s
2022-07-22 11:02:11 +08:00
- Remote Version : % s
2022-01-20 18:38:59 +08:00
- Browser : $ Browser $
- Version : $ Version $
2022-07-22 11:02:11 +08:00
` , sys . OS , types . CURRENTVERSION , disk . Total >> 20 , disk . Used >> 20 , array , version . Version )
2022-01-20 18:38:59 +08:00
// array = append(array, fmt.Sprintf("disk,total:%v,used:%v,UsedPercent:%v", disk.Total>>20, disk.Used>>20, disk.UsedPercent))
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : bugContent } )
2021-10-15 11:43:41 +08:00
}
2021-10-22 16:49:09 +08:00
2021-12-09 19:02:41 +08:00
// @Summary get casaos server port
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/port [get]
func GetCasaOSPort ( c * gin . Context ) {
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS ,
2021-12-09 19:02:41 +08:00
model . Result {
2022-06-29 11:09:58 +08:00
Success : common_err . SUCCESS ,
Message : common_err . GetMsg ( common_err . SUCCESS ) ,
2021-12-09 19:02:41 +08:00
Data : config . ServerInfo . HttpPort ,
} )
}
2021-12-06 17:08:36 +08:00
// @Summary edit casaos server port
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
2022-06-13 20:43:19 +08:00
// @Param port json string true "port"
2021-12-06 17:08:36 +08:00
// @Success 200 {string} string "ok"
2021-12-09 19:02:41 +08:00
// @Router /sys/port [put]
2021-12-06 17:08:36 +08:00
func PutCasaOSPort ( c * gin . Context ) {
2022-06-13 20:43:19 +08:00
json := make ( map [ string ] string )
2022-07-22 11:02:11 +08:00
c . ShouldBind ( & json )
2022-06-13 20:43:19 +08:00
portStr := json [ "port" ]
port , err := strconv . Atoi ( portStr )
2021-12-06 17:08:36 +08:00
if err != nil {
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SERVICE_ERROR ,
2021-12-06 17:08:36 +08:00
model . Result {
2022-07-22 11:02:11 +08:00
Success : common_err . SERVICE_ERROR ,
2021-12-06 17:08:36 +08:00
Message : err . Error ( ) ,
} )
return
}
isAvailable := port2 . IsPortAvailable ( port , "tcp" )
if ! isAvailable {
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SERVICE_ERROR ,
2021-12-06 17:08:36 +08:00
model . Result {
2022-06-29 11:09:58 +08:00
Success : common_err . PORT_IS_OCCUPIED ,
Message : common_err . GetMsg ( common_err . PORT_IS_OCCUPIED ) ,
2021-12-06 17:08:36 +08:00
} )
return
}
service . MyService . System ( ) . UpSystemPort ( strconv . Itoa ( port ) )
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS ,
2021-12-06 17:08:36 +08:00
model . Result {
2022-06-29 11:09:58 +08:00
Success : common_err . SUCCESS ,
Message : common_err . GetMsg ( common_err . SUCCESS ) ,
2021-12-06 17:08:36 +08:00
} )
}
// @Summary active killing casaos
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-06-13 20:43:19 +08:00
// @Router /sys/restart [post]
2021-12-06 17:08:36 +08:00
func PostKillCasaOS ( c * gin . Context ) {
os . Exit ( 0 )
}
2021-12-29 16:42:20 +08:00
2022-07-22 11:02:11 +08:00
func GetSystemAppsStatus ( c * gin . Context ) {
systemAppList := service . MyService . App ( ) . GetSystemAppList ( )
appList := [ ] model2 . MyAppList { }
for _ , v := range systemAppList {
name := strings . ReplaceAll ( v . Names [ 0 ] , "/" , "" )
if len ( v . Labels [ "name" ] ) > 0 {
name = v . Labels [ "name" ]
}
appList = append ( appList , model2 . MyAppList {
Name : name ,
Icon : v . Labels [ "icon" ] ,
State : v . State ,
CustomId : v . Labels [ "custom_id" ] ,
Id : v . ID ,
Port : v . Labels [ "web" ] ,
Index : v . Labels [ "index" ] ,
2022-10-20 02:25:26 -04:00
// Order: m.Labels["order"],
2022-07-22 11:02:11 +08:00
Image : v . Image ,
Latest : false ,
2022-10-20 02:25:26 -04:00
// Type: m.Labels["origin"],
// Slogan: m.Slogan,
// Rely: m.Rely,
2022-07-22 11:02:11 +08:00
Host : v . Labels [ "host" ] ,
Protocol : v . Labels [ "protocol" ] ,
} )
}
c . JSON ( common_err . SUCCESS ,
model . Result {
Success : common_err . SUCCESS ,
Message : common_err . GetMsg ( common_err . SUCCESS ) ,
Data : appList ,
} )
}
2022-05-13 18:12:26 +08:00
// @Summary get system hardware info
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/hardware/info [get]
func GetSystemHardwareInfo ( c * gin . Context ) {
data := make ( map [ string ] string , 1 )
2022-06-29 11:09:58 +08:00
data [ "drive_model" ] = service . MyService . System ( ) . GetDeviceTree ( )
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS ,
2022-05-13 18:12:26 +08:00
model . Result {
2022-06-29 11:09:58 +08:00
Success : common_err . SUCCESS ,
Message : common_err . GetMsg ( common_err . SUCCESS ) ,
2022-05-13 18:12:26 +08:00
Data : data ,
} )
}
2022-06-13 20:43:19 +08:00
// @Summary system utilization
2021-12-29 16:42:20 +08:00
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
2022-06-13 20:43:19 +08:00
// @Router /sys/utilization [get]
func GetSystemUtilization ( c * gin . Context ) {
2022-10-20 02:25:26 -04:00
data := make ( map [ string ] interface { } )
2022-06-08 18:19:45 +08:00
cpu := service . MyService . System ( ) . GetCpuPercent ( )
num := service . MyService . System ( ) . GetCpuCoreNum ( )
2022-11-29 02:25:26 +00:00
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"
}
}
2021-12-29 16:42:20 +08:00
cpuData := make ( map [ string ] interface { } )
cpuData [ "percent" ] = cpu
cpuData [ "num" ] = num
2022-09-06 14:28:49 +08:00
cpuData [ "temperature" ] = service . MyService . System ( ) . GetCPUTemperature ( )
cpuData [ "power" ] = service . MyService . System ( ) . GetCPUPower ( )
2022-11-29 02:25:26 +00:00
cpuData [ "model" ] = cpuModel
2022-09-06 14:28:49 +08:00
2021-12-29 16:42:20 +08:00
data [ "cpu" ] = cpuData
2022-06-08 18:19:45 +08:00
data [ "mem" ] = service . MyService . System ( ) . GetMemInfo ( )
2021-12-29 16:42:20 +08:00
2022-10-20 02:25:26 -04:00
// 拼装网络信息
2022-06-08 18:19:45 +08:00
netList := service . MyService . System ( ) . GetNetInfo ( )
2021-12-29 16:42:20 +08:00
newNet := [ ] model . IOCountersStat { }
2022-06-08 18:19:45 +08:00
nets := service . MyService . System ( ) . GetNet ( true )
2021-12-29 16:42:20 +08:00
for _ , n := range netList {
for _ , netCardName := range nets {
if n . Name == netCardName {
item := * ( * model . IOCountersStat ) ( unsafe . Pointer ( & n ) )
2022-06-29 11:09:58 +08:00
item . State = strings . TrimSpace ( service . MyService . System ( ) . GetNetState ( n . Name ) )
2022-04-06 12:10:51 +08:00
item . Time = time . Now ( ) . Unix ( )
2021-12-29 16:42:20 +08:00
newNet = append ( newNet , item )
break
}
}
}
data [ "net" ] = newNet
2022-10-20 02:25:26 -04:00
for k , v := range service . MyService . Notify ( ) . GetSystemTempMap ( ) {
data [ k ] = v
}
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : data } )
2021-12-29 16:42:20 +08:00
}
2022-06-08 18:19:45 +08:00
// @Summary Get notification port
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/socket/port [get]
func GetSystemSocketPort ( c * gin . Context ) {
2022-07-22 11:02:11 +08:00
c . JSON ( common_err . SUCCESS ,
2022-06-08 18:19:45 +08:00
model . Result {
2022-06-29 11:09:58 +08:00
Success : common_err . SUCCESS ,
Message : common_err . GetMsg ( common_err . SUCCESS ) ,
2022-07-22 11:02:11 +08:00
Data : config . ServerInfo . SocketPort , // @tiger 这里最好封装成 {'port': ...} 的形式,来体现出参的上下文
2022-06-08 18:19:45 +08:00
} )
}
2022-06-13 20:43:19 +08:00
// @Summary get cpu info
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/cpu [get]
func GetSystemCupInfo ( c * gin . Context ) {
cpu := service . MyService . System ( ) . GetCpuPercent ( )
num := service . MyService . System ( ) . GetCpuCoreNum ( )
data := make ( map [ string ] interface { } )
data [ "percent" ] = cpu
data [ "num" ] = num
2022-06-29 11:09:58 +08:00
c . JSON ( http . StatusOK , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : data } )
2022-06-13 20:43:19 +08:00
}
// @Summary get mem info
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/mem [get]
func GetSystemMemInfo ( c * gin . Context ) {
mem := service . MyService . System ( ) . GetMemInfo ( )
2022-06-29 11:09:58 +08:00
c . JSON ( http . StatusOK , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : mem } )
2022-06-13 20:43:19 +08:00
}
// @Summary get disk info
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/disk [get]
func GetSystemDiskInfo ( c * gin . Context ) {
2022-06-29 11:09:58 +08:00
disk := service . MyService . System ( ) . GetDiskInfo ( )
c . JSON ( http . StatusOK , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : disk } )
2022-06-13 20:43:19 +08:00
}
// @Summary get Net info
// @Produce application/json
// @Accept application/json
// @Tags sys
// @Security ApiKeyAuth
// @Success 200 {string} string "ok"
// @Router /sys/net [get]
func GetSystemNetInfo ( c * gin . Context ) {
netList := service . MyService . System ( ) . GetNetInfo ( )
newNet := [ ] model . IOCountersStat { }
for _ , n := range netList {
for _ , netCardName := range service . MyService . System ( ) . GetNet ( true ) {
if n . Name == netCardName {
item := * ( * model . IOCountersStat ) ( unsafe . Pointer ( & n ) )
2022-06-29 11:09:58 +08:00
item . State = strings . TrimSpace ( service . MyService . System ( ) . GetNetState ( n . Name ) )
2022-06-13 20:43:19 +08:00
item . Time = time . Now ( ) . Unix ( )
newNet = append ( newNet , item )
break
}
}
}
2022-06-29 11:09:58 +08:00
c . JSON ( http . StatusOK , model . Result { Success : common_err . SUCCESS , Message : common_err . GetMsg ( common_err . SUCCESS ) , Data : newNet } )
}
2022-09-06 14:28:49 +08:00
func GetSystemProxy ( c * gin . Context ) {
url := c . Query ( "url" )
2022-11-03 18:07:49 -04:00
resp , err := http2 . Get ( url , 30 * time . Second )
2022-09-06 14:28:49 +08:00
if err != nil {
return
}
defer resp . Body . Close ( )
for k , v := range c . Request . Header {
c . Header ( k , v [ 0 ] )
}
rda , _ := ioutil . ReadAll ( resp . Body )
// json.NewEncoder(c.Writer).Encode(json.RawMessage(string(rda)))
// 响应状态码
c . Writer . WriteHeader ( resp . StatusCode )
// 复制转发的响应Body到响应Body
io . Copy ( c . Writer , ioutil . NopCloser ( bytes . NewBuffer ( rda ) ) )
}
2022-11-29 02:25:26 +00:00
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" } )
}