fix: ims telnet issue

This commit is contained in:
2023-12-28 16:42:29 +08:00
parent c0046f421d
commit 0a243a5089
2 changed files with 77 additions and 79 deletions

View File

@@ -5,5 +5,5 @@ ims:
- operation: "help"
object: ""
display: "List IMS MML CMD"
status: "Inactive"
status: "Active"
params:

View File

@@ -264,18 +264,6 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
// mmlResult = append(mmlResult, "COMMAND OK\n")
}
case "ims":
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
if err != nil {
log.Error("io.ReadAll is failed:", err)
services.ResponseNotFound404UriNotExist(w, r)
return
}
log.Trace("Body:", string(body))
mmlRequest := new(MMLRequest)
_ = json.Unmarshal(body, mmlRequest)
for _, mml := range mmlRequest.MML {
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
conn, err := net.Dial("tcp", hostMML)
if err != nil {
@@ -300,7 +288,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
// }
conn.SetDeadline(time.Now().Add(10 * time.Second))
_, err = conn.Write([]byte(config.GetYamlConfig().MML.User + "\n"))
_, err = conn.Write([]byte(config.GetYamlConfig().MML.User + "\r\n"))
if err != nil {
log.Error("Failed to write:", err)
mmlResult = append(mmlResult, err.Error())
@@ -320,7 +308,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
}
log.Debug(string(buf[0:n]))
_, err = conn.Write([]byte(config.GetYamlConfig().MML.Password + "\n"))
_, err = conn.Write([]byte(config.GetYamlConfig().MML.Password + "\r\n"))
if err != nil {
log.Error("Failed to write:", err)
mmlResult = append(mmlResult, err.Error())
@@ -330,7 +318,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
}
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
// loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password)
// loginStr := fmt.Sprintf("%s\r\n%s\r\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password)
// _, err = conn.Write([]byte(loginStr))
// if err != nil {
// log.Error("Failed to write:", err)
@@ -339,6 +327,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
// services.ResponseWithJson(w, http.StatusOK, response)
// return
// }
// time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
n, err = conn.Read(buf[0:])
if err != nil {
@@ -349,8 +338,19 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
return
}
log.Trace(string(buf[0 : n-len(neType)-2]))
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
if err != nil {
log.Error("io.ReadAll is failed:", err)
services.ResponseNotFound404UriNotExist(w, r)
return
}
log.Trace("Body:", string(body))
mmlCommand := fmt.Sprintf("%s\n", mml)
mmlRequest := new(MMLRequest)
_ = json.Unmarshal(body, mmlRequest)
for _, mml := range mmlRequest.MML {
mmlCommand := fmt.Sprintf("%s\r\n", mml)
log.Debug("mml command:", mmlCommand)
_, err = conn.Write([]byte(mmlCommand))
if err != nil {
@@ -377,11 +377,9 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
re2 := regexp.MustCompile(`[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\x1B]`) // 匹配空字符和包含␛的控制字符
//re := regexp.MustCompile(`[\x00-\x1F\x7F]`)
// upf telnet buffer只能读取一次需要去掉前面的多余字符
//result := re1.ReplaceAllString(string(buf[0:n-len(neType)-2]), "")
result := re1.ReplaceAllString(string(buf[0:]), "")
result := re1.ReplaceAllString(string(buf[0:n-len(neType)-2]), "")
result = re2.ReplaceAllString(result, "")
mmlResult = append(mmlResult, result)
conn.Close()
}
default:
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)