feat: ws 身份认证

This commit is contained in:
TsMask
2024-01-25 10:40:00 +08:00
parent 13b5d35d0c
commit ea8b197e8b
3 changed files with 29 additions and 9 deletions

View File

@@ -65,6 +65,11 @@ func IPAddrLocation(c *gin.Context) (string, string) {
// Authorization 解析请求头
func Authorization(c *gin.Context) string {
// Query请求查询
if authQuery, ok := c.GetQuery(token.RESPONSE_FIELD); ok && authQuery != "" {
return authQuery
}
// Header请求头
authHeader := c.GetHeader(token.HEADER_KEY)
if authHeader == "" {
return ""
@@ -99,7 +104,15 @@ func UaOsBrowser(c *gin.Context) (string, string) {
// AcceptLanguage 解析客户端接收语言 zh中文 en: 英文
func AcceptLanguage(c *gin.Context) string {
preferredLanguage := language.English
acceptLanguage := c.GetHeader("Accept-Language")
acceptLanguage := "en_US"
// Query请求查询
if v, ok := c.GetQuery("lang"); ok && v != "" {
acceptLanguage = v
}
// Header请求头
if v := c.GetHeader("Accept-Language"); v != "" {
acceptLanguage = v
}
tags, _, _ := language.ParseAcceptLanguage(acceptLanguage)
if len(tags) > 0 {
preferredLanguage = tags[0]

View File

@@ -31,6 +31,7 @@ type WSImpl struct{}
// UpgraderWs http升级ws请求
func (s *WSImpl) UpgraderWs(w http.ResponseWriter, r *http.Request) *websocket.Conn {
wsUpgrader := websocket.Upgrader{
Subprotocols: []string{"omc-ws"},
// 设置消息发送缓冲区大小byte如果这个值设置得太小可能会导致服务端在发送大型消息时遇到问题
WriteBufferSize: 1024,
// 消息包启用压缩
@@ -61,7 +62,7 @@ func (s *WSImpl) NewClient(uid string, groupIDs []string, conn *websocket.Conn)
BindUid: uid,
SubGroup: groupIDs,
MsgChan: make(chan []byte, 100),
StopChan: make(chan struct{}, 1), // 请求卡死循环标记
StopChan: make(chan struct{}, 1), // 卡死循环标记
}
// 存入客户端
@@ -111,10 +112,12 @@ func (s *WSImpl) clientRead(wsClient *model.WSClient) {
s.CloseClient(wsClient.ID)
return
}
// 文本和二进制类型只处理文本json
if messageType == websocket.TextMessage {
var reqMsg model.WSRequest
err := json.Unmarshal(msg, &reqMsg)
// fmt.Println(messageType, string(msg))
if err != nil {
msgByte, _ := json.Marshal(result.ErrMsg("message format not supported"))
wsClient.MsgChan <- msgByte
@@ -177,11 +180,13 @@ func (s *WSImpl) CloseClient(clientID string) {
if clientIds, ok := WsUsers.Load(client.BindUid); ok {
uidClientIds := clientIds.(*[]string)
if len(*uidClientIds) > 0 {
for i, clientId := range *uidClientIds {
if clientId == client.ID {
*uidClientIds = append((*uidClientIds)[:i], (*uidClientIds)[i+1:]...)
tempClientIds := make([]string, 0, len(*uidClientIds))
for _, v := range *uidClientIds {
if v != client.ID {
tempClientIds = append(tempClientIds, v)
}
}
*uidClientIds = tempClientIds
}
}
}
@@ -196,11 +201,13 @@ func (s *WSImpl) CloseClient(clientID string) {
groupUIDs := uids.(*[]string)
if len(*groupUIDs) > 0 {
for i, v := range *groupUIDs {
if v == client.BindUid {
*groupUIDs = append((*groupUIDs)[:i], (*groupUIDs)[i+1:]...)
tempUIDs := make([]string, 0, len(*groupUIDs))
for _, v := range *groupUIDs {
if v != client.BindUid {
tempUIDs = append(tempUIDs, v)
}
}
*groupUIDs = tempUIDs
}
}
}

View File

@@ -18,7 +18,7 @@ func Setup(router *gin.Engine) {
{
wsGroup.GET("",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("WS 订阅", collectlogs.BUSINESS_TYPE_OTHER)),
collectlogs.OperateLog(collectlogs.OptionNew("WS Subscription", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewWSController.WS,
)