public static void Handel(SocketClient client, ForgotPasswordRequest request)
        {
            var reply = new LoginResponse();
            try
            {
                var user = AccountRepository.GetAccount(null, request.Email);
                if (user != null)
                {
                    if (user.Locked)
                        reply.ResponseType = LoginResponseType.AccountLocked;

                    else if (user.Verified)
                    {
                        user.Locked = true;
                        user.Verified = false;
                        user.Verificationcode = Guid.NewGuid().ToString();
                        reply.AccountId = user.Accountid;
                        reply.ResponseType = LoginResponseType.ResetSent;
                        EmailSender.SendRestEmail(user);
                        BaseRepository.Update(user);
                    }
                    else
                        reply.ResponseType = LoginResponseType.AccountNotVerified;
                }
                else
                    reply.ResponseType = LoginResponseType.ResetInvalid;

            }
            catch (Exception e)
            {
                reply.ResponseType = LoginResponseType.DatabaseError;
                Logger.Error(e.Message);
            }
            client.Send(reply);
        }
Example #2
0
        public static void Handle(SocketClient client, LoginResponse response)
        {
            if (response.ResponseType == LoginResponseType.AccountNotVerified)
                EmailSender.SendWelcomeEmail(AccountRepository.GetAccount(response.AccountId));

            if(response.ResponseType == LoginResponseType.AccountInUse)
                Program.OnlineAccounts[response.AccountId].Disconnect();
        }
        public static void Handle( SocketClient client,RegisterRequest request)
        {
            var registerInfo = request.RegisterInformation;
            var user = new Accounts
            {
                Username = registerInfo[0],
                Password = GetStringSha1Hash(registerInfo[1]),
                Email = registerInfo[2],
                Registerip = client.Handler.RemoteEndPoint.ToString(),
                Lastloginip = client.Handler.RemoteEndPoint.ToString(),
                Registertime = DateTime.Now,
                Lastlogintime = DateTime.Now,
                Locked = false,
                Verified = false,
                Verificationcode = Guid.NewGuid().ToString()
            };

            var reply = new LoginResponse();
            try
            {
                if (AccountRepository.GetAccount(user.Username, null) == null)
                {
                    if (AccountRepository.GetAccount(null, user.Email) == null)
                    {
                        BaseRepository.Add(user);
                        reply.ResponseType = LoginResponseType.AccountCreated;
                        reply.AccountId = user.Accountid;
                        EmailSender.SendWelcomeEmail(user);
                    }
                    else
                        reply.ResponseType = LoginResponseType.EmailInUse;
                }
                else
                    reply.ResponseType = LoginResponseType.UsernameInUse;
            }
            catch (Exception e)
            {
                reply.ResponseType = LoginResponseType.DatabaseError;
                Logger.Error(e.Message);
            }
            client.Send(reply);
        }
