zirconium-go/core/router.go

58 lines
1.7 KiB
Go
Raw Normal View History

package core
import (
"github.com/cadmium-im/zirconium-go/core/models"
2021-04-06 13:55:41 +00:00
"github.com/cadmium-im/zirconium-go/core/utils"
"github.com/fatih/structs"
"github.com/google/logger"
)
type Router struct {
2021-04-06 13:55:41 +00:00
appContext *AppContext
handlers map[string][]C2SMessageHandler
}
type C2SMessageHandler interface {
HandleMessage(s *Session, message models.BaseMessage)
IsAuthorizationRequired() bool
HandlingType() string
}
func NewRouter(ctx *AppContext) (*Router, error) {
r := &Router{
appContext: ctx,
handlers: map[string][]C2SMessageHandler{},
}
return r, nil
}
func (r *Router) RouteMessage(origin *Session, message models.BaseMessage) {
handlers := r.handlers[message.MessageType]
if handlers != nil {
for _, v := range handlers {
if v.IsAuthorizationRequired() {
2021-04-06 13:55:41 +00:00
if origin.Claims == nil {
logger.Warningf("Connection %s isn't authorized", origin.connID)
2021-04-06 13:55:41 +00:00
msg := utils.PrepareMessageUnauthorized(message, r.appContext.cfg.ServerDomains[0]) // fixme: domain
_ = origin.Send(msg)
}
}
go v.HandleMessage(origin, message)
}
} else {
protocolError := models.ProtocolError{
ErrCode: "unhandled",
ErrText: "Server doesn't implement message type " + message.MessageType,
ErrPayload: make(map[string]interface{}),
}
2021-04-06 13:55:41 +00:00
errMsg := models.NewBaseMessage(message.ID, message.MessageType, r.appContext.cfg.ServerID, []string{message.From}, false, structs.Map(protocolError))
logger.Infof("Drop message with type %s because server hasn't proper handlers", message.MessageType)
_ = origin.Send(errMsg)
}
}
func (r *Router) RegisterC2SHandler(c C2SMessageHandler) {
r.handlers[c.HandlingType()] = append(r.handlers[c.HandlingType()], c)
}