fix: 代码优化

This commit is contained in:
TsMask
2025-07-15 15:16:44 +08:00
parent 9f6a7f3bb7
commit 150551acce
12 changed files with 202 additions and 130 deletions

View File

@@ -42,10 +42,14 @@ func (c *ConnTelnet) NewClient() (*ConnTelnet, error) {
}
// 进行登录
time.Sleep(100 * time.Millisecond)
client.Write([]byte(c.User + "\r\n"))
time.Sleep(100 * time.Millisecond)
client.Write([]byte(c.Password + "\r\n"))
if c.User != "" {
time.Sleep(100 * time.Millisecond)
client.Write([]byte(c.User + "\r\n"))
}
if c.Password != "" {
time.Sleep(100 * time.Millisecond)
client.Write([]byte(c.Password + "\r\n"))
}
// fmt.Fprintln(client, c.User)
// fmt.Fprintln(client, c.Password)
@@ -103,6 +107,19 @@ func (c *ConnTelnet) RunCMD(cmd string) (string, error) {
return c.LastResult, nil
}
// WindowChange informs the remote host about a terminal window dimension change to h rows and w columns.
func (s *ConnTelnet) WindowChange(h, w int) error {
if s.Client == nil {
return fmt.Errorf("client is nil to content write failed")
}
conn := *s.Client
// 需要确保接收方理解并正确处理发送窗口大小设置命令
conn.Write([]byte{255, 251, 31})
conn.Write([]byte{255, 250, 31, byte(w >> 8), byte(w & 0xFF), byte(h >> 8), byte(h & 0xFF), 255, 240})
return nil
}
// NewClient 创建Telnet客户端会话对象
func (c *ConnTelnet) NewClientSession(cols, rows int) (*TelnetClientSession, error) {
if c.Client == nil {

View File

@@ -54,9 +54,9 @@ func ParseDateToStr(date any, formatStr string) string {
if v == 0 {
return ""
}
if v > 9999999999 {
if v > 1e12 {
t = time.UnixMilli(v)
} else if v > 999999999 {
} else if v > 1e9 {
t = time.Unix(v, 0)
} else {
logger.Infof("utils ParseDateToStr err %v", "Invalid timestamp")

View File

@@ -0,0 +1,44 @@
package expr
import (
"fmt"
"regexp"
"strings"
"github.com/expr-lang/expr"
)
// Eval 计算表达式返回结果
func Eval(exprStr string, env map[string]any) (any, error) {
return expr.Eval(exprStr, env)
}
// ParseExprEnv 解析表达式环境变量
// 比如 "('SMF.03'/'SMF.04')*100"
// 变量传入"SMF.03": 3
func ParseExprEnv(exprStr string, env map[string]any) (string, map[string]any) {
// 使用正则表达式匹配带单引号的变量名
re := regexp.MustCompile(`'([^']+)'`)
tempEnv := make(map[string]any)
tempExpr := exprStr
varCount := 0
matches := re.FindAllStringSubmatch(exprStr, -1)
for _, match := range matches {
paramName := match[1]
tempVarName := fmt.Sprintf("var%d", varCount)
tempEnv[tempVarName] = env[paramName]
tempExpr = strings.Replace(tempExpr, match[0], tempVarName, 1)
varCount++
}
// 合并临时环境变量和原环境变量
combinedEnv := make(map[string]any)
for k, v := range env {
combinedEnv[k] = v
}
for k, v := range tempEnv {
combinedEnv[k] = v
}
return tempExpr, combinedEnv
}