Merge remote-tracking branch 'origin/main' into multi-tenant

This commit is contained in:
TsMask
2024-08-17 18:25:43 +08:00
69 changed files with 3295 additions and 4634 deletions

View File

@@ -52,6 +52,7 @@ func Setup(router *gin.Engine) {
// Count: 10,
// Type: middleware.LIMIT_IP,
// }),
middleware.CryptoApi(true, true),
controller.NewAccount.Login,
)
indexGroup.GET("/getInfo", middleware.PreAuthorize(nil), controller.NewAccount.Info)
@@ -74,6 +75,7 @@ func Setup(router *gin.Engine) {
// Count: 10,
// Type: middleware.LIMIT_IP,
// }),
middleware.CryptoApi(true, true),
controller.NewRegister.Register,
)
}

View File

@@ -28,21 +28,14 @@ type MonitorController struct {
func (s *MonitorController) Load(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
// 数据类型all/load/cpu/memory/io/network
Type string `form:"type" binding:"required,oneof=all load cpu memory io network"`
// 开始时间
StartTime int64 `form:"startTime" binding:"required"`
// 结束时间
EndTime int64 `form:"endTime" binding:"required"`
// 网元类型
NeType string `form:"neType"`
// 网元ID
NeID string `form:"neId"`
// 名称networ和iok时有效
Name string `form:"name"`
Type string `form:"type" binding:"required,oneof=all load cpu memory io network"` // 数据类型all/load/cpu/memory/io/network
StartTime int64 `form:"startTime" binding:"required"` // 开始时间
EndTime int64 `form:"endTime" binding:"required"` // 结束时间
NeType string `form:"neType"` // 网元类型
NeID string `form:"neId"` // 网元ID
Name string `form:"name"` // 名称networ和io时有效
}
err := c.ShouldBindQuery(&querys)
if err != nil {
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}

View File

@@ -24,12 +24,13 @@ type SystemInfoController struct {
//
// GET /
func (s *SystemInfoController) Info(c *gin.Context) {
c.JSON(200, result.OkData(map[string]any{
data := map[string]any{
"cpu": s.systemInfogService.CPUInfo(),
"memory": s.systemInfogService.MemoryInfo(),
"network": s.systemInfogService.NetworkInfo(),
"time": s.systemInfogService.TimeInfo(),
"system": s.systemInfogService.SystemInfo(),
"disk": s.systemInfogService.DiskInfo(),
}))
}
c.JSON(200, result.OkData(data))
}

View File

@@ -9,11 +9,11 @@ import (
"be.ems/src/modules/monitor/model"
"be.ems/src/modules/monitor/repository"
systemService "be.ems/src/modules/system/service"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
// 实例化服务层 MonitorImpl 结构体

View File

@@ -5,11 +5,11 @@ import (
"testing"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
func init() {

View File

@@ -1,6 +1,7 @@
package service
import (
"context"
"fmt"
"os"
"runtime"
@@ -10,11 +11,11 @@ import (
"be.ems/src/framework/config"
"be.ems/src/framework/utils/parse"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/net"
)
// 实例化服务层 SystemInfoImpl 结构体
@@ -150,9 +151,12 @@ func (s *SystemInfoImpl) NetworkInfo() map[string]string {
// DiskInfo 磁盘信息
func (s *SystemInfoImpl) DiskInfo() []map[string]string {
disks := make([]map[string]string, 0)
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
partitions, err := disk.Partitions(false)
if err != nil {
partitions, err := disk.PartitionsWithContext(ctx, false)
if err != context.DeadlineExceeded {
return disks
}

View File

@@ -29,7 +29,7 @@ const (
// 配置文件路径
configParamDir = "../../../config/param"
// configParamFile = "*" // 目录下全部更新
configParamFile = "upf_param_config.yaml" // 单文件更新
configParamFile = "ims_param_config.yaml" // 单文件更新
)
func TestEncrypt(t *testing.T) {
@@ -217,7 +217,7 @@ func parseParamConfig(data map[string]any) ([]map[string]string, error) {
itemMap["paramSort"] = fmt.Sprint(iiv)
case "perms", "method":
itemMap["paramPerms"] = iiv.(string)
case "data", "list", "array":
case "list", "array": // 参数类型为数组
itemMap["paramType"] = iik
strByte, _ := json.Marshal(iiv)
itemMap["paramJson"] = string(strByte)

View File

@@ -80,15 +80,18 @@ func Setup(router *gin.Engine) {
controller.NewNeInfo.List,
)
neInfoGroup.GET("/:infoId",
middleware.CryptoApi(false, true),
middleware.PreAuthorize(nil),
controller.NewNeInfo.Info,
)
neInfoGroup.POST("",
middleware.CryptoApi(true, true),
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_INSERT)),
controller.NewNeInfo.Add,
)
neInfoGroup.PUT("",
middleware.CryptoApi(true, true),
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_UPDATE)),
controller.NewNeInfo.Edit,
@@ -108,6 +111,7 @@ func Setup(router *gin.Engine) {
controller.NewNeHost.List,
)
neHostGroup.GET("/:hostId",
middleware.CryptoApi(false, true),
middleware.PreAuthorize(nil),
controller.NewNeHost.Info,
)

View File

@@ -7,7 +7,6 @@ import (
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/crypto"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/network_element/model"
@@ -170,32 +169,7 @@ func (r *NeHostImpl) SelectByIds(hostIds []string) []model.NeHost {
return []model.NeHost{}
}
// 转换实体
rows := r.convertResultRows(results)
arr := &rows
for i := range *arr {
passwordDe, err := crypto.StringDecryptByAES((*arr)[i].Password)
if err != nil {
logger.Errorf("selectById %s decrypt: %v", (*arr)[i].HostID, err.Error())
(*arr)[i].Password = ""
} else {
(*arr)[i].Password = passwordDe
}
privateKeyDe, err := crypto.StringDecryptByAES((*arr)[i].PrivateKey)
if err != nil {
logger.Errorf("selectById %s decrypt: %v", (*arr)[i].HostID, err.Error())
(*arr)[i].PrivateKey = ""
} else {
(*arr)[i].PrivateKey = privateKeyDe
}
passPhraseDe, err := crypto.StringDecryptByAES((*arr)[i].PassPhrase)
if err != nil {
logger.Errorf("selectById %s decrypt: %v", (*arr)[i].HostID, err.Error())
(*arr)[i].PassPhrase = ""
} else {
(*arr)[i].PassPhrase = passPhraseDe
}
}
return rows
return r.convertResultRows(results)
}
// CheckUniqueNeHost 校验主机是否唯一
@@ -263,28 +237,13 @@ func (r *NeHostImpl) Insert(neHost model.NeHost) string {
params["auth_mode"] = neHost.AuthMode
}
if neHost.Password != "" {
passwordEn, err := crypto.StringEncryptByAES(neHost.Password)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
params["password"] = passwordEn
params["password"] = neHost.Password
}
if neHost.PrivateKey != "" {
privateKeyEn, err := crypto.StringEncryptByAES(neHost.PrivateKey)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
params["private_key"] = privateKeyEn
params["private_key"] = neHost.PrivateKey
}
if neHost.PassPhrase != "" {
passPhraseEn, err := crypto.StringEncryptByAES(neHost.PassPhrase)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
params["pass_phrase"] = passPhraseEn
params["pass_phrase"] = neHost.PassPhrase
}
if neHost.Remark != "" {
params["remark"] = neHost.Remark
@@ -361,28 +320,13 @@ func (r *NeHostImpl) Update(neHost model.NeHost) int64 {
params["auth_mode"] = neHost.AuthMode
}
if neHost.Password != "" {
passwordEn, err := crypto.StringEncryptByAES(neHost.Password)
if err != nil {
logger.Errorf("update encrypt: %v", err.Error())
return 0
}
params["password"] = passwordEn
params["password"] = neHost.Password
}
if neHost.PrivateKey != "" {
privateKeyEn, err := crypto.StringEncryptByAES(neHost.PrivateKey)
if err != nil {
logger.Errorf("update encrypt: %v", err.Error())
return 0
}
params["private_key"] = privateKeyEn
params["private_key"] = neHost.PrivateKey
}
if neHost.PassPhrase != "" {
passPhraseEn, err := crypto.StringEncryptByAES(neHost.PassPhrase)
if err != nil {
logger.Errorf("update encrypt: %v", err.Error())
return 0
}
params["pass_phrase"] = passPhraseEn
params["pass_phrase"] = neHost.PassPhrase
}
params["remark"] = neHost.Remark
if neHost.UpdateBy != "" {

View File

@@ -3,6 +3,9 @@ package service
import (
"fmt"
"be.ems/src/framework/config"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/crypto"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
)
@@ -30,12 +33,39 @@ func (r *NeHostImpl) SelectList(neHost model.NeHost) []model.NeHost {
// SelectByIds 通过ID查询
func (r *NeHostImpl) SelectById(hostId string) model.NeHost {
neHost := model.NeHost{}
if hostId == "" {
return model.NeHost{}
return neHost
}
neHosts := r.neHostRepository.SelectByIds([]string{hostId})
if len(neHosts) > 0 {
return neHosts[0]
neHost := neHosts[0]
hostKey := config.Get("aes.hostKey").(string)
if neHost.Password != "" {
passwordDe, err := crypto.AESDecryptBase64(neHost.Password, hostKey)
if err != nil {
logger.Errorf("select encrypt: %v", err.Error())
return neHost
}
neHost.Password = passwordDe
}
if neHost.PrivateKey != "" {
privateKeyDe, err := crypto.AESDecryptBase64(neHost.PrivateKey, hostKey)
if err != nil {
logger.Errorf("select encrypt: %v", err.Error())
return neHost
}
neHost.PrivateKey = privateKeyDe
}
if neHost.PassPhrase != "" {
passPhraseDe, err := crypto.AESDecryptBase64(neHost.PassPhrase, hostKey)
if err != nil {
logger.Errorf("select encrypt: %v", err.Error())
return neHost
}
neHost.PassPhrase = passPhraseDe
}
return neHost
}
return model.NeHost{}
}
@@ -54,11 +84,61 @@ func (r *NeHostImpl) Inserts(neHosts []model.NeHost) int64 {
// Insert 新增信息
func (r *NeHostImpl) Insert(neHost model.NeHost) string {
hostKey := config.Get("aes.hostKey").(string)
if neHost.Password != "" {
passwordEn, err := crypto.AESEncryptBase64(neHost.Password, hostKey)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
neHost.Password = passwordEn
}
if neHost.PrivateKey != "" {
privateKeyEn, err := crypto.AESEncryptBase64(neHost.PrivateKey, hostKey)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
neHost.PrivateKey = privateKeyEn
}
if neHost.PassPhrase != "" {
passPhraseEn, err := crypto.AESEncryptBase64(neHost.PassPhrase, hostKey)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return ""
}
neHost.PassPhrase = passPhraseEn
}
return r.neHostRepository.Insert(neHost)
}
// Update 修改信息
func (r *NeHostImpl) Update(neHost model.NeHost) int64 {
hostKey := config.Get("aes.hostKey").(string)
if neHost.Password != "" {
passwordEn, err := crypto.AESEncryptBase64(neHost.Password, hostKey)
if err != nil {
logger.Errorf("update password encrypt: %v", err.Error())
return 0
}
neHost.Password = passwordEn
}
if neHost.PrivateKey != "" {
privateKeyEn, err := crypto.AESEncryptBase64(neHost.PrivateKey, hostKey)
if err != nil {
logger.Errorf("update private key encrypt: %v", err.Error())
return 0
}
neHost.PrivateKey = privateKeyEn
}
if neHost.PassPhrase != "" {
passPhraseEn, err := crypto.AESEncryptBase64(neHost.PassPhrase, hostKey)
if err != nil {
logger.Errorf("update pass phrase encrypt: %v", err.Error())
return 0
}
neHost.PassPhrase = passPhraseEn
}
return r.neHostRepository.Update(neHost)
}

View File

@@ -208,7 +208,17 @@ func (r *NeInfoImpl) bandNeHosts(arr *[]model.NeInfo) {
for i := range *arr {
v := (*arr)[i]
if v.HostIDs != "" {
(*arr)[i].Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(v.HostIDs, ","))
hostIds := strings.Split(v.HostIDs, ",")
if len(hostIds) <= 1 {
continue
}
for _, hostId := range hostIds {
neHost := NewNeHostImpl.SelectById(hostId)
if neHost.HostID == "" || neHost.HostID != hostId {
continue
}
(*arr)[i].Hosts = append((*arr)[i].Hosts, neHost)
}
}
}
}
@@ -222,12 +232,11 @@ func (r *NeInfoImpl) SelectById(infoId string, bandHost bool) model.NeInfo {
}
neInfos := r.neInfoRepository.SelectByIds([]string{infoId})
if len(neInfos) > 0 {
neInfo := neInfos[0]
// 带主机信息
if neInfo.HostIDs != "" && bandHost {
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
if neInfos[0].HostIDs != "" && bandHost {
r.bandNeHosts(&neInfos)
}
return neInfo
return neInfos[0]
}
return model.NeInfo{}
}
@@ -335,12 +344,17 @@ func (r *NeInfoImpl) NeRunSSHClient(neType, neId string) (*ssh.ConnSSH, error) {
logger.Errorf("NeRunSSHClient NeType:%s NeID:%s hostId not found", neType, neId)
return nil, fmt.Errorf("neinfo hostId not found")
}
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
if len(neInfo.Hosts) <= 0 {
logger.Errorf("NeRunSSHClient Hosts %s not found", neInfo.HostIDs)
hostIds := strings.Split(neInfo.HostIDs, ",")
if len(hostIds) <= 1 {
logger.Errorf("NeRunTelnetClient hosts id %s not found", neInfo.HostIDs)
return nil, fmt.Errorf("neinfo host id not found")
}
hostId := hostIds[0] // 网元主机ssh 022
neHost := NewNeHostImpl.SelectById(hostId)
if neHost.HostID == "" || neHost.HostID != hostId {
logger.Errorf("NeRunTelnetClient Hosts %s not found", neInfo.HostIDs)
return nil, fmt.Errorf("neinfo host not found")
}
neHost := neInfo.Hosts[0] // 网元主机ssh 022
if neHost.HostType != "ssh" {
logger.Errorf("NeRunSSHClient Hosts first HostType %s not ssh", neHost.HostType)
return nil, fmt.Errorf("neinfo host type not ssh")
@@ -392,12 +406,17 @@ func (r *NeInfoImpl) NeRunTelnetClient(neType, neId string, num int) (*telnet.Co
logger.Errorf("NeRunTelnetClient NeType:%s NeID:%s hostId not found", neType, neId)
return nil, fmt.Errorf("neinfo hostId not found")
}
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
if len(neInfo.Hosts) <= 0 {
hostIds := strings.Split(neInfo.HostIDs, ",")
if len(hostIds) <= 1 {
logger.Errorf("NeRunTelnetClient hosts id %s not found", neInfo.HostIDs)
return nil, fmt.Errorf("neinfo host id not found")
}
hostId := hostIds[num] // 网元主机telnet 14100 25200
neHost := NewNeHostImpl.SelectById(hostId)
if neHost.HostID == "" || neHost.HostID != hostId {
logger.Errorf("NeRunTelnetClient Hosts %s not found", neInfo.HostIDs)
return nil, fmt.Errorf("neinfo host not found")
}
neHost := neInfo.Hosts[num]
// 创建链接Telnet客户端
var connTelnet telnet.ConnTelnet

View File

@@ -1,6 +1,6 @@
package model
import "github.com/shirou/gopsutil/v3/net"
import "github.com/shirou/gopsutil/v4/net"
// NetConnectData 网络连接进程数据
type NetConnectData struct {

View File

@@ -8,8 +8,8 @@ import (
"be.ems/src/framework/logger"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/ws/model"
"github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v3/process"
"github.com/shirou/gopsutil/v4/net"
"github.com/shirou/gopsutil/v4/process"
)
// GetNetConnections 获取网络连接进程

View File

@@ -12,7 +12,7 @@ import (
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/ws/model"
"github.com/shirou/gopsutil/v3/process"
"github.com/shirou/gopsutil/v4/process"
)
// GetProcessData 获取进程数据