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

This commit is contained in:
TsMask
2025-02-11 18:40:36 +08:00
37 changed files with 626 additions and 215 deletions

View File

@@ -2,14 +2,10 @@ package file_export
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"time"
"github.com/jlaffaye/ftp"
"be.ems/lib/file"
"be.ems/lib/log"
@@ -160,7 +156,7 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
@@ -191,7 +187,7 @@ func (m *SysJob) SetFTPConfig(c *gin.Context) {
c.JSON(200, result.Ok(nil))
}
// 设置FTP配置
// 获取FTP配置
// GET /table/ftp
func (m *SysJob) GetFTPConfig(c *gin.Context) {
// 获取配置
@@ -209,7 +205,7 @@ func (m *SysJob) GetFTPConfig(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
err = json.Unmarshal([]byte(bodyDe), &body)
@@ -251,7 +247,7 @@ func (m *SysJob) PutFTP(c *gin.Context) {
Username string `json:"username" binding:"required"`
ToIp string `json:"toIp" binding:"required"`
ToPort int64 `json:"toPort" binding:"required"`
Protocol string `json:"protocol" binding:"required,oneof=ssh ftp"`
Enable bool `json:"enable"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
@@ -269,70 +265,37 @@ func (m *SysJob) PutFTP(c *gin.Context) {
return
}
}
if cfgData.Protocol == "ssh" {
connSSH := ssh.ConnSSH{
User: cfgData.Username,
Password: cfgData.Password,
Addr: cfgData.ToIp,
Port: cfgData.ToPort,
AuthMode: "0",
}
sshClient, err := connSSH.NewClient()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
c.JSON(200, result.ErrMsg("error uploading file"))
return
}
c.JSON(200, result.Ok(nil))
if !cfgData.Enable {
c.JSON(200, result.ErrMsg("Setting Remote Backup is disabled"))
return
}
if cfgData.Protocol == "ftp" {
// 连接到 FTP 服务器
addr := fmt.Sprintf("%s:%d", cfgData.ToIp, cfgData.ToPort)
ftpComm, err := ftp.Dial(addr, ftp.DialWithTimeout(15*time.Second))
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 登录到 FTP 服务器
err = ftpComm.Login(cfgData.Username, cfgData.Password)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer ftpComm.Quit()
// 打开本地文件
file, err := os.Open(localFilePath)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer file.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 上传文件到 FTP 服务器
err = ftpComm.Stor(remotePath, file)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
connSSH := ssh.ConnSSH{
User: cfgData.Username,
Password: cfgData.Password,
Addr: cfgData.ToIp,
Port: cfgData.ToPort,
AuthMode: "0",
}
c.JSON(200, result.Err(nil))
sshClient, err := connSSH.NewClient()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(body.FilePath), body.FileName)
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
c.JSON(200, result.ErrMsg("error uploading file"))
return
}
c.JSON(200, result.Ok(nil))
}

View File

@@ -17,10 +17,12 @@ func Register(r *gin.RouterGroup) {
)
lmTable.POST("/ftp",
middleware.PreAuthorize(nil),
middleware.CryptoApi(true, false),
m.SetFTPConfig,
)
lmTable.GET("/ftp",
middleware.PreAuthorize(nil),
middleware.CryptoApi(false, true),
m.GetFTPConfig,
)
lmTable.PUT("/ftp",

View File

@@ -15,6 +15,8 @@ import (
"be.ems/lib/log"
"be.ems/lib/services"
tokenConst "be.ems/src/framework/constants/token"
neDataModel "be.ems/src/modules/network_data/model"
neDataService "be.ems/src/modules/network_data/service"
"be.ems/src/modules/network_element/model"
neService "be.ems/src/modules/network_element/service"
"github.com/go-resty/resty/v2"
@@ -35,6 +37,7 @@ type N3iwfUEData struct {
}
var (
UriNBState = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbState"
UriNBInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
UriSMFUEInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/smf/objectType/ueInfo"
UriIMSUEInfo = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/ims/objectType/ueInfo"
@@ -46,6 +49,7 @@ var (
UriNSSFAvailableAMFs = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/availableAMFs"
UriNSSFSubscriptions = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/subscriptions"
CustomUriNBState = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbState"
CustomUriNBInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
CustomUriSMFUEInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/smf/objectType/ueInfo"
CustomUriIMSUEInfo = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/ims/objectType/ueInfo"
@@ -1041,6 +1045,71 @@ func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
}
}
// PostNBInfoFromNF 接收Radio数据请求
func PostNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
log.Info("PostNBInfoFromNF processing... ")
neType := ctx.GetParam(r, "elementTypeValue")
var body struct {
NeType string `json:"neType" `
NeName string `json:"neName" `
RmUID string `json:"rmUID"`
StateList []struct {
Address string `json:"address" `
Name string `json:"name" `
Position string `json:"position" `
NbName string `json:"nbName" `
State string `json:"state" ` // "OFF" or "ON"
OffTime string `json:"offTime" ` //if State=OFF, will set it
OnTime string `json:"onTime" ` //if State=ON , will set it
}
}
if err := ctx.ShouldBindJSON(r, &body); err != nil {
services.ResponseInternalServerError500ProcessError(w, err)
return
}
neTypeLower := strings.ToLower(body.NeType)
if neType == "" || neType != neTypeLower {
services.ResponseInternalServerError500ProcessError(w, fmt.Errorf("inconsistent network element types"))
return
}
neInfo := neService.NewNeInfo.SelectNeInfoByRmuid(body.RmUID)
if neInfo.RmUID != body.RmUID {
services.ResponseInternalServerError500ProcessError(w, fmt.Errorf("inconsistent network element rmUID"))
return
}
if len(body.StateList) == 0 {
services.ResponseInternalServerError500ProcessError(w, fmt.Errorf("no stateList"))
return
}
nbStateService := neDataService.NewNBState
for _, v := range body.StateList {
if v.Address == "" || v.State == "" {
continue
}
timeStr := v.OffTime
if v.State == "ON" {
timeStr = v.OnTime
}
nbStateService.Insert(neDataModel.NBState{
NeType: neInfo.NeType,
NeId: neInfo.NeId,
RmUid: neInfo.RmUID,
Address: v.Address,
Name: v.Name,
Position: v.Position,
NbName: v.NbName,
State: v.State,
Time: timeStr,
})
}
services.ResponseStatusOK204NoContent(w)
}
// Get Radio Info from NF/NFs
func GetNBInfoAllFromNF(w http.ResponseWriter, r *http.Request) {
log.Info("GetNBInfoAllFromNF processing... ")