Beispiel #1
0
 protected override void OnMessage(MessageEventArgs e)
 {
     try
     {
         if (e.IsText)
         {
             var msg = JsonConvert.DeserializeObject <BaseMessage>(e.Data);
             _app.Router.RouteMessage(_app.SessionManager.GetSession(ID), msg);
         }
         else
         {
             var errMsg = OtherUtils.GenerateProtocolError(
                 null,
                 "parseError",
                 $"Server cannot parse this message because it is not JSON",
                 new Dictionary <string, object>()
                 );
             errMsg.From = _app.Config.ServerID;
             var msgStr = JsonConvert.SerializeObject(errMsg);
             this.SendMessage(msgStr);
         }
     }
     catch (Exception ex)
     {
         var errMsg = OtherUtils.GenerateProtocolError(
             null,
             "parseError",
             $"Server cannot parse this message! {ex.Message}",
             new Dictionary <string, object>()
             );
         errMsg.From = _app.Config.ServerID;
         var msgStr = JsonConvert.SerializeObject(errMsg);
         this.SendMessage(msgStr);
     }
 }
        public void HandleMessage(Session session, BaseMessage message)
        {
            var pObj         = message.Payload.ToObject <RegisterRequestPayload>();
            var authProvider = _pluginHost.GetAuthProvider();

            try
            {
                authProvider.CreateUser(pObj.Username, pObj.Password);
            }
            catch (Exception e)
            {
                BaseMessage errorReply;
                if (e.Message.Contains("E11000"))
                {
                    errorReply = OtherUtils.GenerateProtocolError(
                        message,
                        "id_exists",
                        "Username already taken",
                        new Dictionary <string, object>()
                        );
                }
                else
                {
                    errorReply = OtherUtils.GenerateProtocolError(
                        message,
                        "other",
                        e.ToString(),
                        new Dictionary <string, object>()
                        );
                }

                session.ConnectionHandler.SendMessage(errorReply);
                return;
            }

            BaseMessage reply = new BaseMessage(message, true);
            var         p     = new RegisterResponsePayload();

            p.UserID = $"@{pObj.Username}@{_pluginHost.GetServerID()}";

            reply.Payload = p.ToDictionary();
            reply.Ok      = true;
            session.ConnectionHandler.SendMessage(reply);
        }
Beispiel #3
0
        public void RouteMessage(Session session, BaseMessage message)
        {
            var handlers = _c2sMessageHandlers.GetValueOrDefault(message.MessageType, null);

            if (handlers == null)
            {
                Log.Warning($"Drop message with type \"{message.MessageType}\" because server hasn't proper handlers");
                var msg = OtherUtils.GenerateProtocolError(
                    message,
                    "unhandled",
                    $"Server doesn't implement message type \"{message.MessageType}\"",
                    new Dictionary <string, object>()
                    );
                msg.From = _app.Config.ServerID;
                session.ConnectionHandler.SendMessage(msg);
                return;
            }
            var handlerTasks = new List <Task>();

            foreach (var h in handlers)
            {
                if (h.IsAuthorizationRequired() && session.AuthData == null)
                {
                    session.ConnectionHandler.SendMessage(OtherUtils.GenerateUnauthorizedError(message, _app.Config.ServerID));
                    return;
                }

                handlerTasks.Add(Task.Run(() =>
                {
                    // probably need to wrap whole foreach body, not only HandleMessage call - need to investigate
                    h.HandleMessage(session, message);
                }));
            }
            try
            {
                Task.WaitAll(handlerTasks.ToArray());
            }
            catch (Exception e)
            {
                Log.Error(e.ToString());
            }
        }
Beispiel #4
0
        public void HandleMessage(Session session, BaseMessage message)
        {
            var pObj         = message.Payload.ToObject <AuthorizationRequest>();
            var authProvider = _pluginHost.GetAuthProvider();

            if (!authProvider.GetAuthSupportedMethods().Contains(pObj.Type))
            {
                var reply = OtherUtils.GenerateProtocolError(
                    message,
                    invalidAuthType,
                    "auth type is invalid"
                    );
                session.ConnectionHandler.SendMessage(reply);
                return;
            }
            var authData = authProvider.TestAuthFields(pObj.Fields);

            if (authData.Item1 != null)
            {
                BaseMessage reply = new BaseMessage(message, true);
                if (authData.Item2 != null)
                {
                    reply.Payload = authData.Item2.ToDictionary();
                }
                reply.Ok = true;
                session.ConnectionHandler.SendMessage(reply);
                session.AuthData = authData.Item1;
            }
            else
            {
                var reply = OtherUtils.GenerateProtocolError(
                    message,
                    errID,
                    "auth credentials isn't valid"
                    );
                session.ConnectionHandler.SendMessage(reply);
            }
        }