Esempio n. 1
0
        public async Task LoginAsync(string?username, string md5String, ClientVersionSubPacket clientVersion,
                                     ClientSession.ClientSession clientSession, string passwordToken, bool useApiAuth, RegionType language)
        {
            try
            {
                clientSession.SessionId = clientSession.Channel?.Id != null
                    ? SessionFactory.Instance.Sessions[clientSession.Channel.Id.AsLongText()].SessionId : 0;


                if (((_loginConfiguration.ClientVersion != null) &&
                     (clientVersion != _loginConfiguration.ClientVersion)) ||
                    ((_loginConfiguration.Md5String != null) && (md5String != _loginConfiguration.Md5String)))
                {
                    await clientSession.SendPacketAsync(new FailcPacket
                    {
                        Type = LoginFailType.OldClient
                    }).ConfigureAwait(false);

                    await clientSession.DisconnectAsync().ConfigureAwait(false);

                    return;
                }

                if (useApiAuth)
                {
                    username = await _authHttpClient.GetAwaitingConnectionAsync(null, passwordToken, clientSession.SessionId).ConfigureAwait(false);
                }

                var acc = await _accountDao.FirstOrDefaultAsync(s => s.Name.ToLower() == (username ?? "").ToLower()).ConfigureAwait(false);

                if ((acc != null) && (acc.Name != username))
                {
                    await clientSession.SendPacketAsync(new FailcPacket
                    {
                        Type = LoginFailType.WrongCaps
                    }).ConfigureAwait(false);

                    await clientSession.DisconnectAsync().ConfigureAwait(false);

                    return;
                }

                if ((acc == null) ||
                    (!useApiAuth && !string.Equals(acc.Password, passwordToken, StringComparison.OrdinalIgnoreCase)))
                {
                    await clientSession.SendPacketAsync(new FailcPacket
                    {
                        Type = LoginFailType.AccountOrPasswordWrong
                    }).ConfigureAwait(false);

                    await clientSession.DisconnectAsync().ConfigureAwait(false);

                    return;
                }

                switch (acc.Authority)
                {
                case AuthorityType.Banned:
                    await clientSession.SendPacketAsync(new FailcPacket
                    {
                        Type = LoginFailType.Banned
                    }).ConfigureAwait(false);

                    break;

                case AuthorityType.Closed:
                case AuthorityType.Unconfirmed:
                    await clientSession.SendPacketAsync(new FailcPacket
                    {
                        Type = LoginFailType.CantConnect
                    }).ConfigureAwait(false);

                    break;

                default:
                    var servers = (await _channelHttpClient.GetChannelsAsync().ConfigureAwait(false))
                                  ?.Where(c => c.Type == ServerType.WorldServer).ToList();
                    var alreadyConnnected = false;
                    var connectedAccount  = new Dictionary <int, List <ConnectedAccount> >();
                    var i = 1;
                    foreach (var server in servers ?? new List <ChannelInfo>())
                    {
                        var channelList = await _connectedAccountHttpClient.GetConnectedAccountAsync(
                            server).ConfigureAwait(false);

                        connectedAccount.Add(i, channelList);
                        i++;
                        if (channelList.Any(a => a.Name == acc.Name))
                        {
                            alreadyConnnected = true;
                        }
                    }

                    if (alreadyConnnected)
                    {
                        await clientSession.SendPacketAsync(new FailcPacket
                        {
                            Type = LoginFailType.AlreadyConnected
                        }).ConfigureAwait(false);

                        await clientSession.DisconnectAsync().ConfigureAwait(false);

                        return;
                    }

                    acc.Language = language;

                    acc = await _accountDao.TryInsertOrUpdateAsync(acc).ConfigureAwait(false);

                    if (servers == null || servers.Count <= 0)
                    {
                        await clientSession.SendPacketAsync(new FailcPacket
                        {
                            Type = LoginFailType.CantConnect
                        }).ConfigureAwait(false);

                        await clientSession.DisconnectAsync().ConfigureAwait(false);

                        return;
                    }

                    if (servers.Count(s => !s.IsMaintenance) == 0 && acc.Authority < AuthorityType.GameMaster)
                    {
                        await clientSession.SendPacketAsync(new FailcPacket
                        {
                            Type = LoginFailType.Maintenance
                        });

                        await clientSession.DisconnectAsync();

                        return;
                    }

                    var subpacket = new List <NsTeStSubPacket?>();
                    i = 1;
                    var servergroup = string.Empty;
                    var worldCount  = 1;
                    foreach (var server in servers.OrderBy(s => s.Name))
                    {
                        if (server.Name != servergroup)
                        {
                            i           = 1;
                            servergroup = server.Name ?? "";
                            worldCount++;
                        }

                        var channelcolor =
                            (int)Math.Round((double)connectedAccount[i].Count / server.ConnectedAccountLimit * 20)
                            + 1;
                        subpacket.Add(new NsTeStSubPacket
                        {
                            Host       = server.DisplayHost ?? server.Host,
                            Port       = server.DisplayPort ?? server.Port,
                            Color      = channelcolor,
                            WorldCount = worldCount,
                            WorldId    = i,
                            Name       = server.Name,
                        });
                        i++;
                    }

                    subpacket.Add(new NsTeStSubPacket
                    {
                        Host       = "-1",
                        Port       = null,
                        Color      = null,
                        WorldCount = 10000,
                        WorldId    = 10000,
                        Name       = useApiAuth ? "4" : "1"
                    });     //useless server to end the client reception
                    await clientSession.SendPacketAsync(new NsTestPacket
                    {
                        AccountName = username,
                        SubPacket   = subpacket,
                        SessionId   = clientSession.SessionId,
                        Unknown     = useApiAuth ? 2 : (int?)null,
                        RegionType  = language
                    }).ConfigureAwait(false);

                    return;
                }

                await clientSession.DisconnectAsync().ConfigureAwait(false);
            }
            catch
            {
                await clientSession.SendPacketAsync(new FailcPacket
                {
                    Type = LoginFailType.UnhandledError
                }).ConfigureAwait(false);

                await clientSession.DisconnectAsync().ConfigureAwait(false);
            }
        }
        public static async Task VerifyConnectionAsync(ClientSession clientSession, ILogger _logger, IAuthHttpClient authHttpClient,
                                                       IConnectedAccountHttpClient connectedAccountHttpClient, IDao <AccountDto, long> accountDao, IChannelHttpClient channelHttpClient, bool passwordLessConnection, string accountName, string password, int sessionId)
        {
            var alreadyConnnected = false;
            var servers           = await channelHttpClient.GetChannelsAsync().ConfigureAwait(false) ?? new List <ChannelInfo>();

            foreach (var channel in servers.Where(c => c.Type == ServerType.WorldServer))
            {
                var accounts = await connectedAccountHttpClient.GetConnectedAccountAsync(channel).ConfigureAwait(false);

                var target = accounts.FirstOrDefault(s => s.Name == accountName);

                if (target == null)
                {
                    continue;
                }

                alreadyConnnected = true;
                break;
            }

            if (alreadyConnnected)
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.ALREADY_CONNECTED), new
                {
                    accountName
                });
                await clientSession.DisconnectAsync().ConfigureAwait(false);

                return;
            }

            var account = await accountDao.FirstOrDefaultAsync(s => s.Name == accountName).ConfigureAwait(false);

            if (account == null)
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_ACCOUNT), new
                {
                    accountName
                });
                await clientSession.DisconnectAsync().ConfigureAwait(false);

                return;
            }

            var awaitingConnection =
                (passwordLessConnection
                    ? await authHttpClient
                 .GetAwaitingConnectionAsync(accountName, password, sessionId)
                 .ConfigureAwait(false) != null
                    : account.Password?.Equals(new Sha512Hasher().Hash(password), StringComparison.OrdinalIgnoreCase) ==
                 true);

            if (!awaitingConnection)
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_PASSWORD), new
                {
                    accountName
                });
                await clientSession.DisconnectAsync().ConfigureAwait(false);

                return;
            }

            var sessionMapping = SessionFactory.Instance.Sessions
                                 .FirstOrDefault(s => s.Value.SessionId == clientSession.SessionId);

            if (!sessionMapping.Equals(default(KeyValuePair <string, RegionTypeMapping>)))
            {
                sessionMapping.Value.RegionType = account.Language;
            }
            clientSession.InitializeAccount(account);
            //todo Send Account Connected
        }
