package service import ( "encoding/json" "fmt" "io" "time" "nms_cxy/src/framework/logger" "nms_cxy/src/framework/utils/ssh" "nms_cxy/src/framework/utils/telnet" "nms_cxy/src/framework/vo/result" "nms_cxy/src/modules/ws/model" "nms_cxy/src/modules/ws/processor" ) // 实例化服务层 WSReceiveImpl 结构体 var NewWSReceiveImpl = &WSReceiveImpl{} // WSReceiveImpl WebSocket消息接收处理 服务层处理 type WSReceiveImpl struct{} // Commont 接收通用业务处理 func (s *WSReceiveImpl) close(client *model.WSClient) { // 主动关闭 resultByte, _ := json.Marshal(result.OkMsg("user initiated closure")) client.MsgChan <- resultByte // 等待1s后关闭连接 time.Sleep(1 * time.Second) NewWSImpl.ClientClose(client.ID) } // Commont 接收通用业务处理 func (s *WSReceiveImpl) Commont(client *model.WSClient, reqMsg model.WSRequest) { // 必传requestId确认消息 if reqMsg.RequestID == "" { msg := "message requestId is required" logger.Infof("ws Commont UID %s err: %s", client.BindUid, msg) msgByte, _ := json.Marshal(result.ErrMsg(msg)) client.MsgChan <- msgByte return } var resByte []byte var err error switch reqMsg.Type { case "close": s.close(client) return case "ps": resByte, err = processor.GetProcessData(reqMsg.RequestID, reqMsg.Data) case "net": resByte, err = processor.GetNetConnections(reqMsg.RequestID, reqMsg.Data) case "ims_cdr": resByte, err = processor.GetCDRConnectByIMS(reqMsg.RequestID, reqMsg.Data) case "smf_cdr": resByte, err = processor.GetCDRConnectBySMF(reqMsg.RequestID, reqMsg.Data) case "smsc_cdr": resByte, err = processor.GetCDRConnectBySMSC(reqMsg.RequestID, reqMsg.Data) case "amf_ue": resByte, err = processor.GetUEConnectByAMF(reqMsg.RequestID, reqMsg.Data) case "mme_ue": resByte, err = processor.GetUEConnectByMME(reqMsg.RequestID, reqMsg.Data) case "upf_tf": resByte, err = processor.GetUPFTotalFlow(reqMsg.RequestID, reqMsg.Data) case "ne_state": resByte, err = processor.GetNeState(reqMsg.RequestID, reqMsg.Data) default: err = fmt.Errorf("message type %s not supported", reqMsg.Type) } if err != nil { logger.Warnf("ws Commont UID %s err: %s", client.BindUid, err.Error()) msgByte, _ := json.Marshal(result.ErrMsg(err.Error())) client.MsgChan <- msgByte return } if len(resByte) > 0 { client.MsgChan <- resByte } } // Shell 接收终端交互业务处理 func (s *WSReceiveImpl) Shell(client *model.WSClient, reqMsg model.WSRequest) { // 必传requestId确认消息 if reqMsg.RequestID == "" { msg := "message requestId is required" logger.Infof("ws Shell UID %s err: %s", client.BindUid, msg) msgByte, _ := json.Marshal(result.ErrMsg(msg)) client.MsgChan <- msgByte return } var resByte []byte var err error switch reqMsg.Type { case "close": s.close(client) return case "ssh": // SSH会话消息接收写入会话 command := reqMsg.Data.(string) sshClientSession := client.ChildConn.(*ssh.SSHClientSession) _, err = sshClientSession.Write(command) case "ssh_resize": // SSH会话窗口重置 msgByte, _ := json.Marshal(reqMsg.Data) var data struct { Cols int `json:"cols"` Rows int `json:"rows"` } err = json.Unmarshal(msgByte, &data) if err == nil { sshClientSession := client.ChildConn.(*ssh.SSHClientSession) err = sshClientSession.Session.WindowChange(data.Rows, data.Cols) } default: err = fmt.Errorf("message type %s not supported", reqMsg.Type) } if err != nil { logger.Warnf("ws Shell UID %s err: %s", client.BindUid, err.Error()) msgByte, _ := json.Marshal(result.ErrMsg(err.Error())) client.MsgChan <- msgByte if err == io.EOF { // 等待1s后关闭连接 time.Sleep(1 * time.Second) client.StopChan <- struct{}{} } return } if len(resByte) > 0 { client.MsgChan <- resByte } } // ShellView 接收查看文件终端交互业务处理 func (s *WSReceiveImpl) ShellView(client *model.WSClient, reqMsg model.WSRequest) { // 必传requestId确认消息 if reqMsg.RequestID == "" { msg := "message requestId is required" logger.Infof("ws ShellView UID %s err: %s", client.BindUid, msg) msgByte, _ := json.Marshal(result.ErrMsg(msg)) client.MsgChan <- msgByte return } var resByte []byte var err error switch reqMsg.Type { case "close": s.close(client) return case "cat", "tail": var command string if reqMsg.Type == "cat" { command, err = processor.ParseCat(reqMsg.Data) } if reqMsg.Type == "tail" { command, err = processor.ParseTail(reqMsg.Data) } if command != "" && err == nil { sshClientSession := client.ChildConn.(*ssh.SSHClientSession) _, err = sshClientSession.Write(command) } case "ctrl-c": // 模拟按下 Ctrl+C sshClientSession := client.ChildConn.(*ssh.SSHClientSession) _, err = sshClientSession.Write("\u0003\n") case "resize": // 会话窗口重置 msgByte, _ := json.Marshal(reqMsg.Data) var data struct { Cols int `json:"cols"` Rows int `json:"rows"` } err = json.Unmarshal(msgByte, &data) if err == nil { sshClientSession := client.ChildConn.(*ssh.SSHClientSession) err = sshClientSession.Session.WindowChange(data.Rows, data.Cols) } default: err = fmt.Errorf("message type %s not supported", reqMsg.Type) } if err != nil { logger.Warnf("ws ShellView UID %s err: %s", client.BindUid, err.Error()) msgByte, _ := json.Marshal(result.ErrMsg(err.Error())) client.MsgChan <- msgByte if err == io.EOF { // 等待1s后关闭连接 time.Sleep(1 * time.Second) client.StopChan <- struct{}{} } return } if len(resByte) > 0 { client.MsgChan <- resByte } } // Telnet 接收终端交互业务处理 func (s *WSReceiveImpl) Telnet(client *model.WSClient, reqMsg model.WSRequest) { // 必传requestId确认消息 if reqMsg.RequestID == "" { msg := "message requestId is required" logger.Infof("ws Shell UID %s err: %s", client.BindUid, msg) msgByte, _ := json.Marshal(result.ErrMsg(msg)) client.MsgChan <- msgByte return } var resByte []byte var err error switch reqMsg.Type { case "close": s.close(client) return case "telnet": // Telnet会话消息接收写入会话 command := reqMsg.Data.(string) telnetClientSession := client.ChildConn.(*telnet.TelnetClientSession) _, err = telnetClientSession.Write(command) case "telnet_resize": // Telnet会话窗口重置 msgByte, _ := json.Marshal(reqMsg.Data) var data struct { Cols int `json:"cols"` Rows int `json:"rows"` } err = json.Unmarshal(msgByte, &data) if err == nil { telnetClientSession := client.ChildConn.(*telnet.TelnetClientSession) err = telnetClientSession.WindowChange(data.Rows, data.Cols) _ = telnetClientSession.Read() } default: err = fmt.Errorf("message type %s not supported", reqMsg.Type) } if err != nil { logger.Warnf("ws Shell UID %s err: %s", client.BindUid, err.Error()) msgByte, _ := json.Marshal(result.ErrMsg(err.Error())) client.MsgChan <- msgByte if err == io.EOF { // 等待1s后关闭连接 time.Sleep(1 * time.Second) client.StopChan <- struct{}{} } return } if len(resByte) > 0 { client.MsgChan <- resByte } }