feat: 更新多个模块以支持新的数据结构和日志格式

This commit is contained in:
TsMask
2025-02-20 10:08:27 +08:00
parent 045a2b6b01
commit f3c33b31ac
272 changed files with 13246 additions and 15885 deletions

View File

@@ -7,8 +7,8 @@ import (
"time"
"be.ems/src/framework/logger"
"be.ems/src/framework/resp"
"be.ems/src/framework/utils/generate"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/ws/model"
"github.com/gorilla/websocket"
)
@@ -53,7 +53,7 @@ func (s *WS) UpgraderWs(w http.ResponseWriter, r *http.Request) *websocket.Conn
// groupIDs 用户订阅组
// conn ws连接实例
// childConn 子连接实例
func (s *WS) ClientCreate(uid string, groupIDs []string, conn *websocket.Conn, childConn any) *model.WSClient {
func (s *WS) ClientCreate(uid int64, groupIDs []string, conn *websocket.Conn, childConn any) *model.WSClient {
// clientID也可以用其他方式生成只要能保证在所有服务端中都能保证唯一即可
clientID := generate.Code(16)
@@ -72,7 +72,7 @@ func (s *WS) ClientCreate(uid string, groupIDs []string, conn *websocket.Conn, c
wsClients.Store(clientID, wsClient)
// 存入用户持有客户端
if uid != "" {
if uid > 0 {
if v, ok := wsUsers.Load(uid); ok {
uidClientIds := v.(*[]string)
*uidClientIds = append(*uidClientIds, clientID)
@@ -82,7 +82,7 @@ func (s *WS) ClientCreate(uid string, groupIDs []string, conn *websocket.Conn, c
}
// 存入用户订阅组
if uid != "" && len(groupIDs) > 0 {
if uid > 0 && len(groupIDs) > 0 {
for _, groupID := range groupIDs {
if v, ok := wsGroup.Load(groupID); ok {
groupClientIds := v.(*[]string)
@@ -112,7 +112,7 @@ func (s *WS) ClientClose(clientID string) {
}()
// 客户端断线时自动踢出Uid绑定列表
if client.BindUid != "" {
if client.BindUid > 0 {
if v, ok := wsUsers.Load(client.BindUid); ok {
uidClientIds := v.(*[]string)
if len(*uidClientIds) > 0 {
@@ -160,7 +160,7 @@ func (s *WS) ClientReadListen(wsClient *model.WSClient, receiveFn func(*model.WS
// 读取消息
messageType, msg, err := wsClient.Conn.ReadMessage()
if err != nil {
logger.Warnf("ws ReadMessage UID %s err: %s", wsClient.BindUid, err.Error())
logger.Warnf("ws ReadMessage UID %d err: %s", wsClient.BindUid, err.Error())
s.ClientClose(wsClient.ID)
return
}
@@ -170,7 +170,7 @@ func (s *WS) ClientReadListen(wsClient *model.WSClient, receiveFn func(*model.WS
if messageType == websocket.TextMessage {
var reqMsg model.WSRequest
if err := json.Unmarshal(msg, &reqMsg); err != nil {
msgByte, _ := json.Marshal(result.ErrMsg("message format json error"))
msgByte, _ := json.Marshal(resp.ErrMsg("message format json error"))
wsClient.MsgChan <- msgByte
continue
}
@@ -189,7 +189,7 @@ func (s *WS) ClientWriteListen(wsClient *model.WSClient) {
}
}()
// 发客户端id确认是否连接
msgByte, _ := json.Marshal(result.OkData(map[string]string{
msgByte, _ := json.Marshal(resp.OkData(map[string]string{
"clientId": wsClient.ID,
}))
wsClient.MsgChan <- msgByte
@@ -203,7 +203,7 @@ func (s *WS) ClientWriteListen(wsClient *model.WSClient) {
// 发送消息
err := wsClient.Conn.WriteMessage(websocket.TextMessage, msg)
if err != nil {
logger.Warnf("ws WriteMessage UID %s err: %s", wsClient.BindUid, err.Error())
logger.Warnf("ws WriteMessage UID %d err: %s", wsClient.BindUid, err.Error())
s.ClientClose(wsClient.ID)
return
}

View File

@@ -8,11 +8,11 @@ import (
"strings"
"time"
"be.ems/src/framework/database/redis"
"be.ems/src/framework/logger"
"be.ems/src/framework/redis"
"be.ems/src/framework/resp"
"be.ems/src/framework/ssh"
"be.ems/src/framework/telnet"
"be.ems/src/framework/utils/ssh"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/ws/model"
"be.ems/src/modules/ws/processor"
)
@@ -26,7 +26,7 @@ type WSReceive struct{}
// close 关闭服务连接
func (s *WSReceive) close(client *model.WSClient) {
// 主动关闭
resultByte, _ := json.Marshal(result.OkMsg("user initiated closure"))
resultByte, _ := json.Marshal(resp.OkMsg("user initiated closure"))
client.MsgChan <- resultByte
// 等待1s后关闭连接
time.Sleep(1 * time.Second)
@@ -38,8 +38,8 @@ func (s *WSReceive) 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))
logger.Infof("ws Commont UID %d err: %s", client.BindUid, msg)
msgByte, _ := json.Marshal(resp.ErrMsg(msg))
client.MsgChan <- msgByte
return
}
@@ -74,8 +74,8 @@ func (s *WSReceive) Commont(client *model.WSClient, reqMsg model.WSRequest) {
}
if err != nil {
logger.Warnf("ws Commont UID %s err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(result.ErrMsg(err.Error()))
logger.Warnf("ws Commont UID %d err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(resp.ErrMsg(err.Error()))
client.MsgChan <- msgByte
return
}
@@ -89,8 +89,8 @@ func (s *WSReceive) 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))
logger.Infof("ws Shell UID %d err: %s", client.BindUid, msg)
msgByte, _ := json.Marshal(resp.ErrMsg(msg))
client.MsgChan <- msgByte
return
}
@@ -124,8 +124,8 @@ func (s *WSReceive) Shell(client *model.WSClient, reqMsg model.WSRequest) {
}
if err != nil {
logger.Warnf("ws Shell UID %s err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(result.ErrMsg(err.Error()))
logger.Warnf("ws Shell UID %d err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(resp.ErrMsg(err.Error()))
client.MsgChan <- msgByte
if err == io.EOF {
// 等待1s后关闭连接
@@ -144,8 +144,8 @@ func (s *WSReceive) 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))
logger.Infof("ws ShellView UID %d err: %s", client.BindUid, msg)
msgByte, _ := json.Marshal(resp.ErrMsg(msg))
client.MsgChan <- msgByte
return
}
@@ -190,8 +190,8 @@ func (s *WSReceive) ShellView(client *model.WSClient, reqMsg model.WSRequest) {
}
if err != nil {
logger.Warnf("ws ShellView UID %s err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(result.ErrMsg(err.Error()))
logger.Warnf("ws ShellView UID %d err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(resp.ErrMsg(err.Error()))
client.MsgChan <- msgByte
if err == io.EOF {
// 等待1s后关闭连接
@@ -210,8 +210,8 @@ func (s *WSReceive) 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))
logger.Infof("ws Shell UID %d err: %s", client.BindUid, msg)
msgByte, _ := json.Marshal(resp.ErrMsg(msg))
client.MsgChan <- msgByte
return
}
@@ -245,8 +245,8 @@ func (s *WSReceive) Telnet(client *model.WSClient, reqMsg model.WSRequest) {
}
if err != nil {
logger.Warnf("ws Shell UID %s err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(result.ErrMsg(err.Error()))
logger.Warnf("ws Shell UID %d err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(resp.ErrMsg(err.Error()))
client.MsgChan <- msgByte
if err == io.EOF {
// 等待1s后关闭连接
@@ -265,8 +265,8 @@ func (s *WSReceive) Redis(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))
logger.Infof("ws Shell UID %d err: %s", client.BindUid, msg)
msgByte, _ := json.Marshal(resp.ErrMsg(msg))
client.MsgChan <- msgByte
return
}
@@ -309,7 +309,7 @@ func (s *WSReceive) Redis(client *model.WSClient, reqMsg model.WSRequest) {
dataStr = fmt.Sprintf("%s \r\n", output)
}
}
resByte, _ = json.Marshal(result.Ok(map[string]any{
resByte, _ = json.Marshal(resp.Ok(map[string]any{
"requestId": reqMsg.RequestID,
"data": dataStr,
}))
@@ -318,8 +318,8 @@ func (s *WSReceive) Redis(client *model.WSClient, reqMsg model.WSRequest) {
}
if err != nil {
logger.Warnf("ws Shell UID %s err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(result.ErrMsg(err.Error()))
logger.Warnf("ws Shell UID %d err: %s", client.BindUid, err.Error())
msgByte, _ := json.Marshal(resp.ErrMsg(err.Error()))
client.MsgChan <- msgByte
if err == io.EOF {
// 等待1s后关闭连接

View File

@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"be.ems/src/framework/vo/result"
"be.ems/src/framework/resp"
"be.ems/src/modules/ws/model"
)
@@ -34,6 +34,10 @@ const (
GROUP_AMF_UE = "1010"
// 组号-MME_UE会话事件 1011_neId
GROUP_MME_UE = "1011"
// 组号-告警 2000_neType_neId
GROUP_ALARM = "2000"
// 组号-告警事件 2000_neType_neId
GROUP_ALARM_EVENT = "2002"
)
// 实例化服务层 WSSend 结构体
@@ -49,7 +53,7 @@ func (s *WSSend) ByClientID(clientID string, data any) error {
return fmt.Errorf("no fount client ID: %s", clientID)
}
dataByte, err := json.Marshal(result.OkData(data))
dataByte, err := json.Marshal(resp.OkData(data))
if err != nil {
return err
}