private static int UnkDataHandler(SessionTcpClient client, byte[] data, int Length) { Logger.Info("Unk Server Data Handler"); //Logger.Log(Utility.ByteArrayToString(data)); return(1); }
private static int ViewConnectHandler(SessionTcpClient client) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; Logger.Info("View Server Connection : {0} : {1}", new object[] { addr.ToString(), port }); return(1); }
private static int LobbyDataHandler(SessionTcpClient client, byte[] data, int Length) { Logger.Info("Lobby Server Data Handler"); //Logger.Log(Utility.ByteArrayToString(data)); int result = Length; bool bIsNewChar = false; ByteRef recvBuf = new ByteRef(data, Length); byte temp = recvBuf.GetByte(0x04); recvBuf.Fill(0, 0x00, 32); switch (_packetCount) { case 0: recvBuf.Set <byte>(0, 0x81); var t = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; recvBuf.Set <uint>(0x14, t); result = 24; break; case 1: if (temp != 0x28) { bIsNewChar = true; } recvBuf.Set <byte>(0x00, 0x28); recvBuf.Set <byte>(0x04, 0x20); recvBuf.Set <byte>(0x08, 0x01); recvBuf.Set <byte>(0x0B, 0x7F); result = bIsNewChar ? 144 : 24; if (bIsNewChar) { bIsNewChar = false; } break; } client.Session.LobbySend(recvBuf.Get(), result); _packetCount++; if (_packetCount == 3) { client.Client.Disconnect(true); } /* Echo back the buffer to the server.. */ //if (send(client, (char*)recvBuffer, result, 0) == SOCKET_ERROR) //{ // xiloader::console::output(xiloader::color::error, "Client send failed: %d", WSAGetLastError()); // break; //} return(1); }
private static int UnkConnectHandler(SessionTcpClient client) { Logger.Info("Unk Server Connect Handler"); var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; Console.WriteLine("STATUS CONNECT CLIENT INFO: {0} {1}", addr, port); return(1); }
private static int ViewDisconnectHandler(SessionTcpClient client) { if (!client.IsDead) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; Logger.Info("View Server Disconnection : {0} : {1}", new object[] { addr.ToString(), port }); } SessionHandler.CheckSessionHealth(client.Session); return(1); }
private static int DatabaseDisconnectHandler(SessionTcpClient client) { if (!client.IsDead) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; client.Client.Disconnect(true); Logger.Info("Database Server Disconnection : {0} : {1}", new object[] { addr.ToString(), port }); } return(1); }
private static void SendLobbyError(SessionTcpClient client) { MD5 md5Hash = MD5.Create(); byte[] hash; ByteRef ReserveData = new ByteRef(ViewServer.lobbyErrorData); byte SendBuffSize = ReserveData.GetByte(0); ReserveData.Set <ushort>(32, 321); hash = md5Hash.ComputeHash(ReserveData.Get(), 0, SendBuffSize); ReserveData.BlockCopy(hash, 12, 16); client.Session.ViewSend(ReserveData.Get(), SendBuffSize); SessionHandler.KillSession(client.Session); }
private static int LobbyConnectHandler(SessionTcpClient client) { Logger.Info("Lobby Server Connect Handler"); var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; LoginSession session = SessionHandler.GetSessionByIP(addr.ToString(), SESSIONSTATUS.SYNCHRONIZING); session.Lobby_client = client; client.Session = session; Console.WriteLine("STATUS CONNECT CLIENT INFO: {0} {1}", addr, port); return(1); }
private static int DataConnectHandler(SessionTcpClient client) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; Logger.Info("Data Server Connection : {0} : {1}", new object[] { addr.ToString(), port }); LoginSession session = SessionHandler.GetSessionByIP(addr.ToString(), SESSIONSTATUS.ACCEPTINGTERMS); if (session != null) { session.Data_client = client; session.Status = SESSIONSTATUS.MAINMENU; client.Session = session; } return(1); }
private static int AuthConnectHandler(SessionTcpClient client) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; var port = ((IPEndPoint)client.Client.RemoteEndPoint).Port; Logger.Info("Auth Server Connection : {0} : {1}", new object[] { addr.ToString(), port }); LoginSession session = new LoginSession(); session.Entry_time = DateTime.Now; session.Ip_address = addr.ToString(); session.Status = SESSIONSTATUS.LOGGINGIN; client.Session = session; session.Auth_client = client; SessionHandler.AddSession(session); return(1); }
private static int LobbyDisconnectHandler(SessionTcpClient client) { Logger.Info("Lobby Server Disconnect Handler"); return(1); }
private static int ViewDataHandler(SessionTcpClient client, byte[] data, int Length) { var addr = ((IPEndPoint)client.Client.RemoteEndPoint).Address; ByteRef recv = new ByteRef(data, Length); byte id = recv.GetByte(8); int sendSize = 0x28; ByteRef reserveData = new ByteRef(sendSize); //Logger.Log("View Server Data Handler"); //Logger.Log(Utility.ByteArrayToString(data)); //Logger.Log("VIEW ID {0:X}", id); switch (id) { case 0x26: ByteRef SessionHash = new ByteRef(16); SessionHash.BlockCopy(recv.At(12), 0, 16); LoginSession session = SessionHandler.GetSessionByHash(addr.ToString(), SessionHash.Get()); if (session != null) { client.Session = session; session.View_client = client; session.Status = SESSIONSTATUS.CHARSELECT; string clientVersionStr = recv.GetString(0x74, 6); string expectedVersionStr = "301812"; //ConfigHandler.VersionConfig.ClientVersion.Substring(0, 6); uint clientVersion = Convert.ToUInt32(clientVersionStr); uint expectedVersion = Convert.ToUInt32(expectedVersionStr); bool versionMismatch = clientVersion != expectedVersion; bool fatalMismatch = false; if (versionMismatch) { switch (ConfigHandler.VersionConfig.VersionLock) { case 1: if (expectedVersion < clientVersion) { fatalMismatch = true; } break; case 2: if (expectedVersion > clientVersion) { fatalMismatch = true; } break; } } if (fatalMismatch) { sendSize = 0x24; reserveData.Reset(0x24); reserveData.BlockCopy(lobbyErrorData, 0, sendSize); reserveData.Set <ushort>(0x20, 331); } else { // TODO: get expansions and features from database AccountEF aef = new AccountEF() { Expansions = 14, Features = 13 }; // MySQL.GetAccountEF(client.Session.Account_id); ByteRef response = new ByteRef(new byte[] { 0x28, 0x00, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0xe0, 0x5d, 0xad, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); response.Set <ushort>(32, aef.Expansions); // expansion bitmask response.Set <ushort>(36, aef.Features); // feature bitmask reserveData.BlockCopy(response.Get(), 0, sendSize); } MD5 md5Hash = MD5.Create(); byte[] hash = md5Hash.ComputeHash(reserveData.Get(), 0, sendSize); reserveData.BlockCopy(hash, 12, 16); client.Session.ViewSend(reserveData.Get()); } else { sendSize = 0x24; reserveData.Reset(0x24); reserveData.BlockCopy(lobbyErrorData, 0, sendSize); reserveData.Set <ushort>(0x20, 314); MD5 md5Hash = MD5.Create(); byte[] hash = md5Hash.ComputeHash(reserveData.Get(), 0, sendSize); reserveData.BlockCopy(hash, 12, 16); client.Client.Send(reserveData.Get()); } break; case 0x1F: reserveData.Reset(5); reserveData.Set <byte>(0, 0x01); client.Session.DataSend(reserveData.Get()); break; case 0x24: byte[] Data24 = new byte[] { 0x40, 0x00, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x70, 0x58, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; ByteRef ReservePacket24 = new ByteRef(Data24); // TODO: bring in from configuration ReservePacket24.Set <string>(36, "Onyx"); MD5 md5Hash24 = MD5.Create(); byte[] hash24 = md5Hash24.ComputeHash(ReservePacket24.Get(), 0, 64); ReservePacket24.BlockCopy(hash24, 12, 16); client.Session.ViewSend(ReservePacket24.Get(), 64); break; case 0x07: reserveData.Reset(5); reserveData.Set <byte>(0, 0x02); if (recv.Length > 30) { client.Session.Char_id = recv.GetUInt32(28); } else { SessionHandler.KillSession(client.Session); return(-1); } client.Session.DataSend(reserveData.Get()); break; case 0x14: // Delete Character byte[] Data14 = new byte[] { 0x20, 0x00, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; uint charid = recv.GetUInt32(0x20); if (charid > 0 && client.Session.Char_id_list.Contains(charid)) { ByteRef ReservePacket14 = new ByteRef(Data14); MD5 md5Hash14 = MD5.Create(); byte[] hash14 = md5Hash14.ComputeHash(ReservePacket14.Get(), 0, 0x20); ReservePacket14.BlockCopy(hash14, 12, 16); client.Session.ViewSend(ReservePacket14.Get()); // TODO: add character deletion //MySQL.DeleteChar(client.Session.Account_id, charid); } else { SessionHandler.KillSession(client.Session); return(-1); } break; case 0x21: // Create a Character - Save if (!CreateCharacter(client.Session.Account_id, client.Session.Char_name, recv)) { SessionHandler.KillSession(client.Session); return(-1); } ByteRef ReservePacket21 = new ByteRef(0x20); ReservePacket21.BlockCopy(lobbyActionDone, 0, 0x20); MD5 md5Hash21 = MD5.Create(); byte[] hash21 = md5Hash21.ComputeHash(ReservePacket21.Get(), 0, 0x20); ReservePacket21.BlockCopy(hash21, 12, 16); client.Session.ViewSend(ReservePacket21.Get()); client.Session.Status = SESSIONSTATUS.INGAME; break; case 0x22: // Create a Character - Validate sendSize = 0x24; ByteRef ReservePacket22 = new ByteRef(sendSize); if (ConfigHandler.MaintConfig.MaintMode > 0) { ReservePacket22.BlockCopy(lobbyErrorData, 0, 0x24); ReservePacket22.Set <ushort>(32, 314); } else { ByteRef NameBuf = new ByteRef(15); NameBuf.BlockCopy(recv.At(32), 0, 15); string CharName = Utility.ReadCString(NameBuf.Get()); Player player = DBClient.GetOne <Player>(DBREQUESTTYPE.PLAYER, p => p.Name.Equals(CharName)); if (!Regex.IsMatch(CharName, @"^[a-zA-Z]+$") || SessionHandler.CharNameExists(CharName) || player != null) { ReservePacket22.BlockCopy(lobbyErrorData, 0, sendSize); ReservePacket22.Set <ushort>(32, 313); } else { client.Session.Char_name = CharName; sendSize = 0x20; ReservePacket22.BlockCopy(lobbyActionDone, 0, sendSize); } } MD5 md5Hash22 = MD5.Create(); byte[] hash22 = md5Hash22.ComputeHash(ReservePacket22.Get(), 0, 0x20); ReservePacket22.BlockCopy(hash22, 12, 16); client.Session.ViewSend(ReservePacket22.Get(), sendSize); break; } return(1); }
private static int DataDataHandler(SessionTcpClient client, byte[] data, int Length) { ByteRef recv = new ByteRef(data, Length); byte id = recv.GetByte(0); MD5 md5Hash; byte[] hash; //Logger.Log("Data Server Data Handler"); //Logger.Log(Utility.ByteArrayToString(data)); //Logger.Log("DATA ID {0:X}", id); switch (id) { case 0xA1: if (data.Length < 9) { Logger.Warning("Data Server data size received incorrect."); //SessionHandler.KillSession(client.Session); return(-1); } uint accountId = recv.GetUInt32(1); if (accountId >= 1000) { ByteRef UList = new ByteRef(500); ByteRef CharList = new ByteRef(2500); ByteRef ReserveDataA1 = new ByteRef(new byte[] { 0xc4, 0x01, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x72, 0x4a, 0x94, 0x4f, 0x60, 0x27, 0xc4, 0x45, 0x4b, 0x7d, 0xcf, 0x27, 0x8e, 0x6d, 0xcd, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x61, 0x6c, 0x65, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x05, 0x00, 0x07, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x02, 0x00, 0x10, 0x00, 0x20, 0x00, 0x30, 0x00, 0x40, 0x00, 0x50, 0x00, 0x60, 0x00, 0x70, 0x00, 0x01, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb5, 0xfa, 0x01, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x46, 0x6e, 0xcf, 0x09, 0xde, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x52, 0x03, 0x00, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00 }); client.Session.Server_ip = recv.GetUInt32(5); // Set reserved data CharList.Set <byte>(0, 0xE0); CharList.Set <byte>(1, 0x08); CharList.Set <byte>(4, 0x49); CharList.Set <byte>(5, 0x58); CharList.Set <byte>(6, 0x46); CharList.Set <byte>(7, 0x46); CharList.Set <byte>(8, 0x20); // Content IDS // TODO: get number of content ids for account from database byte ContentIDCount = 5; //MySQL.ContentIDCount(client.Session.Account_id); CharList.Set <uint>(28, ContentIDCount); // content id count // Server name in the lobby menu // TODO: Get server name from config ReserveDataA1.Set <string>(60, "Onyx"); // server name // Prepare char list data for (int i = 0; i < 16; ++i) { CharList.BlockCopy(ReserveDataA1.At(32), (32 + 140 * i), 140); CharList.Fill(32 + 140 * i, 0x00, 4); UList.Fill(16 * (i + 1), 0x00, 4); } UList.Set <byte>(0, 0x03); int j = 0; List <Player> CharacterList = DBClient.GetMany <Player>(DBREQUESTTYPE.PLAYER, p => p.AccountId == client.Session.Account_id); client.Session.Char_id_list = new List <uint>(); client.Session.Char_id_list.Clear(); if (CharacterList != null && CharacterList.Count > 0) { foreach (Player C in CharacterList) { if (ConfigHandler.MaintConfig.MaintMode == 0 || C.GMLevel > 0) { // Add character id to session valid char id list client.Session.Char_id_list.Add(C.PlayerId); // Content Ids UList.Set <uint>(16 * (j + 1), C.PlayerId); CharList.Set <uint>(32 + 140 * j, C.PlayerId); UList.Set <uint>(20 * (j + 1), C.PlayerId); CharList.Set <uint>(4 + 32 + j * 140, C.PlayerId); CharList.Set <string>(12 + 32 + j * 140, C.Name); CharList.Set <byte>(46 + 32 + j * 140, C.Stats.Job); CharList.Set <byte>(73 + 32 + j * 140, C.Stats.JobLevel); // TODO: look into block copying structs CharList.Set <byte>(44 + 32 + j * 140, C.Look.Model.Race); CharList.Set <byte>(56 + 32 + j * 140, C.Look.Model.Face); CharList.Set <ushort>(58 + 32 + j * 140, C.Look.Equipment.Head); CharList.Set <ushort>(60 + 32 + j * 140, C.Look.Equipment.Body); CharList.Set <ushort>(62 + 32 + j * 140, C.Look.Equipment.Hands); CharList.Set <ushort>(64 + 32 + j * 140, C.Look.Equipment.Legs); CharList.Set <ushort>(66 + 32 + j * 140, C.Look.Equipment.Feet); CharList.Set <ushort>(68 + 32 + j * 140, C.Look.Equipment.Main); CharList.Set <ushort>(70 + 32 + j * 140, C.Look.Equipment.Sub); CharList.Set <byte>(72 + 32 + j * 140, (byte)C.Location.CurrentZone); CharList.Set <ushort>(78 + 32 + j * 140, C.Location.CurrentZone); j++; } } } md5Hash = MD5.Create(); if (ConfigHandler.MaintConfig.MaintMode > 0 && j == 0) { SendLobbyError(client); } else { UList.Set <byte>(1, 0x10); client.Session.DataSend(UList.Get(), 328); hash = md5Hash.ComputeHash(CharList.Get(), 0, 2272); CharList.BlockCopy(hash, 12, 16); client.Session.ViewSend(CharList.Get(), 2272); } } else { SendLobbyError(client); } break; case 0xA2: ByteRef ReserveDataA2 = new ByteRef(new byte[] { 0x48, 0x00, 0x00, 0x00, 0x49, 0x58, 0x46, 0x46, 0x0b, 0x00, 0x00, 0x00, 0x30, 0xD0, 0x10, 0xDC, 0x87, 0x64, 0x4B, 0x34, 0x72, 0x9A, 0x51, 0x23, 0x54, 0x14, 0x67, 0xF0, 0x82, 0xB2, 0xc0, 0x00, 0xC3, 0x57, 0x00, 0x00, 0x52, 0x65, 0x67, 0x69, 0x75, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x01, 0xd6, 0xd3, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x01, 0xf2, 0xd2, 0x00, 0x00 }); ByteRef key3 = new ByteRef(20); key3.BlockCopy(data.Skip(1).ToArray(), 0, key3.Length); key3.Set <byte>(16, key3.GetByte(16) - 2); ByteRef MainReserveData = new ByteRef(0x48); md5Hash = MD5.Create(); uint charid = client.Session.Char_id; if (client.Session.Char_id_list.Contains(charid)) { // TODO: get character from database ZoneChar zoneChar = new ZoneChar(); // MySQL.GetZoneChar(charid); zoneChar.Account_id = 1001; zoneChar.Zone_ip_str = "127.0.0.1"; zoneChar.Zone_ip = Utility.IPToInt("127.0.0.1", false); zoneChar.Zone_port = 54240; if (zoneChar != null) { if (zoneChar.Prev_zone == 0) { key3.Set <byte>(16, key3.GetByte(16) + 6); } if (ConfigHandler.MaintConfig.MaintMode == 0 || zoneChar.Gm_level > 0) { // TODO: handle session clearing //MySQL.ClearAccountSession(zoneChar.Account_id); // TODO: update characters previous zone //if (zoneChar.Prev_zone == 0) // MySQL.UpdateCharPrevZone(charid, zoneChar.Zone_id); // TODO: validate session for character bool accountValidated = true; if (accountValidated) // !MySQL.ValidAccountSession(zoneChar.Account_id)) { string sessionKey = Utility.ByteArrayToString(key3.Get(), ""); Console.WriteLine("SESSIONKEY: " + sessionKey); client.Session.Session_key = sessionKey; // TODO: create account session //MySQL.CreateAccountSession(zoneChar, charid, client.Session.Ip_address, sessionKey, client.Session.Version_mismatch); if (ConfigHandler.LoginConfig.LogUserIP == true) { // TODO: handle ip logging //MySQL.CreateIPRecord(zoneChar, charid, client.Session.Ip_address, client.Session.Mac_address); } ReserveDataA2.Set <uint>(0x38, zoneChar.Zone_ip); // server IP integer ReserveDataA2.Set <ushort>(0x3C, zoneChar.Zone_port); // server port ReserveDataA2.Set <uint>(0x40, client.Session.Server_ip); // search server IP integer ReserveDataA2.Set <uint>(0x44, ConfigHandler.LoginConfig.SearchServerPort); // search server port MainReserveData.BlockCopy(ReserveDataA2, 0, 0x48); MD5 md5HashA2 = MD5.Create(); MainReserveData.Fill(12, 0, 16); byte[] hasha2 = md5HashA2.ComputeHash(MainReserveData.Get(), 0, 0x48); MainReserveData.BlockCopy(hasha2, 12, 16); if (client.Session.View_client != null && client.Session.View_client.Connected) { client.Session.ViewSend(MainReserveData.Get(), 0x48); client.Session.View_client.Client.Disconnect(true); } client.Session.Status = SESSIONSTATUS.INGAME; } else { SendLobbyError(client); } } else { SendLobbyError(client); } } } else { SendLobbyError(client); } break; default: SendLobbyError(client); break; } return(1); }
private static int DatabaseDataHandler(SessionTcpClient client, byte[] data, int length) { byte[] response = new byte[] { 0 }; DBREQUESTTYPE requestType; DBRESULTTYPE resultType; //Console.WriteLine("Request handler fired : " + length.ToString()); //Logger.Info("Threads in Use: {0}, Total Memory: {1}", new object[] { Process.GetCurrentProcess().Threads.Count, GC.GetTotalMemory(false) }); if (length >= 2) { requestType = (DBREQUESTTYPE)data[0]; resultType = (DBRESULTTYPE)data[1]; (var type, string colname) = GetType(requestType); MethodInfo serialize = typeof(Program).GetMethod("Serialize", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); byte[] queryResponse = null; uint resultCount = 0; if (resultType <= DBRESULTTYPE.GETMANY) { var serializer = new ExpressionSerializer(new BinarySerializer()); var exp = serializer.DeserializeBinary(data.Skip(2).Take(length - 2).ToArray()); if (resultType == DBRESULTTYPE.GETONE) { MethodInfo queryOne = typeof(Query).GetMethod("QueryOne", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); var result = queryOne.Invoke(null, new object[] { db, colname, exp }); if (result != null) { resultCount = 1; queryResponse = ObjectToByteArray(result); } } else if (resultType == DBRESULTTYPE.GETMANY) { MethodInfo queryMany = typeof(Query).GetMethod("QueryMany", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); object[] args = new object[4] { db, colname, exp, resultCount }; var result = queryMany.Invoke(null, args); if (result != null) { resultCount = (uint)args[3]; queryResponse = ObjectToByteArray(result); } } if (queryResponse != null && queryResponse.Length > 0) { ByteRef qrref = new ByteRef(5 + queryResponse.Length); qrref.Set <byte>(0, 1); qrref.Set <uint>(1, resultCount); qrref.Set <byte[]>(5, queryResponse); client.Client.Send(qrref.Get()); return(1); } else { ByteRef qrref = new ByteRef(2); qrref.Set <byte>(0, 1); qrref.Set <uint>(1, 0); client.Client.Send(qrref.Get()); return(1); } } else if (resultType <= DBRESULTTYPE.INSERTMANY) { byte[] InsertData = data.Skip(2).ToArray(); if (resultType == DBRESULTTYPE.INSERTONE) { MethodInfo insertOne = typeof(Query).GetMethod("InsertOne", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); insertOne.Invoke(null, new object[] { db, colname, InsertData }); } else if (resultType == DBRESULTTYPE.INSERTMANY) { MethodInfo insertMany = typeof(Query).GetMethod("InsertMany", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); insertMany.Invoke(null, new object[] { db, colname, InsertData }); } } else if (resultType <= DBRESULTTYPE.UPDATEMANY) { uint updated = 0; ByteRef dataBR = new ByteRef(data); int filterLength = dataBR.GetInt32(2); byte[] filterBytes = dataBR.GetBytes(6, filterLength); int updateLength = dataBR.GetInt32(6 + filterLength); byte[] updateBytes = dataBR.GetBytes(6 + filterLength + 4, updateLength); var json = Deserialize <string>(updateBytes); var serializer = new ExpressionSerializer(new BinarySerializer()); var exp = serializer.DeserializeBinary(filterBytes); Dictionary <string, object> update = JsonConvert.DeserializeObject <Dictionary <string, object> >(json); if (resultType == DBRESULTTYPE.UPDATEONE) { MethodInfo updateOne = typeof(Query).GetMethod("UpdateOne", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); updated = Convert.ToUInt32(updateOne.Invoke(null, new object[] { db, colname, exp, update })); } else if (resultType == DBRESULTTYPE.UPDATEMANY) { MethodInfo updateMany = typeof(Query).GetMethod("UpdateMany", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); updated = Convert.ToUInt32(updateMany.Invoke(null, new object[] { db, colname, exp, update })); } ByteRef qrref = new ByteRef(5); qrref.Set <byte>(0, 1); qrref.Set <uint>(1, updated); client.Client.Send(qrref.Get()); return(1); } else if (resultType <= DBRESULTTYPE.DELETEMANY) { var serializer = new ExpressionSerializer(new BinarySerializer()); var exp = serializer.DeserializeBinary(data.Skip(2).Take(length - 2).ToArray()); long deleted = 0; if (resultType == DBRESULTTYPE.DELETEONE) { MethodInfo queryOne = typeof(Query).GetMethod("DeleteOne", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); deleted = Convert.ToUInt32(queryOne.Invoke(null, new object[] { db, colname, exp })); } else if (resultType == DBRESULTTYPE.DELETEMANY) { MethodInfo queryOne = typeof(Query).GetMethod("DeleteMany", BindingFlags.Public | BindingFlags.Static).MakeGenericMethod(type); deleted = Convert.ToUInt32(queryOne.Invoke(null, new object[] { db, colname, exp })); } ByteRef qrref = new ByteRef(5); qrref.Set <byte>(0, 1); qrref.Set <uint>(1, deleted); client.Client.Send(qrref.Get()); return(1); } else if (resultType <= DBRESULTTYPE.GETMAXID) { uint maxID = 0; if (requestType == DBREQUESTTYPE.ACCOUNT) { maxID = Query.GetMaxAccountID(db, colname); } else if (requestType == DBREQUESTTYPE.PLAYER) { maxID = Query.GetMaxPlayerID(db, colname); } ByteRef qrref = new ByteRef(5); qrref.Set <byte>(0, 1); qrref.Set <uint>(1, maxID); client.Client.Send(qrref.Get()); return(1); } } client.Client.Send(response); return(0); }
private static int AuthDataHandler(SessionTcpClient client, byte[] data, int Length) { ByteRef response = new ByteRef(33); int MaxUserName = 16; int MaxPassword = 16; int MaxMac = 24; int Success = 1; byte Action = data[32]; // Check if IP banned if (!IsIPBanned(client.Session.Ip_address)) { byte[] bUsername = new byte[MaxUserName]; byte[] bPassword = new byte[MaxPassword]; byte[] bMac = new byte[MaxMac]; Array.Copy(data, 0, bUsername, 0, MaxUserName); Array.Copy(data, MaxUserName, bPassword, 0, MaxPassword); Array.Copy(data, 0xC0, bMac, 0, MaxMac); string Username = Utility.ReadCString(bUsername); string Password = Utility.ReadCString(bPassword); string Mac = Utility.ReadCString(bMac); client.Session.Mac_address = Mac; // Check for valid string input if (!Utility.ValidAuthString(Username, MaxUserName) || !Utility.ValidAuthString(Password, MaxPassword)) { //bad characters in username or password Logger.Warning("Invalid characters sent from: {0}", new object[] { client.Session.Ip_address }); response.Resize(1); response.Set <byte>(0, LOGINRESULT.ERROR); Success = 0; } else { Username = Username.Trim(); Password = Password.Trim(); // TODO: check to see if account locked out if (Action == (byte)LOGINRESULT.ATTEMPT) { Account acc = DBClient.GetOne <Account>(DBREQUESTTYPE.ACCOUNT, a => a.Username.Equals(Username)); if (acc != null && acc.Password.Equals(Utility.GenMD5(Password))) { uint AccountID = acc.AccountId; // TODO: handle maintenance mode and gm accounts if (ConfigHandler.MaintConfig.MaintMode > 0) // && !MySQL.IsGMAccount(AccountID)) { response.Fill(0, 0x00, 33); Success = 0; } else if (acc.Status.HasFlag(ACCOUNTSTATUS.NORMAL)) { if (SessionHandler.AlreadyLoggedIn(AccountID)) { Success = 0; // TODO: might want to see about killing the other logged in session, but we'll see if it's necessary } else { // TODO: reset accounts last modification date //MySQL.ResetAccountLastModify(AccountID); client.Session.Account_id = AccountID; client.Session.Session_hash = Utility.GenerateSessionHash(AccountID, Username); response.Set <byte>(0, LOGINRESULT.SUCCESS); response.Set <uint>(1, client.Session.Account_id); response.BlockCopy(client.Session.Session_hash, 5, 16); client.Session.Status = SESSIONSTATUS.ACCEPTINGTERMS; Success = 1; } } else if (acc.Status.HasFlag(ACCOUNTSTATUS.BANNED)) { response.Fill(0, 0x00, 33); Success = 0; } } else { //if (acc != null && acc.Locked) //{ // TODO: increment attempts in accounts table //uint newLockTime = 0; //if (attempts + 1 >= 20) newLockTime = 172800; // 48 hours //else if (attempts + 1 == 10) newLockTime = 3600; // 1 hour //else if (attempts + 1 == 5) newLockTime = 900; // 15 minutes //fmtQuery = "UPDATE accounts SET attempts = %u, lock_time = UNIX_TIMESTAMP(NOW()) + %u WHERE id = %d;"; //if (Sql_Query(SqlHandle, fmtQuery, attempts + 1, newLockTime, accountId) == SQL_ERROR) // ShowError("Failed to update lock time for account: %s\n", name.c_str()); //} Logger.Warning("Invalid login attempt for: {0}", new object[] { Username }); response.Resize(1); response.Set <byte>(0, LOGINRESULT.ERROR); Success = 0; } } else if (Action == (byte)LOGINRESULT.CREATE) { response.Resize(1); if (ConfigHandler.MaintConfig.MaintMode == 0) { Account acc = DBClient.GetOne <Account>(DBREQUESTTYPE.ACCOUNT, a => a.Username.Equals(Username)); bool accountexists = (acc != null); uint maxAccountID = DBClient.GetMaxID(DBREQUESTTYPE.ACCOUNT); if (!accountexists && maxAccountID > 0) { uint AccountID = maxAccountID + 1; Account newAccount = new Account() { AccountId = maxAccountID + 1, Username = Username, Password = Utility.GenMD5(Password), TimeCreate = Utility.Timestamp(), TimeLastModify = Utility.Timestamp(), // TODO: make these configurable ContentIds = 3, Expansions = 14, Features = 13, Status = ACCOUNTSTATUS.NORMAL }; bool success = DBClient.InsertOne <Account>(DBREQUESTTYPE.ACCOUNT, newAccount); if (success) { response.Set <byte>(0, LOGINRESULT.ERROR_CREATE); Success = 0; } else { response.Set <byte>(0, LOGINRESULT.SUCCESS_CREATE); Success = 1; } } else { response.Set <byte>(0, LOGINRESULT.ERROR_CREATE); Success = 0; } } else { response.Set <byte>(0, LOGINRESULT.ERROR); Success = 0; } } } } else { response.Set <byte>(0, LOGINRESULT.ERROR_CREATE); Success = 0; } if (Success >= 0) { client.Session.AuthSend(response.Get()); if (Success == 0) { SessionHandler.KillSession(client.Session); } } return(Success); }