feat: 更新框架工具库

This commit is contained in:
TsMask
2024-08-31 14:24:32 +08:00
parent ef704d36e3
commit a8e976039c
6 changed files with 61 additions and 74 deletions

View File

@@ -8,6 +8,7 @@ import (
"runtime"
"time"
"nms_cxy/src/framework/config"
"nms_cxy/src/framework/constants/common"
"nms_cxy/src/framework/logger"
"nms_cxy/src/framework/utils/cmd"
@@ -68,7 +69,8 @@ func codeFileRead() (map[string]any, error) {
}
content := string(bytes)
// 解密
contentDe, err := crypto.StringDecryptByAES(content)
hostKey := config.Get("aes.hostKey").(string)
contentDe, err := crypto.AESDecryptBase64(content, hostKey)
if err != nil {
logger.Errorf("CodeFileRead decrypt: %v", err.Error())
return mapData, fmt.Errorf("decrypt fail")
@@ -86,7 +88,8 @@ func codeFileRead() (map[string]any, error) {
func codeFileWrite(data map[string]any) error {
jsonByte, _ := json.Marshal(data)
// 加密
contentEn, err := crypto.StringEncryptByAES(string(jsonByte))
hostKey := config.Get("aes.hostKey").(string)
contentEn, err := crypto.AESEncryptBase64(string(jsonByte), hostKey)
if err != nil {
logger.Errorf("insert encrypt: %v", err.Error())
return fmt.Errorf("encrypt fail")
@@ -160,11 +163,11 @@ func Reset() error {
// return fmt.Errorf("not support window")
} else {
// 重置数据库
if _, err := cmd.Execf("sudo /usr/local/omc/bin/setomc.sh -m install"); err != nil {
if _, err := cmd.Execf("/usr/local/omc/bin/setomc.sh -m install"); err != nil {
return err
}
// 重启服务
if _, err := cmd.Execf("nohup sh -c \"sleep 1s && %s\" > /dev/null 2>&1 &", "sudo systemctl restart omc"); err != nil {
if _, err := cmd.Execf("nohup sh -c \"sleep 1s && %s\" > /dev/null 2>&1 &", "sudo systemctl restart restagent"); err != nil {
return err
}
}

View File

@@ -24,6 +24,13 @@ func (s *SSHClientSFTP) Close() {
// CopyDirRemoteToLocal 复制目录-远程到本地
func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
// 创建本地目录
err := os.MkdirAll(localDir, 0775)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to creating local directory %s: => %s", localDir, err.Error())
return err
}
// 列出远程目录中的文件和子目录
remoteFiles, err := s.Client.ReadDir(remoteDir)
if err != nil {
@@ -31,13 +38,6 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
return err
}
// 创建本地目录
err = os.MkdirAll(localDir, 0775)
if err != nil {
logger.Errorf("CopyDirRemoteToLocal failed to creating local directory %s: => %s", localDir, err.Error())
return err
}
// 遍历远程文件和子目录并复制到本地
for _, remoteFile := range remoteFiles {
remotePath := filepath.ToSlash(filepath.Join(remoteDir, remoteFile.Name()))
@@ -63,56 +63,33 @@ func (s *SSHClientSFTP) CopyDirRemoteToLocal(remoteDir, localDir string) error {
// CopyDirRemoteToLocal 复制目录-本地到远程
func (s *SSHClientSFTP) CopyDirLocalToRemote(localDir, remoteDir string) error {
// 创建远程目录
err := s.Client.MkdirAll(remoteDir)
if err != nil {
logger.Errorf("CopyDirLocalToRemote failed to creating remote directory %s: => %s", remoteDir, err.Error())
return err
}
// 遍历本地目录中的文件和子目录并复制到远程
err = filepath.Walk(localDir, func(localPath string, info os.FileInfo, err error) error {
err := filepath.Walk(localDir, func(localPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 生成远程路径
remotePath := filepath.Join(remoteDir, localPath[len(localDir):])
remotePath := filepath.ToSlash(filepath.Join(remoteDir, localPath[len(localDir):]))
if info.IsDir() {
// 如果是子目录,则创建远程目录
err := s.Client.MkdirAll(remotePath)
if err != nil {
if err := s.Client.MkdirAll(remotePath); err != nil {
logger.Errorf("CopyDirLocalToRemote failed to creating remote directory %s: => %s", remotePath, err.Error())
return nil
return err
}
} else {
// 如果是文件,则复制文件内容
localFile, err := os.Open(localPath)
if err != nil {
logger.Errorf("CopyDirLocalToRemote failed to opening local file %s: => %s", localPath, err.Error())
return nil
}
defer localFile.Close()
remoteFile, err := s.Client.Create(remotePath)
if err != nil {
logger.Errorf("CopyDirLocalToRemote failed to creating remote file %s: => %s", remotePath, err.Error())
return nil
}
defer remoteFile.Close()
_, err = io.Copy(remoteFile, localFile)
if err != nil {
logger.Errorf("CopyDirLocalToRemote failed to copying file contents from %s to %s: => %s", localPath, remotePath, err.Error())
return nil
if err := s.CopyFileLocalToRemote(localPath, remotePath); err != nil {
logger.Errorf("CopyDirLocalToRemote failed to copying remote file %s: => %s", localPath, err.Error())
return err
}
}
return nil
})
if err != nil {
logger.Errorf("CopyDirLocalToRemote failed to walking local directory: => %s", err.Error())
logger.Errorf("CopyDirLocalToRemote failed to walking remote directory: => %s", err.Error())
return err
}
return nil
@@ -141,8 +118,7 @@ func (s *SSHClientSFTP) CopyFileRemoteToLocal(remotePath, localPath string) erro
defer localFile.Close()
// 复制文件内容
_, err = io.Copy(localFile, remoteFile)
if err != nil {
if _, err = io.Copy(localFile, remoteFile); err != nil {
logger.Errorf("CopyFileRemoteToLocal failed to copying contents: => %s", err.Error())
return err
}
@@ -159,6 +135,12 @@ 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
// }
// 创建远程文件
remoteFile, err := s.Client.Create(remotePath)
if err != nil {
@@ -168,8 +150,7 @@ func (s *SSHClientSFTP) CopyFileLocalToRemote(localPath, remotePath string) erro
defer remoteFile.Close()
// 复制文件内容
_, err = io.Copy(remoteFile, localFile)
if err != nil {
if _, err = io.Copy(remoteFile, localFile); err != nil {
logger.Errorf("CopyFileLocalToRemote failed to copying contents: => %s", err.Error())
return err
}

View File

@@ -101,6 +101,7 @@ func (c *ConnSSH) RunCMD(cmd string) (string, error) {
defer session.Close()
buf, err := session.CombinedOutput(cmd)
if err != nil {
logger.Infof("RunCMD failed run command: => %s", cmd)
logger.Errorf("RunCMD failed run command: => %s", err.Error())
}
c.LastResult = string(buf)

View File

@@ -50,10 +50,12 @@ func (c *ConnTelnet) NewClient() (*ConnTelnet, error) {
// fmt.Fprintln(client, c.User)
// fmt.Fprintln(client, c.Password)
c.Client = &client
// 调整窗口大小 (120 列 x 128 行)
requestPty(c.Client, 120, 128)
// 需要确保接收方理解并正确处理发送窗口大小设置命令
client.Write([]byte{255, 251, 31})
client.Write([]byte{255, 250, 31, byte(120 >> 8), byte(120 & 0xFF), byte(128 >> 8), byte(128 & 0xFF), 255, 240})
c.Client = &client
// 排空连接登录的信息
c.RunCMD("")
@@ -111,20 +113,9 @@ func (c *ConnTelnet) NewClientSession(cols, rows int) (*TelnetClientSession, err
if c.Client == nil {
return nil, fmt.Errorf("telnet client not connected")
}
requestPty(c.Client, cols, rows)
return &TelnetClientSession{
s := &TelnetClientSession{
Client: *c.Client,
}, nil
}
// requestPty 调整终端窗口大小
func requestPty(client *net.Conn, cols, rows int) error {
if client == nil {
return fmt.Errorf("telnet client not connected")
}
conn := *client
// 需要确保接收方理解并正确处理发送窗口大小设置命令
conn.Write([]byte{255, 251, 31})
conn.Write([]byte{255, 250, 31, byte(cols >> 8), byte(cols & 0xFF), byte(rows >> 8), byte(rows & 0xFF), 255, 240})
return nil
s.WindowChange(cols, rows)
return s, nil
}

View File

@@ -19,6 +19,17 @@ func (s *TelnetClientSession) Close() {
}
}
// WindowChange informs the remote host about a terminal window dimension change to h rows and w columns.
func (s *TelnetClientSession) WindowChange(h, w int) error {
if s.Client == nil {
return fmt.Errorf("client is nil to content write failed")
}
// 需要确保接收方理解并正确处理发送窗口大小设置命令
s.Client.Write([]byte{255, 251, 31})
s.Client.Write([]byte{255, 250, 31, byte(w >> 8), byte(w & 0xFF), byte(h >> 8), byte(h & 0xFF), 255, 240})
return nil
}
// Write 写入命令 不带回车(\n)也会执行根据客户端情况
func (s *TelnetClientSession) Write(cmd string) (int, error) {
if s.Client == nil {

View File

@@ -1,7 +1,7 @@
package result
import (
"nms_cxy/src/framework/constants/result"
constResult "nms_cxy/src/framework/constants/result"
)
// CodeMsg 响应结果
@@ -15,8 +15,8 @@ func CodeMsg(code int, msg string) map[string]any {
// 响应成功结果 map[string]any{}
func Ok(v map[string]any) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_SUCCESS
args["msg"] = result.MSG_SUCCESS
args["code"] = constResult.CODE_SUCCESS
args["msg"] = constResult.MSG_SUCCESS
// v合并到args
for key, value := range v {
args[key] = value
@@ -27,7 +27,7 @@ func Ok(v map[string]any) map[string]any {
// 响应成功结果信息
func OkMsg(msg string) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_SUCCESS
args["code"] = constResult.CODE_SUCCESS
args["msg"] = msg
return args
}
@@ -35,8 +35,8 @@ func OkMsg(msg string) map[string]any {
// 响应成功结果数据
func OkData(data any) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_SUCCESS
args["msg"] = result.MSG_SUCCESS
args["code"] = constResult.CODE_SUCCESS
args["msg"] = constResult.MSG_SUCCESS
args["data"] = data
return args
}
@@ -44,8 +44,8 @@ func OkData(data any) map[string]any {
// 响应失败结果 map[string]any{}
func Err(v map[string]any) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_ERROR
args["msg"] = result.MSG_ERROR
args["code"] = constResult.CODE_ERROR
args["msg"] = constResult.MSG_ERROR
// v合并到args
for key, value := range v {
args[key] = value
@@ -56,7 +56,7 @@ func Err(v map[string]any) map[string]any {
// 响应失败结果信息
func ErrMsg(msg string) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_ERROR
args["code"] = constResult.CODE_ERROR
args["msg"] = msg
return args
}
@@ -64,8 +64,8 @@ func ErrMsg(msg string) map[string]any {
// 响应失败结果数据
func ErrData(data any) map[string]any {
args := make(map[string]any)
args["code"] = result.CODE_ERROR
args["msg"] = result.MSG_ERROR
args["code"] = constResult.CODE_ERROR
args["msg"] = constResult.MSG_ERROR
args["data"] = data
return args
}