using System; using System.Net; using System.Text; using System.Net.Sockets; namespace Launcher_Server.Networking { class Client { private Socket ClientSocket; private IPEndPoint ClientEndPoint; private ClientInformation Information; private Boolean SentInformation = false; private Byte[] ClientBuffer; public Client(Socket ClientHandle) { ClientSocket = ClientHandle; ClientEndPoint = (IPEndPoint)ClientSocket.RemoteEndPoint; Program.Writer.WriteBlank(true, "({0}) Has Connected!", ClientEndPoint.Address); Send(String.Format("1|{0}", Properties.Settings.Default.Launcher_Version)); Receive(); } private void Receive() { try { ClientBuffer = new Byte[Properties.Settings.Default.Byte_Size]; ClientSocket.BeginReceive(ClientBuffer, 0, ClientBuffer.Length, SocketFlags.None, new AsyncCallback(Receive), ClientSocket); } catch { } } private void Receive(IAsyncResult Result) { try { Int32 PacketLength = ClientSocket.EndReceive(Result); String[] MessageReceived = Encoding.Unicode.GetString(ClientBuffer).Split('|'); Int32 Type = Convert.ToInt32(MessageReceived[0]); if (PacketLength != 0) { switch (Type) { case 1: { SentInformation = true; Information = new ClientInformation() { IPAddress = Convert.ToString(ClientEndPoint.Address), MacAddress = MessageReceived[1], UniqueID = MessageReceived[2] }; if (Information.MacAddress.Length != 0) { Program.Writer.WriteBlank(true, "({0}) Sent Invalid Mac Address Length -> {1}!", ClientEndPoint.Address, Information.MacAddress.Length); Close(); return; } if (Information.UniqueID.Length != 19) { Program.Writer.WriteBlank(true, "({0}) Sent Invalid UniqueID Length -> {1}!", ClientEndPoint.Address, Information.UniqueID.Length); Close(); return; } break; } case 2: { String Username = MessageReceived[1]; String Password = MessageReceived[2]; if (!SentInformation) { Program.Writer.WriteBlank(true, "({0}) Sent Invalid Packet!", ClientEndPoint.Address); Close(); return; } if (Username.Length > 50 || Password.Length > 50) { Program.Query.AddLoginLog(Username, Information, "Incorrect Length!", false); Program.Writer.WriteBlank(true, "({0}) Sent Invalid Lengths!", ClientEndPoint.Address); Send("2|Username / Password Length Incorrect", Username); Receive(); return; } else { if (!Program.Query.UsernameExist(Username)) { Program.Query.AddLoginLog(Username, Information, "Username Incorrect!", false); Program.Writer.WriteBlank(true, "({0}) Entered Incorrect Username!", ClientEndPoint.Address); Send("2|No Account Found With Username -> {0}", Username); Receive(); return; } else if (!Program.Query.CheckPassword(Username, Password)) { Program.Query.AddLoginLog(Username, Information, "Password Incorrect", false); Program.Writer.WriteBlank(true, "({0}) Entered Incorrect Password!", ClientEndPoint.Address); Send("2|Incorrect Password Has Been Entered!"); Receive(); return; } else if (!Program.Query.Maintenance(Username)) { Program.Query.AddLoginLog(Username, Information, "Server Maintenance", true); Program.Writer.WriteBlank(true, "({0}) Tryed To Login But Server Maintenance In Progress!", ClientEndPoint.Address); Send("2|Server Currently Down For Maintenance!"); Receive(); return; } else if (Program.Query.IsBanned(Username, Information)) { Program.Query.AddLoginLog(Username, Information, "Banned From Game", false); Program.Writer.WriteBlank(true, "({0}) Tryed To Login But They Are Banned!", ClientEndPoint.Address); Send("2|You Are Banned From Playing This Server!"); Receive(); return; } else { String Token = Program.Query.CheckToken(Program.CreateRandom(50)); if (Token == "Error!" || !Program.Query.InsertToken(Username, Token)) { Program.Query.AddLoginLog(Username, Information, "Token Insert Failed", false); Program.Writer.WriteBlank(true, "({0}) Tryed To Login But Query Error!", ClientEndPoint.Address); Send("2|There Was An Error Please Try Again!"); Receive(); return; } Program.Query.AddLoginLog(Username, Information, String.Format("Successful {0}", Token), true); Program.Writer.WriteBlank(true, "({0}) Logged Into The Game -> {1}!", ClientEndPoint.Address, Token); Send("3|{0}", Token); Close(); return; } } } default: { Program.Writer.WriteBlank(false, "({0}) Sent Invalid Packet!"); Close(); return; } } } else { Program.Writer.WriteBlank(false, "({0}) Has Disconnected!"); Close(); } } catch (Exception Error) { Program.Writer.WriteBlank(false, "({0}) Has Disconnected With An Error!"); Program.Writer.WriteLine(WriteType.Error, true, Error.Message); Close(); } } private void Send(String Message, params Object[] Arguments) { try { String FormattedMessage = String.Format(Message, Arguments); Byte[] Buffer = Encoding.Unicode.GetBytes(FormattedMessage); ClientSocket.Send(Buffer); } catch { } } private void Close() { try { ClientSocket.Disconnect(false); ClientSocket.Close(); if (ClientSocket != null) { ClientSocket.Dispose(); } } catch { } } } }