private void HandleAuthentication(string username, string password) { var result = Authentication(username, password); if (result != AuthenticationResult.SUCCESS) { LoginPacketFactory.AuthenticationFailed(_client, result); return; } DbUser dbUser = _database.Users.First(x => x.Username.Equals(username, StringComparison.OrdinalIgnoreCase)); if (_server.IsClientConnected(dbUser.Id)) { _client.Disconnect(); return; } dbUser.LastConnectionTime = DateTime.UtcNow; _database.Users.Update(dbUser); _database.SaveChanges(); _client.SetClientUserID(dbUser.Id); LoginPacketFactory.AuthenticationSuccess(_client, result, dbUser); }
public static void OnSelectServer(LoginClient client, IPacketStream packet) { if (packet.Length != 9) { return; } var selectServerPacket = new SelectServerPacket(packet); var server = DependencyContainer.Instance.Resolve <ILoginServer>(); var worldInfo = server.GetWorldByID(selectServerPacket.WorldId); if (worldInfo == null) { LoginPacketFactory.SelectServerFailed(client, SelectServer.CannotConnect); return; } // For some reason, the current game.exe has version -1. Maybe this is somehow connected with decrypted packages? // In any case, for now client version check is disabled. if (worldInfo.BuildVersion != selectServerPacket.BuildClient && selectServerPacket.BuildClient != -1) { LoginPacketFactory.SelectServerFailed(client, SelectServer.VersionDoesntMatch); return; } if (worldInfo.ConnectedUsers >= worldInfo.MaxAllowedUsers) { LoginPacketFactory.SelectServerFailed(client, SelectServer.ServerSaturated); return; } LoginPacketFactory.SelectServerSuccess(client, worldInfo.Host); }
private void HandleSelectServer(SelectServerPacket selectServerPacket) { var worldInfo = _server.GetWorldByID(selectServerPacket.WorldId); if (worldInfo == null) { LoginPacketFactory.SelectServerFailed(_client, SelectServer.CannotConnect); return; } // For some reason, the current game.exe has version -1. Maybe this is somehow connected with decrypted packages? // In any case, for now client version check is disabled. if (false && worldInfo.BuildVersion != selectServerPacket.BuildClient && selectServerPacket.BuildClient != -1) { LoginPacketFactory.SelectServerFailed(_client, SelectServer.VersionDoesntMatch); return; } if (worldInfo.ConnectedUsers >= worldInfo.MaxAllowedUsers) { LoginPacketFactory.SelectServerFailed(_client, SelectServer.ServerSaturated); return; } LoginPacketFactory.SelectServerSuccess(_client, worldInfo.Host); }
public static void OnLoginRequest(LoginClient client, IPacketStream packet) { if (packet.Length != 52) { return; } var authenticationPacket = new AuthenticationPacket(packet); var result = Authentication(authenticationPacket.Username, authenticationPacket.Password); if (result != AuthenticationResult.SUCCESS) { LoginPacketFactory.AuthenticationFailed(client, result); return; } var loginServer = DependencyContainer.Instance.Resolve <ILoginServer>(); using var database = DependencyContainer.Instance.Resolve <IDatabase>(); DbUser dbUser = database.Users.Get(x => x.Username.Equals(authenticationPacket.Username, StringComparison.OrdinalIgnoreCase)); if (loginServer.IsClientConnected(dbUser.Id)) { client.Disconnect(); return; } dbUser.LastConnectionTime = DateTime.Now; database.Users.Update(dbUser); database.Complete(); client.SetClientUserID(dbUser.Id); LoginPacketFactory.AuthenticationFailed(client, result, dbUser); }
public static void OnSelectServer(LoginClient client, IPacketStream packet) { if (packet.Length != 9) { return; } var selectServerPacket = new SelectServerPacket(packet); var server = DependencyContainer.Instance.Resolve <ILoginServer>(); var worldInfo = server.GetWorldByID(selectServerPacket.WorldId); if (worldInfo == null) { LoginPacketFactory.SelectServerFailed(client, SelectServer.CannotConnect); return; } if (worldInfo.BuildVersion != selectServerPacket.BuildClient) { LoginPacketFactory.SelectServerFailed(client, SelectServer.VersionDoesntMatch); return; } if (worldInfo.ConnectedUsers >= worldInfo.MaxAllowedUsers) { LoginPacketFactory.SelectServerFailed(client, SelectServer.ServerSaturated); return; } LoginPacketFactory.SelectServerSuccess(client, worldInfo.Host); }
private void HandleAuthentication(AuthenticationPacket authenticationPacket) { var result = Authentication(authenticationPacket.Username, authenticationPacket.Password); if (result != AuthenticationResult.SUCCESS) { LoginPacketFactory.AuthenticationFailed(_client, result); return; } var loginServer = DependencyContainer.Instance.Resolve <ILoginServer>(); using var database = DependencyContainer.Instance.Resolve <IDatabase>(); DbUser dbUser = database.Users.First(x => x.Username.Equals(authenticationPacket.Username, StringComparison.OrdinalIgnoreCase)); if (loginServer.IsClientConnected(dbUser.Id)) { _client.Disconnect(); return; } dbUser.LastConnectionTime = DateTime.Now; database.Users.Update(dbUser); database.SaveChanges(); _client.SetClientUserID(dbUser.Id); LoginPacketFactory.AuthenticationSuccess(_client, result, dbUser); }
/// <inheritdoc /> protected override void OnClientConnected(LoginClient client) { base.OnClientConnected(client); LoginPacketFactory.SendLoginHandshake(client); LoginManagers.Add(client.Id, _loginManagerFactory.CreateLoginManager(client, this)); client.OnPacketArrived += Client_OnPacketArrived; }
private static void AuthenticationFailed(LoginClient client, ErrorType error, string reason, bool disconnectClient = true) { Logger.LogWarning($"Unable to authenticate user from {client.RemoteEndPoint}. Reason: {reason}"); LoginPacketFactory.SendLoginError(client, error); if (disconnectClient) { client.Disconnect(); } }
/// <inheritdoc /> protected override void OnClientConnected(LoginClient client) { LoginPacketFactory.SendLoginHandshake(client); }
public static void OnLogin(LoginClient client, INetPacketStream packet) { var loginServer = DependencyContainer.Instance.Resolve <ILoginServer>(); var configuration = DependencyContainer.Instance.Resolve <LoginConfiguration>(); var certifyPacket = new CertifyPacket(packet, configuration.PasswordEncryption); string password = null; if (certifyPacket.BuildVersion != configuration.BuildVersion) { AuthenticationFailed(client, ErrorType.CERT_GENERAL, "bad client build version"); return; } if (configuration.PasswordEncryption) { byte[] encryptionKey = Aes.BuildEncryptionKeyFromString(configuration.EncryptionKey, 16); password = Aes.DecryptByteArray(certifyPacket.EncryptedPassword, encryptionKey); } else { password = packet.Read <string>(); } var authenticationService = DependencyContainer.Instance.Resolve <IAuthenticationService>(); var authenticationResult = authenticationService.Authenticate(certifyPacket.Username, password); switch (authenticationResult) { case AuthenticationResult.BadUsername: AuthenticationFailed(client, ErrorType.FLYFF_ACCOUNT, "bad username"); break; case AuthenticationResult.BadPassword: AuthenticationFailed(client, ErrorType.FLYFF_PASSWORD, "bad password"); break; case AuthenticationResult.AccountSuspended: // TODO break; case AuthenticationResult.AccountTemporarySuspended: // TODO break; case AuthenticationResult.AccountDeleted: AuthenticationFailed(client, ErrorType.ILLEGAL_ACCESS, "logged in with deleted account"); break; case AuthenticationResult.Success: if (loginServer.IsClientConnected(certifyPacket.Username)) { AuthenticationFailed(client, ErrorType.DUPLICATE_ACCOUNT, "client already connected", disconnectClient: false); return; } using (var database = DependencyContainer.Instance.Resolve <IDatabase>()) { var user = database.Users.Get(x => x.Username.Equals(certifyPacket.Username, StringComparison.OrdinalIgnoreCase)); if (user == null) { AuthenticationFailed(client, ErrorType.ILLEGAL_ACCESS, "Cannot find user in database"); Logger.LogCritical($"Cannot find user '{certifyPacket.Username}' in database to update last connection time."); return; } user.LastConnectionTime = DateTime.UtcNow; database.Users.Update(user); database.Complete(); } LoginPacketFactory.SendServerList(client, certifyPacket.Username, loginServer.GetConnectedClusters()); client.SetClientUsername(certifyPacket.Username); Logger.LogInformation($"User '{client.Username}' logged succesfully from {client.RemoteEndPoint}."); break; default: break; } }