Exemplo n.º 1
0
 private void OnClose(SockChatConnection conn)
 {
     Logger.Debug($@"[{conn}] Connection closed");
     Connections.RemoveConnection(conn);
     Context.Sessions.Destroy(conn);
     Context.RateLimiter.ClearConnection(conn);
 }
Exemplo n.º 2
0
        private void OnMessage(SockChatConnection conn, string msg)
        {
            bool hasSession = conn.Session != null;

            RateLimitState rateLimit = RateLimitState.None;

            if (!hasSession || !Context.RateLimiter.HasRankException(conn.Session.User))
            {
                rateLimit = Context.RateLimiter.BumpConnection(conn);
            }

            Logger.Debug($@"[{conn}] {rateLimit}");
            if (!hasSession && rateLimit == RateLimitState.Drop)
            {
                conn.Close();
                return;
            }

            IEnumerable <string> args = msg.Split(IServerPacket.SEPARATOR);

            if (!Enum.TryParse(args.ElementAtOrDefault(0), out ClientPacketId packetId))
            {
                return;
            }

            if (packetId != ClientPacketId.Authenticate)
            {
                if (!hasSession)
                {
                    return;
                }

                if (rateLimit == RateLimitState.Drop)
                {
                    Context.BanUser(conn.Session.User, Context.RateLimiter.BanDuration, UserDisconnectReason.Flood);
                    return;
                } /*else if(rateLimit == RateLimitState.Warn)
                   * sess.SendPacket(new FloodWarningPacket(Context.Bot));*/
            }

            if (PacketHandlers.TryGetValue(packetId, out IPacketHandler handler))
            {
                handler.HandlePacket(new PacketHandlerContext(args, conn));
            }
        }
Exemplo n.º 3
0
        public void Listen(EndPoint endPoint)
        {
            if (Server != null)
            {
                throw new ProtocolAlreadyListeningException();
            }
            if (endPoint == null)
            {
                throw new ArgumentNullException(nameof(endPoint));
            }
            if (endPoint is not IPEndPoint ipEndPoint)
            {
                throw new ArgumentException(@"EndPoint must be an IPEndPoint", nameof(endPoint));
            }

            Server = new FleckWebSocketServer(ipEndPoint, false);
            Server.Start(rawConn => {
                SockChatConnection conn = new SockChatConnection(rawConn);
                rawConn.OnOpen         += () => OnOpen(conn);
                rawConn.OnClose        += () => OnClose(conn);
                rawConn.OnError        += ex => OnError(conn, ex);
                rawConn.OnMessage      += msg => OnMessage(conn, msg);
            });
        }
Exemplo n.º 4
0
 private static void OnError(SockChatConnection conn, Exception ex)
 {
     Logger.Write($@"[{conn}] {ex}");
 }
Exemplo n.º 5
0
 private void OnOpen(SockChatConnection conn)
 {
     Logger.Debug($@"[{conn}] Connection opened");
     Connections.AddConnection(conn);
 }