Example #1
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            try
            {
                if (!(message is string buff))
                {
                    return;
                }

                string[] packet = buff.Split(' ');
                if (packet.Length <= 4 || packet[0] != "NoS0575")
                {
                    Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_REQUEST] Wrong packet");
                    SendPacket(GetFailPacket(AuthResponse.CantConnect));
                    Disconnect();
                    return;
                }

                string     accountName  = packet[2];
                string     passwordHash = packet[3];
                AccountDto dto          = AccountService.GetByName(accountName.ToLower());
                if (dto == null)
                {
                    Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_REQUEST] {accountName} Not found in database");
                    SendPacket(GetFailPacket(AuthResponse.AccountOrPasswordWrong));
                    Disconnect();
                    return;
                }

                if (!string.Equals(dto.Password, passwordHash, StringComparison.CurrentCultureIgnoreCase))
                {
                    Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_REQUEST] {accountName} Wrong password");
                    SendPacket(GetFailPacket(AuthResponse.AccountOrPasswordWrong));
                    Disconnect();
                    return;
                }

                var response = AuthResponse.Ok;

                if (response != AuthResponse.Ok)
                {
                    Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_REQUEST] MAINTENANCE MODE");
                    SendPacket(GetFailPacket(AuthResponse.AccountOrPasswordWrong));
                    Disconnect();
                    return;
                }

                PlayerSessionDto session = SessionService.GetByAccountName(accountName);
                if (session != null && session.State == PlayerSessionState.Connected)
                {
                    Log.Info($"[{_endPoint.Address}][CONNECT_REQUEST] {accountName} already connected on World {session.WorldServerId}");
                    SendPacket(GetFailPacket(AuthResponse.AlreadyConnected));
                    Disconnect();
                    return;
                }


                if (session == null)
                {
                    session = new PlayerSessionDto
                    {
                        Password = passwordHash,
                        Username = accountName,
                        State    = PlayerSessionState.Unauthed
                    };
                    SessionService.RegisterSession(session);
                    Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_ACCEPT] {accountName} waiting for world endpoint");
                }

                IEnumerable <WorldServerDto> test = ServerApi.GetServers();
                SendPacket(GenerateWorldListPacket(accountName, session.Id, test));

                Log.Info($"[{_endPoint.Address.MapToIPv4()}][CONNECT_ACCEPT] Server list sent to {accountName}");
                Disconnect();
            }
            catch (Exception e)
            {
                Log.Error("[PACKET_RECV]", e);
                _channel.WriteAndFlushAsync(GetFailPacket(AuthResponse.CantConnect)).Wait();
                _channel.DisconnectAsync().Wait();
            }
        }