Merge branch 'lichang' into lite

This commit is contained in:
TsMask
2025-04-29 16:15:16 +08:00
670 changed files with 17749 additions and 19324 deletions

View File

@@ -0,0 +1,31 @@
package crypto
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
)
// SHA256ToBase64 编码字符串
func SHA256ToBase64(str string) string {
hash := sha256.Sum256([]byte(str))
return base64.URLEncoding.EncodeToString(hash[:])
}
// SHA256Hmac HMAC-SHA256算法
func SHA256Hmac(key string, data string) string {
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(data))
return hex.EncodeToString(mac.Sum(nil))
}
// MD5 md5加密
func MD5(str string) (md5str string) {
data := []byte(str)
has := md5.Sum(data)
md5str = fmt.Sprintf("%x", has)
return md5str
}

View File

@@ -13,11 +13,11 @@ import (
"strings"
"time"
libGlobal "be.ems/lib/global"
"be.ems/src/framework/config"
)
// userAgent 自定义 User-Agent
var userAgent = fmt.Sprintf("OMC/%s", libGlobal.Version)
var userAgent = fmt.Sprintf("OMC/%s", config.Version)
// Get 发送 GET 请求
// timeout 超时时间(毫秒)

View File

@@ -0,0 +1,78 @@
package file
import (
"os"
"path/filepath"
)
// FileListRow 文件列表行数据
type FileListRow struct {
FileType string `json:"fileType"` // 文件类型 dir, file, symlink
FileMode string `json:"fileMode"` // 文件的权限
LinkCount int64 `json:"linkCount"` // 硬链接数目
Owner string `json:"owner"` // 所属用户
Group string `json:"group"` // 所属组
Size int64 `json:"size"` // 文件的大小
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
FileName string `json:"fileName"` // 文件的名称
}
// 文件列表
// search 文件名后模糊*
//
// return 行记录,异常
func FileList(path, search string) ([]FileListRow, error) {
var rows []FileListRow
// 构建搜索模式
pattern := "*"
if search != "" {
pattern = search + pattern
}
// 读取目录内容
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
// 遍历目录项
for _, entry := range entries {
// 匹配文件名
matched, err := filepath.Match(pattern, entry.Name())
if err != nil || !matched {
continue
}
// 获取文件详细信息
info, err := entry.Info()
if err != nil {
continue
}
// 确定文件类型
fileType := "file"
if info.IsDir() {
fileType = "dir"
} else if info.Mode()&os.ModeSymlink != 0 {
fileType = "symlink"
}
// 获取系统特定的文件信息
linkCount, owner, group := getFileInfo(info)
// 组装文件信息
rows = append(rows, FileListRow{
FileMode: info.Mode().String(),
FileType: fileType,
LinkCount: linkCount,
Owner: owner,
Group: group,
Size: info.Size(),
ModifiedTime: info.ModTime().UnixMilli(),
FileName: entry.Name(),
})
}
return rows, nil
}

View File

@@ -0,0 +1,36 @@
//go:build !windows
// +build !windows
package file
import (
"fmt"
"os"
"os/user"
"syscall"
)
// getFileInfo 获取系统特定的文件信息s
func getFileInfo(info os.FileInfo) (linkCount int64, owner, group string) {
// Unix-like 系统 (Linux, macOS)
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
// 获取用户名
ownerName := "root"
if stat.Uid != 0 {
if u, err := user.LookupId(fmt.Sprint(stat.Uid)); err == nil {
ownerName = u.Username
}
}
// 获取组名
groupName := "root"
if stat.Gid != 0 {
if g, err := user.LookupGroupId(fmt.Sprint(stat.Gid)); err == nil {
groupName = g.Name
}
}
return int64(stat.Nlink), ownerName, groupName
}
return 1, "", ""
}

View File

@@ -0,0 +1,13 @@
//go:build windows
// +build windows
package file
import (
"os"
)
// getFileInfo 获取系统特定的文件信息
func getFileInfo(_ os.FileInfo) (linkCount int64, owner, group string) {
return 1, "Administrator", "Administrators"
}

View File

@@ -163,11 +163,11 @@ func Reset() error {
// return fmt.Errorf("not support window")
} else {
// 重置数据库
if _, err := cmd.Execf("sudo cp -rf /usr/local/omc/etc/db/omc_db.sqlite /usr/local/omc/database/omc_db.sqlite"); err != nil {
if _, err := cmd.Execf("/usr/local/etc/omc/script/setup.sh -i"); err != nil {
return err
}
// 重启服务
if _, err := cmd.Execf("nohup sh -c \"sleep 1s && %s\" > /dev/null 2>&1 &", "sudo systemctl restart restagent"); err != nil {
if _, err := cmd.Execf("nohup sh -c \"sleep 1s && %s\" > /dev/null 2>&1 &", "sudo systemctl restart omc"); err != nil {
return err
}
}