Example #4
0
        public static void Handel(SocketClient client, LoginRequest request, bool bypass)
        {
            var info = request.AccountInformation;
            var reply = new LoginResponse();
            var record = BlacklistIpsRepository.GetRecordByMac(client.MAddress) ??
                         new BlackListedIps {Attempts = 0, MacAddress = client.MAddress};
            var address = (IPEndPoint)client.Handler.RemoteEndPoint;
            record.Ip = address.Address.ToString();
            record.Attempts++;
            try
            {
                if (record.Attempts > 3 && !bypass)
                {
                    if (record.Attempts == 5)
                        record.BlacklistLiftTime = DateTime.Now.AddMinutes(15);
                    else if (record.Attempts == 8)
                        record.BlacklistLiftTime = DateTime.Now.AddMinutes(30);
                    else if (record.Attempts >= 11)
                        record.BlacklistLiftTime = DateTime.Now.AddMinutes(record.Attempts*10);
                    else
                    {
                        Handel(client, request, true);
                        return;
                    }
                    BaseRepository.Update(record);
                    reply.ResponseType = LoginResponseType.TooManyTries;
                    reply.AccountId = (int) record.BlacklistLiftTime.Subtract(DateTime.Now).TotalMinutes;
                }
                else
                {

                    BaseRepository.SaveOrUpdate(record);
                    var account = AccountRepository.GetAccount(info[0], null);
                    if (account != null)
                    {
                        if (account.Locked && !account.Verified)
                        {
                            var resetinfo = info[1].Split(':');
                            if (account.Verificationcode.Equals(resetinfo[0]))
                            {
                                account.Locked = false;
                                account.Verified = true;
                                account.Password = GetStringSha1Hash(resetinfo[1]);
                                reply.ResponseType = LoginResponseType.ResetVerified;
                                BaseRepository.Update(account);
                            }
                            else
                                reply.ResponseType = LoginResponseType.ResetLocked;
                        }
                        else if (!account.Locked)
                        {
                            if (account.Verified)
                            {
                                if (account.Password.Equals(GetStringSha1Hash(info[1])))
                                {
                                    if (Program.OnlineAccounts.ContainsKey(account.Accountid))
                                    {
                                        reply.ResponseType = LoginResponseType.AccountInUse;
                                        reply.AccountId = account.Accountid;
                                        client.Send(reply);
                                        return;
                                    }
                                    BaseRepository.Remove(record);
                                    reply.AccountId = account.Accountid;
                                    reply.ResponseType = LoginResponseType.Correct;
                                    account.Lastloginip = client.Handler.RemoteEndPoint.ToString();
                                    account.Lastlogintime = DateTime.Today;
                                    BaseRepository.Update(account);
                                    client.Account = account;
                                    client.Send(reply);
                                    HandleSuccessfulLogin(client);
                                    return;
                                }
                                reply.ResponseType = LoginResponseType.InvalidPassword;
                            }
                            else
                            {
                                if (account.Verificationcode.Equals(info[1]))
                                {
                                    account.Verified = true;
                                    reply.ResponseType = LoginResponseType.AccountVerified;
                                    BaseRepository.Update(account);
                                }
                                else
                                {
                                    reply.AccountId = account.Accountid;
                                    reply.ResponseType = LoginResponseType.AccountNotVerified;
                                }
                            }
                        }
                        else
                            reply.ResponseType = LoginResponseType.AccountLocked;
                    }
                    else
                        reply.ResponseType = LoginResponseType.InvalidPassword;
                }
            }
            catch (Exception e)
            {
                reply.ResponseType = LoginResponseType.DatabaseError;
                Logger.Error(e.Message);
            }
            client.Send(reply);
        }
Example #5
0
        public void ReadCallback(IAsyncResult ar)
        {
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            var client = (SocketClient) ar.AsyncState;
            Socket handler = client.Handler;
            // Read data from the client socket.
            try
            {
                int bytesRead = handler.EndReceive(ar);
                var bytesExpected = BitConverter.ToInt16(client.Buffer, 2);
                if (bytesRead > 0)
                {
                    if (bytesRead.Equals(bytesExpected))
                    {
                        if (client.MAddress == null && BitConverter.ToInt16(client.Buffer, 0) == 100)
                        {
                            client.PacketBuffer = new byte[bytesExpected];
                            Array.Copy(client.Buffer, client.PacketBuffer, bytesExpected);
                            Handler.HandlePacket(client);
                            handler.BeginReceive(client.Buffer, 0, SocketClient.BufferSize, 0,
                            ReadCallback, client);
                        }
                        else if (client.MAddress != null)
                        {
                            var record = BlacklistIpsRepository.GetRecordByMac(client.MAddress);
                            if (record == null || record.BlacklistLiftTime < DateTime.Now)
                            {
                                client.PacketBuffer = new byte[bytesExpected];
                                Array.Copy(client.Buffer, client.PacketBuffer, bytesExpected);
                                Handler.HandlePacket(client);
                                handler.BeginReceive(client.Buffer, 0, SocketClient.BufferSize, 0,
                                    ReadCallback, client);
                            }
                            else
                            {
                                var reply = new LoginResponse
                                {
                                    ResponseType = LoginResponseType.TooManyTries,
                                    AccountId = (int) record.BlacklistLiftTime.Subtract(DateTime.Now).TotalMinutes
                                };
                                client.Send(reply);
                                client.Handler.Shutdown(SocketShutdown.Both);
                                client.Handler.Close(50);
                            }
                        }
                        else
                        {
                            var reply = new LoginResponse { ResponseType = LoginResponseType.InvalidMac };
                            client.Send(reply);
                            client.Handler.Shutdown(SocketShutdown.Both);
                            client.Handler.Close(50);
                        }

                    }
                    else
                    {
                        //TODO: Fragmented packet.
                    }
                }
                else
                {
                    client.Disconnect();
                }
            }
            catch (SocketException e)
            {
                Logger.Error($"Server Error: {e.Message}");
                client.Handler.Shutdown(SocketShutdown.Both);
                client.Handler.Close();
            }
        }