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

This commit is contained in:
TsMask
2025-01-22 19:43:58 +08:00
17 changed files with 353 additions and 23 deletions

View File

@@ -175,6 +175,8 @@ aes:
apiKey: "T9ox2DCzpLfJIPzkH9pKhsOTMOEMJcFv"
# 网元主机密钥
hostKey: "AGT66VfY4SMaiT97a7df0aef1704d5c5"
# 应用密钥
appKey: "E83dbfeb35BA4839232e2761b0FE5f32"
# 用户配置
user:

View File

@@ -41,7 +41,7 @@ func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) {
// 是否远程客户端读取
if sshClient == nil {
resultStr, err := cmd.Execf(cmdStr)
resultStr, err := cmd.Exec(cmdStr)
if err != nil {
logger.Errorf("Ne FileList Path: %s, Search: %s, Error:%s", path, search, err.Error())
return rows, err

View File

@@ -135,10 +135,10 @@ func (s *SSHClientSFTP) CopyFileLocalToRemote(localPath, remotePath string) erro
defer localFile.Close()
// 创建远程目录
// if err := s.Client.MkdirAll(filepath.Dir(remotePath)); err != nil {
// logger.Errorf("CopyFileLocalToRemote failed to creating remote directory %s: => %s", remotePath, err.Error())
// return err
// }
if err := s.Client.MkdirAll(filepath.Dir(remotePath)); err != nil {
logger.Errorf("CopyFileLocalToRemote failed to creating remote directory %s: => %s", remotePath, err.Error())
return err
}
// 创建远程文件
remoteFile, err := s.Client.Create(remotePath)

View File

@@ -6,11 +6,19 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/framework/config"
"be.ems/src/framework/cron"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/crypto"
"be.ems/src/framework/utils/ssh"
systemService "be.ems/src/modules/system/service"
"github.com/jlaffaye/ftp"
)
var NewProcessor = &BarProcessor{
@@ -94,6 +102,9 @@ func (s *BarProcessor) Execute(data any) (any, error) {
return nil, err
}
// put ftp
s.putFTP(params.FilePath, filepath.Base(filePath))
// 返回结果,用于记录执行结果
return map[string]any{
"msg": "sucess",
@@ -158,3 +169,94 @@ func (s *BarProcessor) exportData(query, filePath string) (int64, error) {
return affected, nil
}
func (s BarProcessor) putFTP(filePath, fileName string) {
// 获取配置
var cfgData struct {
Password string `json:"password" `
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"`
Dir string `json:"dir" binding:"required"`
}
cfg := systemService.NewSysConfigImpl.SelectConfigByKey("sys.exportTable")
if cfg.ConfigID != "" {
// 解密body
appKey := config.Get("aes.appKey").(string)
bodyDe, err := crypto.AESDecryptBase64(cfg.ConfigValue, appKey)
if err != nil {
logger.Errorf("putFTP decrypt error: %v", err)
return
}
err = json.Unmarshal([]byte(bodyDe), &cfgData)
if err != nil {
logger.Errorf("putFTP unmarshal error: %v", err)
return
}
}
localFilePath := filepath.Join(filePath, fileName)
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 {
logger.Errorf("putFTP ssh error: %v", err)
return
}
defer sshClient.Close()
// 网元主机的SSH客户端进行文件传输
sftpClient, err := sshClient.NewClientSFTP()
if err != nil {
logger.Errorf("putFTP sftp error: %v", err)
return
}
defer sftpClient.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(filePath), fileName)
// 复制到远程
if err = sftpClient.CopyFileLocalToRemote(localFilePath, remotePath); err != nil {
logger.Errorf("putFTP uploading error: %v", err)
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 {
logger.Errorf("putFTP ftp error: %v", err)
return
}
// 登录到 FTP 服务器
err = ftpComm.Login(cfgData.Username, cfgData.Password)
if err != nil {
logger.Errorf("putFTP login error: %v", err)
return
}
defer ftpComm.Quit()
// 打开本地文件
file, err := os.Open(localFilePath)
if err != nil {
logger.Errorf("putFTP open error: %v", err)
return
}
defer file.Close()
// 远程文件
remotePath := filepath.Join(cfgData.Dir, path.Base(filePath), fileName)
// 上传文件到 FTP 服务器
err = ftpComm.Stor(remotePath, file)
if err != nil {
logger.Errorf("putFTP uploading error: %v", err)
return
}
}
}

View File

@@ -12,6 +12,7 @@ import (
"be.ems/src/framework/logger"
"be.ems/src/framework/redis"
"be.ems/src/framework/telnet"
"be.ems/src/framework/utils/generate"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/ssh"
neFetchlink "be.ems/src/modules/network_element/fetch_link"
@@ -243,9 +244,10 @@ func (r *NeInfo) SelectById(infoId string, bandHost bool) model.NeInfo {
func (r *NeInfo) Insert(neInfo model.NeInfo) string {
// 主机信息新增
if neInfo.Hosts != nil {
uuid := generate.Code(4)
var hostIDs []string
for _, host := range neInfo.Hosts {
host.Title = fmt.Sprintf("%s_%s_%d", strings.ToUpper(neInfo.NeType), neInfo.NeId, host.Port)
host.Title = neInfo.NeName + "_" + uuid
host.GroupID = "1"
host.CreateBy = neInfo.CreateBy
hostId := NewNeHost.Insert(host)
@@ -268,9 +270,10 @@ func (r *NeInfo) Insert(neInfo model.NeInfo) string {
func (r *NeInfo) Update(neInfo model.NeInfo) int64 {
// 主机信息更新
if neInfo.Hosts != nil {
uuid := generate.Code(4)
for _, host := range neInfo.Hosts {
if host.HostID != "" {
host.Title = fmt.Sprintf("%s_%s_%d", strings.ToUpper(neInfo.NeType), neInfo.NeId, host.Port)
host.Title = neInfo.NeName + "_" + uuid
host.GroupID = "1"
host.UpdateBy = neInfo.UpdateBy
NewNeHost.Update(host)