Files
HY2XS_flamy/apps/controller/log.go
T

110 lines
2.8 KiB
Go

package controller
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"hy2xs-admin/model/constant"
"hy2xs-admin/model/dto"
"hy2xs-admin/model/vo"
"hy2xs-admin/service"
"hy2xs-admin/util"
"time"
)
func LogSystem(c *gin.Context) {
logSystemDto, err := validateField(c, dto.LogDto{})
if err != nil {
return
}
exists := util.Exists(constant.SystemLogPath)
logSystemVos := make([]vo.LogSystemVo, 0)
if !exists {
vo.Success(logSystemVos, c)
return
}
numLine := 0
if logSystemDto.NumLine != nil && *logSystemDto.NumLine > 0 {
numLine = *logSystemDto.NumLine
}
logLines, total, err := util.ReadLinesFromBottom(constant.SystemLogPath, numLine)
if err != nil {
vo.Fail("Unable to read log file", c)
return
}
for _, line := range logLines {
if line == "" {
continue
}
logSystemVo := vo.LogSystemVo{}
err := json.Unmarshal([]byte(line), &logSystemVo)
if err != nil {
vo.Fail("Unable to unmarshal log data", c)
continue
}
logSystemVos = append(logSystemVos, logSystemVo)
}
vo.Success(vo.LogSystemPage[vo.LogSystemVo]{
LogSystemVos: logSystemVos,
Total: int64(total),
}, c)
}
func LogHysteria2(c *gin.Context) {
logSystemDto, err := validateField(c, dto.LogDto{})
if err != nil {
return
}
logHysteria2Vos := make([]vo.LogHysteria2Vo, 0)
numLine := 0
if logSystemDto.NumLine != nil && *logSystemDto.NumLine > 0 {
numLine = *logSystemDto.NumLine
}
logHysteria2Vos, total, err := service.ReadHysteriaJournalLogs(numLine)
if err != nil {
vo.Fail("Unable to read hysteria journal logs", c)
return
}
vo.Success(vo.LogSystemPage[vo.LogHysteria2Vo]{
LogSystemVos: logHysteria2Vos,
Total: int64(total),
}, c)
}
func ExportLog(c *gin.Context) {
logExportDto, err := validateField(c, dto.LogExportDto{})
if err != nil {
return
}
var fileName string
var filePath string
if *logExportDto.Option == 0 {
fileName = fmt.Sprintf("hy2xs-admin-%s.log", time.Now().Format("20060102150405"))
filePath = constant.SystemLogPath
} else if *logExportDto.Option == 1 {
fileName = fmt.Sprintf("hysteria2-%s.log", time.Now().Format("20060102150405"))
output, exportErr := service.ExportHysteriaJournalLogs(5000)
if exportErr != nil {
vo.Fail("failed to export hysteria journal logs", c)
return
}
c.Header("Content-Type", "text/plain; charset=utf-8")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
c.Data(200, "text/plain; charset=utf-8", []byte(output))
return
}
if !util.Exists(filePath) {
vo.Fail("log file not exist", c)
return
}
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
c.File(filePath)
}