Merge branch 'main' into multi-tenant

This commit is contained in:
2024-10-31 17:50:10 +08:00
229 changed files with 11395 additions and 11870 deletions

View File

@@ -26,7 +26,7 @@ func ClearLocaleData() {
// LoadLocaleData 加载国际化数据
func LoadLocaleData(language string) []localeItem {
dictType := fmt.Sprintf("i18n_%s", language)
dictTypeList := systemService.NewSysDictTypeImpl.DictDataCache(dictType)
dictTypeList := systemService.NewSysDictType.DictDataCache(dictType)
localeData := []localeItem{}
for _, v := range dictTypeList {
localeData = append(localeData, localeItem{
@@ -58,7 +58,7 @@ func UpdateKeyValue(language, key, value string) bool {
}
// 更新字典数据
sysDictDataService := systemService.NewSysDictDataImpl
sysDictDataService := systemService.NewSysDictData
item := sysDictDataService.SelectDictDataByCode(code)
if item.DictCode == code && item.DictLabel == key {
item.DictValue = value

View File

@@ -0,0 +1,61 @@
package redis
import (
"context"
"fmt"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
// ConnRedis 连接redis对象
type ConnRedis struct {
Addr string `json:"addr"` // 地址
Port int64 `json:"port"` // 端口
User string `json:"user"` // 用户名
Password string `json:"password"` // 认证密码
Database int `json:"database"` // 数据库名称
DialTimeOut time.Duration `json:"dialTimeOut"` // 连接超时断开
Client *redis.Client `json:"client"`
}
// NewClient 创建Redis客户端
func (c *ConnRedis) NewClient() (*ConnRedis, error) {
// IPV6地址协议
if strings.Contains(c.Addr, ":") {
c.Addr = fmt.Sprintf("[%s]", c.Addr)
}
addr := fmt.Sprintf("%s:%d", c.Addr, c.Port)
// 默认等待5s
if c.DialTimeOut == 0 {
c.DialTimeOut = 5 * time.Second
}
// 连接
rdb := redis.NewClient(&redis.Options{
Addr: addr,
// Username: c.User,
Password: c.Password,
DB: c.Database,
DialTimeout: c.DialTimeOut,
})
// 测试数据库连接
if _, err := rdb.Ping(context.Background()).Result(); err != nil {
return nil, err
}
c.Client = rdb
return c, nil
}
// Close 关闭当前Redis客户端
func (c *ConnRedis) Close() {
if c.Client != nil {
c.Client.Close()
}
}

View File

@@ -30,6 +30,15 @@ if tonumber(current) == 1 then
end
return tonumber(current);`)
// 连接Redis实例
func ConnectPush(source string, rdb *redis.Client) {
if rdb == nil {
delete(rdbMap, source)
return
}
rdbMap[source] = rdb
}
// 连接Redis实例
func Connect() {
ctx := context.Background()

View File

@@ -52,16 +52,18 @@ func (s *SocketTCP) Close() {
}
// Resolve 处理消息
func (s *SocketTCP) Resolve(callback func(conn *net.Conn)) error {
func (s *SocketTCP) Resolve(callback func(conn *net.Conn, err error)) {
if s.Listener == nil {
return fmt.Errorf("tcp service not created")
callback(nil, fmt.Errorf("tcp service not created"))
return
}
listener := *s.Listener
for {
select {
case <-s.StopChan:
return fmt.Errorf("udp service stop")
callback(nil, fmt.Errorf("udp service stop"))
return
default:
conn, err := listener.Accept()
if err != nil {
@@ -69,14 +71,7 @@ func (s *SocketTCP) Resolve(callback func(conn *net.Conn)) error {
continue
}
defer conn.Close()
// 处理连接
callback(&conn)
// 发送响应
if _, err = conn.Write([]byte("tcp>")); err != nil {
fmt.Println("Error sending response:", err)
}
callback(&conn, nil)
}
}
}

View File

@@ -50,22 +50,18 @@ func (s *SocketUDP) Close() {
}
// Resolve 处理消息
func (s *SocketUDP) Resolve(callback func(*net.UDPConn)) error {
func (s *SocketUDP) Resolve(callback func(*net.UDPConn, error)) {
if s.Conn == nil {
return fmt.Errorf("udp service not created")
callback(nil, fmt.Errorf("udp service not created"))
return
}
for {
select {
case <-s.StopChan:
return fmt.Errorf("udp service stop")
callback(nil, fmt.Errorf("udp service not created"))
default:
callback(s.Conn)
// 发送响应
if _, err := s.Conn.WriteTo([]byte("udp>"), s.Conn.RemoteAddr()); err != nil {
fmt.Println("Error sending response:", err)
}
callback(s.Conn, nil)
}
}
}

View File

@@ -14,7 +14,6 @@ import (
"golang.org/x/text/language"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// QueryMap 查询参数转换Map
@@ -30,7 +29,7 @@ func QueryMap(c *gin.Context) map[string]any {
// BodyJSONMap JSON参数转换Map
func BodyJSONMap(c *gin.Context) map[string]any {
params := make(map[string]any)
c.ShouldBindBodyWith(&params, binding.JSON)
c.ShouldBindBodyWithJSON(&params)
return params
}
@@ -39,7 +38,7 @@ func RequestParamsMap(c *gin.Context) map[string]any {
params := make(map[string]any)
// json
if strings.HasPrefix(c.ContentType(), "application/json") {
c.ShouldBindBodyWith(&params, binding.JSON)
c.ShouldBindBodyWithJSON(&params)
}
// 表单

View File

@@ -0,0 +1,76 @@
package file
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
)
// CompressTarGZByDir 将目录下文件添加到 tar.gz 压缩文件
func CompressTarGZByDir(zipFilePath, dirPath string) error {
// 创建本地输出目录
if err := os.MkdirAll(filepath.Dir(zipFilePath), 0775); err != nil {
return err
}
// 创建输出文件
tarFile, err := os.Create(zipFilePath)
if err != nil {
return err
}
defer tarFile.Close()
gw := gzip.NewWriter(tarFile)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
// 遍历目录下的所有文件和子目录
err = filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 忽略目录
if info.IsDir() {
return nil
}
// 创建文件条目
header, err := tar.FileInfoHeader(info, "")
if err != nil {
return err
}
relPath, err := filepath.Rel(dirPath, path)
if err != nil {
return err
}
header.Name = relPath
if err := tw.WriteHeader(header); err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
// 打开文件
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// 写入文件内容
_, err = io.Copy(tw, file)
if err != nil {
return err
}
return nil
})
return err
}

View File

@@ -24,9 +24,8 @@ type FileListRow struct {
// 文件列表
// search 文件名后模糊*
//
// return 目录大小,行记录,异常
func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, error) {
totalSize := ""
// return 行记录,异常
func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) {
var rows []FileListRow
rowStr := ""
@@ -35,40 +34,37 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
if search != "" {
searchStr = search + searchStr
}
cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr)
// cd /var/log && find. -maxdepth 1 -name'mme*' -exec ls -lthd --time-style=+%s {} +
cmdStr := fmt.Sprintf("cd %s && find . -maxdepth 1 -name '%s' -exec ls -lthd --time-style=+%%s {} +", path, searchStr)
// cd /var/log && ls -lthd --time-style=+%s mme*
// cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr)
// 是否远程客户端读取
if sshClient == nil {
resultStr, err := cmd.Execf(cmdStr)
if err != nil {
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err
return rows, err
}
rowStr = resultStr
} else {
resultStr, err := sshClient.RunCMD(cmdStr)
if err != nil {
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return totalSize, rows, err
return rows, err
}
rowStr = resultStr
}
// 遍历组装
rowStrList := strings.Split(rowStr, "\n")
for i, rowStr := range rowStrList {
for _, rowStr := range rowStrList {
if rowStr == "" {
continue
}
// 使用空格对字符串进行切割
fields := strings.Fields(rowStr)
// 无查询过滤会有total总计
if i == 0 && searchStr == "" {
totalSize = fields[1]
continue
}
// 拆分不足7位跳过
if len(fields) != 7 {
continue
@@ -83,6 +79,14 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
fileType = "symlink"
}
// 文件名
fileName := fields[6]
if fileName == "." {
continue
} else if strings.HasPrefix(fileName, "./") {
fileName = strings.TrimPrefix(fileName, "./")
}
// 提取各个字段的值
rows = append(rows, FileListRow{
FileMode: fileMode,
@@ -92,8 +96,8 @@ func FileList(sshClient *ConnSSH, path, search string) (string, []FileListRow, e
Group: fields[3],
Size: fields[4],
ModifiedTime: parse.Number(fields[5]),
FileName: fields[6],
FileName: fileName,
})
}
return totalSize, rows, nil
return rows, nil
}

View File

@@ -60,7 +60,7 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
return nil
}
// CopyDirRemoteToLocal 复制目录-本地到远程
// CopyDirLocalToRemote 复制目录-本地到远程
func (s *SSHClientSFTP) CopyDirLocalToRemote(localDir, remoteDir string) error {
// 遍历本地目录中的文件和子目录并复制到远程
err := filepath.Walk(localDir, func(localPath string, info os.FileInfo, err error) error {
@@ -94,7 +94,7 @@ func (s *SSHClientSFTP) CopyDirLocalToRemote(localDir, remoteDir string) error {
return nil
}
// CopyDirRemoteToLocal 复制文件-远程到本地
// CopyFileRemoteToLocal 复制文件-远程到本地
func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) error {
if err := os.MkdirAll(filepath.Dir(localPath), 0775); err != nil {
return err
@@ -124,7 +124,7 @@ func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) erro
return nil
}
// CopyDirRemoteToLocal 复制文件-本地到远程
// CopyFileLocalToRemote 复制文件-本地到远程
func (s *SSHClientSFTP) CopyFileLocalToRemote(localPath, remotePath string) error {
// 打开本地文件
localFile, err := os.Open(localPath)

View File

@@ -212,11 +212,14 @@ func (c *ConnSSH) SendToAuthorizedKeys() error {
if err != nil {
return err
}
// "sudo mkdir -p ~/.ssh && sudo chown omcuser:omcuser ~/.ssh && sudo chmod 700 ~/.ssh"
// "sudo touch ~/.ssh/authorized_keys && sudo chown omcuser:omcuser ~/.ssh/authorized_keys && sudo chmod 600 ~/.ssh/authorized_keys"
// "echo 'ssh-rsa AAAAB3= pc-host\n' | sudo tee -a ~/.ssh/authorized_keys"
authorizedKeysEntry := fmt.Sprintln(strings.TrimSpace(publicKey))
cmdStrArr := []string{
fmt.Sprintf("sudo mkdir -p /home/%s/.ssh && sudo chown %s:%s /home/%s/.ssh && sudo chmod 700 /home/%s/.ssh", c.User, c.User, c.User, c.User, c.User),
fmt.Sprintf("sudo touch /home/%s/.ssh/authorized_keys && sudo chown %s:%s /home/%s/.ssh/authorized_keys && sudo chmod 600 /home/%s/.ssh/authorized_keys", c.User, c.User, c.User, c.User, c.User),
fmt.Sprintf("echo '%s' | sudo tee -a /home/%s/.ssh/authorized_keys", authorizedKeysEntry, c.User),
fmt.Sprintf("sudo mkdir -p ~/.ssh && sudo chown %s:%s ~/.ssh && sudo chmod 700 ~/.ssh", c.User, c.User),
fmt.Sprintf("sudo touch ~/.ssh/authorized_keys && sudo chown %s:%s ~/.ssh/authorized_keys && sudo chmod 600 ~/.ssh/authorized_keys", c.User, c.User),
fmt.Sprintf("echo '%s' | sudo tee -a ~/.ssh/authorized_keys", authorizedKeysEntry),
}
_, err = c.RunCMD(strings.Join(cmdStrArr, " && "))
if err != nil {