fix: ims telnet issue
This commit is contained in:
@@ -5,5 +5,5 @@ ims:
|
|||||||
- operation: "help"
|
- operation: "help"
|
||||||
object: ""
|
object: ""
|
||||||
display: "List IMS MML CMD"
|
display: "List IMS MML CMD"
|
||||||
status: "Inactive"
|
status: "Active"
|
||||||
params:
|
params:
|
||||||
|
|||||||
@@ -264,6 +264,80 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
// mmlResult = append(mmlResult, "COMMAND OK\n")
|
// mmlResult = append(mmlResult, "COMMAND OK\n")
|
||||||
}
|
}
|
||||||
case "ims":
|
case "ims":
|
||||||
|
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
|
||||||
|
conn, err := net.Dial("tcp", hostMML)
|
||||||
|
if err != nil {
|
||||||
|
errMsg := fmt.Sprintf("Failed to dial %s: %v", hostMML, err)
|
||||||
|
log.Error(errMsg)
|
||||||
|
mmlResult = append(mmlResult, errMsg)
|
||||||
|
response := Response{mmlResult}
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// localAddr := conn.LocalAddr()
|
||||||
|
// remoteAddr := conn.RemoteAddr()
|
||||||
|
// if localAddr == nil || remoteAddr == nil {
|
||||||
|
// errMsg := fmt.Sprintf("connect invalid: localAddr=%v, remoteAddr=%v", localAddr, remoteAddr)
|
||||||
|
// log.Error(errMsg)
|
||||||
|
// mmlResult = append(mmlResult, errMsg)
|
||||||
|
// response := Response{mmlResult}
|
||||||
|
// services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
||||||
|
|
||||||
|
_, err = conn.Write([]byte(config.GetYamlConfig().MML.User + "\r\n"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to write:", err)
|
||||||
|
mmlResult = append(mmlResult, err.Error())
|
||||||
|
response := Response{mmlResult}
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
||||||
|
|
||||||
|
n, err = conn.Read(buf[0:])
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to read:", err)
|
||||||
|
mmlResult = append(mmlResult, err.Error())
|
||||||
|
response := Response{mmlResult}
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debug(string(buf[0: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())
|
||||||
|
response := Response{mmlResult}
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
// mmlResult = append(mmlResult, err.Error())
|
||||||
|
// response := Response{mmlResult}
|
||||||
|
// services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
||||||
|
|
||||||
|
n, err = conn.Read(buf[0:])
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Failed to read:", err)
|
||||||
|
mmlResult = append(mmlResult, err.Error())
|
||||||
|
response := Response{mmlResult}
|
||||||
|
services.ResponseWithJson(w, http.StatusOK, response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Trace(string(buf[0 : n-len(neType)-2]))
|
||||||
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("io.ReadAll is failed:", err)
|
log.Error("io.ReadAll is failed:", err)
|
||||||
@@ -276,81 +350,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) {
|
|||||||
_ = json.Unmarshal(body, mmlRequest)
|
_ = json.Unmarshal(body, mmlRequest)
|
||||||
|
|
||||||
for _, mml := range mmlRequest.MML {
|
for _, mml := range mmlRequest.MML {
|
||||||
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
|
mmlCommand := fmt.Sprintf("%s\r\n", mml)
|
||||||
conn, err := net.Dial("tcp", hostMML)
|
|
||||||
if err != nil {
|
|
||||||
errMsg := fmt.Sprintf("Failed to dial %s: %v", hostMML, err)
|
|
||||||
log.Error(errMsg)
|
|
||||||
mmlResult = append(mmlResult, errMsg)
|
|
||||||
response := Response{mmlResult}
|
|
||||||
services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// localAddr := conn.LocalAddr()
|
|
||||||
// remoteAddr := conn.RemoteAddr()
|
|
||||||
// if localAddr == nil || remoteAddr == nil {
|
|
||||||
// errMsg := fmt.Sprintf("connect invalid: localAddr=%v, remoteAddr=%v", localAddr, remoteAddr)
|
|
||||||
// log.Error(errMsg)
|
|
||||||
// mmlResult = append(mmlResult, errMsg)
|
|
||||||
// response := Response{mmlResult}
|
|
||||||
// services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
|
||||||
|
|
||||||
_, err = conn.Write([]byte(config.GetYamlConfig().MML.User + "\n"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to write:", err)
|
|
||||||
mmlResult = append(mmlResult, err.Error())
|
|
||||||
response := Response{mmlResult}
|
|
||||||
services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
|
||||||
|
|
||||||
n, err = conn.Read(buf[0:])
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to read:", err)
|
|
||||||
mmlResult = append(mmlResult, err.Error())
|
|
||||||
response := Response{mmlResult}
|
|
||||||
services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debug(string(buf[0:n]))
|
|
||||||
|
|
||||||
_, err = conn.Write([]byte(config.GetYamlConfig().MML.Password + "\n"))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to write:", err)
|
|
||||||
mmlResult = append(mmlResult, err.Error())
|
|
||||||
response := Response{mmlResult}
|
|
||||||
services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
|
|
||||||
|
|
||||||
// loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password)
|
|
||||||
// _, err = conn.Write([]byte(loginStr))
|
|
||||||
// if err != nil {
|
|
||||||
// log.Error("Failed to write:", err)
|
|
||||||
// mmlResult = append(mmlResult, err.Error())
|
|
||||||
// response := Response{mmlResult}
|
|
||||||
// services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
n, err = conn.Read(buf[0:])
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Failed to read:", err)
|
|
||||||
mmlResult = append(mmlResult, err.Error())
|
|
||||||
response := Response{mmlResult}
|
|
||||||
services.ResponseWithJson(w, http.StatusOK, response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Trace(string(buf[0 : n-len(neType)-2]))
|
|
||||||
|
|
||||||
mmlCommand := fmt.Sprintf("%s\n", mml)
|
|
||||||
log.Debug("mml command:", mmlCommand)
|
log.Debug("mml command:", mmlCommand)
|
||||||
_, err = conn.Write([]byte(mmlCommand))
|
_, err = conn.Write([]byte(mmlCommand))
|
||||||
if err != nil {
|
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]`) // 匹配空字符和包含␛的控制字符
|
re2 := regexp.MustCompile(`[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\x1B]`) // 匹配空字符和包含␛的控制字符
|
||||||
//re := regexp.MustCompile(`[\x00-\x1F\x7F]`)
|
//re := regexp.MustCompile(`[\x00-\x1F\x7F]`)
|
||||||
// upf telnet buffer只能读取一次,需要去掉前面的多余字符
|
// upf telnet buffer只能读取一次,需要去掉前面的多余字符
|
||||||
//result := re1.ReplaceAllString(string(buf[0:n-len(neType)-2]), "")
|
result := re1.ReplaceAllString(string(buf[0:n-len(neType)-2]), "")
|
||||||
result := re1.ReplaceAllString(string(buf[0:]), "")
|
|
||||||
result = re2.ReplaceAllString(result, "")
|
result = re2.ReplaceAllString(result, "")
|
||||||
mmlResult = append(mmlResult, result)
|
mmlResult = append(mmlResult, result)
|
||||||
conn.Close()
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
|
hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
|
||||||
|
|||||||
Reference in New Issue
Block a user