Esempio n. 3
0
        public override async Task ExecuteAsync(EntryPointPacket packet, ClientSession clientSession)
        {
            if (clientSession == null)
            {
                throw new ArgumentNullException(nameof(clientSession));
            }

            if (clientSession.Account == null)
            {
                var alreadyConnnected = false;
                var name = packet.Name;
                foreach (var channel in (await _channelHttpClient.GetChannelsAsync().ConfigureAwait(false)).Where(c => c.Type == ServerType.WorldServer))
                {
                    var accounts = await _connectedAccountHttpClient.GetConnectedAccountAsync(channel).ConfigureAwait(false);

                    var target = accounts.FirstOrDefault(s => s.Name == name);

                    if (target == null)
                    {
                        continue;
                    }

                    alreadyConnnected = true;
                    break;
                }

                if (alreadyConnnected)
                {
                    await clientSession.DisconnectAsync().ConfigureAwait(false);

                    return;
                }

                var account = _accountDao.FirstOrDefault(s => s.Name == name);

                if (account != null)
                {
                    var result =
                        packet.Password != "thisisgfmode" ? null : await _authHttpClient.GetAwaitingConnectionAsync(name, packet.Password, clientSession.SessionId).ConfigureAwait(false);

                    if (result != null || (packet.Password != "thisisgfmode" && account.Password?.Equals(packet.Password.ToSha512(), StringComparison.OrdinalIgnoreCase) == true))
                    {
                        var accountobject = new AccountDto
                        {
                            AccountId = account.AccountId,
                            Name      = account.Name,
                            Password  = account.Password !.ToLower(),
                            Authority = account.Authority,
                            Language  = account.Language
                        };
                        SessionFactory.Instance.Sessions
                        .FirstOrDefault(s => s.Value.SessionId == clientSession.SessionId)
                        .Value.RegionType = account.Language;
                        clientSession.InitializeAccount(accountobject);
                        //Send Account Connected
                    }
                    else
                    {
                        _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_PASSWORD));
                        await clientSession.DisconnectAsync().ConfigureAwait(false);

                        return;
                    }
                }
                else
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_ACCOUNT));
                    await clientSession.DisconnectAsync().ConfigureAwait(false);

                    return;
                }

                _logger.Information(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.ACCOUNT_ARRIVED),
                                    clientSession.Account !.Name);
            }

            var characters = _characterDao.Where(s =>
                                                 (s.AccountId == clientSession.Account !.AccountId) && (s.State == CharacterState.Active));

            // load characterlist packet for each character in Character
            await clientSession.SendPacketAsync(new ClistStartPacket { Type = 0 }).ConfigureAwait(false);

            foreach (var character in characters.Select(characterDto => characterDto.Adapt <Character>()))
            {
                var equipment = new WearableInstance?[16];

                /* IEnumerable<ItemInstanceDTO> inventory = _iteminstanceDAO.Where(s => s.CharacterId == character.CharacterId && s.Type == (byte)InventoryType.Wear);
                 *
                 *
                 * foreach (ItemInstanceDTO equipmentEntry in inventory)
                 * {
                 *   // explicit load of iteminstance
                 *   WearableInstance currentInstance = equipmentEntry as WearableInstance;
                 *   equipment[(short)currentInstance.Item.EquipmentSlot] = currentInstance;
                 *
                 * }
                 */
                var petlist = new List <short?>();
                var mates   = _mateDao.Where(s => s.CharacterId == character.CharacterId)
                              .ToList();
                for (var i = 0; i < 26; i++)
                {
                    if (mates.Count > i)
                    {
                        petlist.Add(mates[i].Skin);
                        petlist.Add(mates[i].VNum);
                    }
                    else
                    {
                        petlist.Add(-1);
                    }
                }

                // 1 1 before long string of -1.-1 = act completion
                await clientSession.SendPacketAsync(new ClistPacket
                {
                    Slot       = character.Slot,
                    Name       = character.Name,
                    Unknown    = 0,
                    Gender     = character.Gender,
                    HairStyle  = character.HairStyle,
                    HairColor  = character.HairColor,
                    Unknown1   = 0,
                    Class      = character.Class,
                    Level      = character.Level,
                    HeroLevel  = character.HeroLevel,
                    Equipments = new List <short?>
                    {
                        equipment[(byte)EquipmentType.Hat]?.ItemVNum,
                        equipment[(byte)EquipmentType.Armor]?.ItemVNum,
                        equipment[(byte)EquipmentType.WeaponSkin]?.ItemVNum ??
                        equipment[(byte)EquipmentType.MainWeapon]?.ItemVNum,
                        equipment[(byte)EquipmentType.SecondaryWeapon]?.ItemVNum,
                        equipment[(byte)EquipmentType.Mask]?.ItemVNum,
                        equipment[(byte)EquipmentType.Fairy]?.ItemVNum,
                        equipment[(byte)EquipmentType.CostumeSuit]?.ItemVNum,
                        equipment[(byte)EquipmentType.CostumeHat]?.ItemVNum
                    },
                    JobLevel        = character.JobLevel,
                    QuestCompletion = 1,
                    QuestPart       = 1,
                    Pets            = petlist,
                    Design          = equipment[(byte)EquipmentType.Hat]?.Item?.IsColored ?? false
                        ? equipment[(byte)EquipmentType.Hat]?.Design ?? 0 : 0,
                    Rename = false
                }).ConfigureAwait(false);
            }

            await clientSession.SendPacketAsync(new ClistEndPacket()).ConfigureAwait(false);
        }