public int Receive(ref byte[] data, int maxLength) { int bytesReceived = 0; if (!Client.Connected) { return(bytesReceived); } ByteRef outData = new ByteRef(0); using (NetworkStream stream = Client.GetStream()) { uint starttime = Utility.Timestamp(); while (!stream.DataAvailable) { if (Utility.Timestamp() - starttime >= Timeout) { return(bytesReceived); } } int bytesRead; byte[] buffer = new byte[4096]; while (stream.DataAvailable && (bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0) { outData.Append(buffer.Take(bytesRead).ToArray()); } } data = outData.Get(); return(bytesReceived); }
public static T GetOne <T>(DBREQUESTTYPE type, Expression <Func <T, bool> > expression) where T : IQueryResult, new() { var serializer = new ExpressionSerializer(new BinarySerializer()); byte[] query = serializer.SerializeBinary(expression); ByteRef payload = new ByteRef(2 + query.Length); payload.Set <byte>(0, (byte)type); payload.Set <byte>(1, (byte)DBRESULTTYPE.GETONE); payload.Set <byte[]>(2, query); ByteRef queryResult = new ByteRef(SendQuery(payload.Get())); if (queryResult.Length == 1 || (DBRESPONSETYPE)queryResult.GetByte(0) == DBRESPONSETYPE.FAILURE || (queryResult.Length == 2 && queryResult.GetByte(1) == 0) || (queryResult.Length >= 5 && queryResult.GetUInt32(1) == 0)) { return(default(T)); } T result = Deserialize <T>(queryResult.Get().Skip(5).ToArray()); return(result); }
public static long UpdateMany <T>(DBREQUESTTYPE type, Expression <Func <T, bool> > expression, Dictionary <string, object> update) { long updated = 0; var serializer = new ExpressionSerializer(new BinarySerializer()); byte[] serializedFilter = serializer.SerializeBinary(expression); byte[] serializedUpdate = Serialize(update); ByteRef payload = new ByteRef(2 + 4 + serializedFilter.Length + 4 + serializedUpdate.Length); payload.Set <byte>(0, type); payload.Set <byte>(1, (byte)DBRESULTTYPE.UPDATEMANY); payload.Set <int>(2, serializedFilter.Length); payload.Set <byte[]>(6, serializedFilter); payload.Set <int>(6 + serializedFilter.Length, serializedUpdate.Length); payload.Set <byte[]>(6 + serializedFilter.Length + 4, serializedUpdate); ByteRef queryResult = new ByteRef(SendQuery(payload.Get())); if (queryResult.Length >= 5 || (DBRESPONSETYPE)queryResult.GetByte(0) != DBRESPONSETYPE.FAILURE) { updated = queryResult.GetUInt32(1); } return(updated); }
public BaseChunk(byte[] bytes = null) { data = new ByteRef(512); if (bytes != null) { data.Set <byte[]>(0, bytes); } }
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); }
public override int GetHashCode() { var hash = base.GetHashCode(); hash ^= Readonly.GetHashCode(); hash ^= IntId.GetHashCode(); hash ^= UIntId.GetHashCode(); hash ^= LongId.GetHashCode(); hash ^= ULongId.GetHashCode(); hash ^= ShortId.GetHashCode(); hash ^= UShortId.GetHashCode(); hash ^= DoulbeId.GetHashCode(); hash ^= ByteId.GetHashCode(); hash ^= CharId.GetHashCode(); hash ^= SByteId.GetHashCode(); hash ^= CharRef.GetHashCode(); hash ^= ByteRef.GetHashCode(); hash ^= SByteRef.GetHashCode(); hash ^= Int16Id.GetHashCode(); hash ^= UInt16Id.GetHashCode(); hash ^= Int32Id.GetHashCode(); hash ^= UInt32Id.GetHashCode(); hash ^= Int64Id.GetHashCode(); hash ^= UInt64Id.GetHashCode(); hash ^= DateTime.GetHashCode(); hash ^= TitleEnum.GetHashCode(); hash ^= ATitleEnum.GetHashCode(); hash ^= FirstName.GetHashCode(); hash ^= LastName.GetHashCode(); hash ^= Bool.GetHashCode(); hash ^= BoolRef.GetHashCode(); if (Array != null) { hash ^= Array.GetHashCode(); } if (Enumerable != null) { hash ^= Enumerable.GetHashCode(); } if (Father != null) { hash ^= Father.GetHashCode(); } if (Mother != null) { hash ^= Mother.GetHashCode(); } if (Hidden != null) { hash ^= Hidden.GetHashCode(); } return(hash); }
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); }
public static bool InsertMany <T>(DBREQUESTTYPE type, List <T> obj) { byte[] serializedObj = Serialize(obj); ByteRef payload = new ByteRef(2 + serializedObj.Length); payload.Set <byte>(0, type); payload.Set <byte>(1, (byte)DBRESULTTYPE.INSERTMANY); payload.Set <byte[]>(2, serializedObj); byte[] queryResult = SendQuery(payload.Get()); if (queryResult.Length == 1 || (DBRESPONSETYPE)queryResult[0] == DBRESPONSETYPE.FAILURE) { return(false); } return(true); }
public static uint GetMaxID(DBREQUESTTYPE type) { uint maxID = 0; ByteRef payload = new ByteRef(2); payload.Set <byte>(0, (byte)type); payload.Set <byte>(1, (byte)DBRESULTTYPE.GETMAXID); ByteRef queryResult = new ByteRef(SendQuery(payload.Get())); if (queryResult.Length >= 5 && (DBRESPONSETYPE)queryResult.GetByte(0) != DBRESPONSETYPE.FAILURE) { maxID = queryResult.GetUInt32(1); } return(maxID); }
public static ushort ProcessPacket(Player player, byte[] packetData, int packetSize, ZoneCluster cluster) { bool canProcess = true; ByteRef packetRef = new ByteRef(packetData.Take(packetSize).ToArray()); byte[] decryptedData; ushort cursor = 0; // TODO: make this more efficient and trim off the header before anything else... int checksum = Utility.Checksum(packetRef.GetBytes(PACKET_HEADER_SIZE, packetSize - PACKET_HEADER_SIZE), packetSize - (PACKET_HEADER_SIZE + 16), packetRef.GetBytes(packetSize - 16, 16)); decryptedData = packetRef.Get(); if (checksum != 0) { Crypto.DecryptPacket(player.Client.blowfish, ref decryptedData); packetRef = new ByteRef(decryptedData); checksum = Utility.Checksum(packetRef.GetBytes(PACKET_HEADER_SIZE, packetSize - PACKET_HEADER_SIZE), packetSize - (PACKET_HEADER_SIZE + 16), packetRef.GetBytes(packetSize - 16, 16)); if (checksum != 0) { canProcess = false; Logger.Error("Unable to decrypt a packet"); packetRef.DebugDump(); } } packetRef = new ByteRef(packetRef.Get().Skip(PACKET_HEADER_SIZE).ToArray()); while (canProcess && packetRef.Length - cursor > 4) { byte id = packetRef.GetByte(cursor); ushort size = (byte)(packetRef.GetByte(cursor + 1) * 2); if (size > packetRef.Length) { return(0); } if (!ProcessDataChunk(player, packetRef.GetBytes(cursor, size), cluster)) { Logger.Warning("Unable to process chunk ID {0} for Player ID: {1}, Possible validation issue", new object[] { id, player.PlayerId }); } cursor += size; } return(cursor); }
public static long DeleteMany <T>(DBREQUESTTYPE type, Expression <Func <T, bool> > expression) { long deleted = 0; var serializer = new ExpressionSerializer(new BinarySerializer()); byte[] query = serializer.SerializeBinary(expression); ByteRef payload = new ByteRef(2 + query.Length); payload.Set <byte>(0, (byte)type); payload.Set <byte>(1, (byte)DBRESULTTYPE.DELETEMANY); payload.Set <byte[]>(2, query); ByteRef queryResult = new ByteRef(SendQuery(payload.Get())); if (queryResult.Length >= 5 || (DBRESPONSETYPE)queryResult.GetByte(0) != DBRESPONSETYPE.FAILURE) { deleted = queryResult.GetUInt32(1); } return(deleted); }
public bool Handler(Player player, byte[] bytes) { if (bytes.Length < MinSize || bytes.Length > MaxSize) { return(false); } LockStyleInfoDataHeader LockStyleInfoDataHeader = Utility.Deserialize <LockStyleInfoDataHeader>(bytes.Take(8).ToArray()); LockStyleInfoDataHeader.header.size *= 2; ByteRef byteRef = new ByteRef(bytes.Skip(8).ToArray()); if (byteRef.Length != bytes.Length - 8) { return(false); } LockItem[] lockItems = null; if (LockStyleInfoDataHeader.count > 0 && byteRef.Length > 0) { lockItems = new LockItem[LockStyleInfoDataHeader.count]; for (int i = 0; i < LockStyleInfoDataHeader.count; i++) { lockItems[i] = Utility.Deserialize <LockItem>(byteRef.GetBytes(i * 8, 8)); } } if (Validator(LockStyleInfoDataHeader, lockItems)) { // TODO: Handle the lockstyle information in player object i.e. Player->UpdateLockStyle(header,items) return(true); } return(false); }
private static bool CreateCharacter(uint accountID, string charName, ByteRef buf) { Random rnd = new Random(); Player player = new Player() { AccountId = accountID, TimeCreate = Utility.Timestamp(), TimeLastModify = Utility.Timestamp() }; // TODO: put all these class initializers in various constructors player.Name = charName; player.Look = new Data.Game.Entities.Look(); player.Look.Model = new ModelInfo(); player.Look.Model.Race = buf.GetByte(48); player.Look.Size = buf.GetByte(57); player.Look.Model.Face = buf.GetByte(60); player.Stats = new PlayerStats(); byte job = buf.GetByte(50); player.Stats.Job = (byte)Utility.Clamp(job, 1, 6); if (player.Stats.Job != job) { Logger.Warning("{0} tried to create a character with an invalid starting job", new object[] { charName }); } player.Profile = new ProfileInfo(); player.Profile.Nation = buf.GetByte(54); player.Location = new LocationInfo(); player.Location.Position = new PositionInfo(); player.Location.Position.Pos = new OnyxVec3(0, 0, 0); switch (player.Profile.Nation) { case 0x02: while (player.Location.CurrentZone == 0 || player.Location.CurrentZone == 0xEF) { player.Location.CurrentZone = (ushort)rnd.Next(0xEE, 0xF1); } break; case 0x01: player.Location.CurrentZone = (ushort)rnd.Next(0xEA, 0xEC); break; case 0x00: player.Location.CurrentZone = (ushort)rnd.Next(0xE6, 0xE8); break; } uint PlayerID = 1000; uint maxPlayerID = DBClient.GetMaxID(DBREQUESTTYPE.PLAYER); if (maxPlayerID > 0) { PlayerID = maxPlayerID + 1; } player.PlayerId = PlayerID; bool success = DBClient.InsertOne <Player>(DBREQUESTTYPE.PLAYER, player); if (success) { Logger.Success("New Character Created : {0}", new object[] { charName }); } else { Logger.Error("Failed to Create Character : {0}", new object[] { charName }); } return(success); }
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); }
static void TestJunk() { //hashstring: 72BDF6261A0B4EE38EC41C57AB6AAC25 //keystring: 0000000000000000000000000000000058E05DAD //encrypted packet data: 3F003E00DB0200002667BF5F010028010000000000000000280100007 3B0330A3 1924FB87 38FDB7A3 FB7E6B0B A0ADA8BED DF1D113A 80B364929AA24715002ACF3D9F1A381B102F2D18F008B9AD2815 //[BLOCK: 0] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E7 38FDB7A3 FB7E6B0B A0ADA8BED DF1D113A 80B364929AA24715002ACF3D9F1A381B102F2D18F008B9AD2815 //[BLOCK: 2] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E4 F4749784 89F9CFDB A0ADA8BED DF1D113A 80B364929AA24715002ACF3D9F1A381B102F2D18F008B9AD2815 //[BLOCK: 4] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E4 F4749784 89F9CFD8 D2D8FEFD6 5E271F3A 80B364929AA24715002ACF3D9F1A381B102F2D18F008B9AD2815 //[BLOCK: 6] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E4 F4749784 89F9CFD8 D2D8FEFD6 5E271F12 74BEFC26799FF515002ACF3D9F1A381B102F2D18F008B9AD2815 //[BLOCK: 8] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E4 F4749784 89F9CFD8 D2D8FEFD6 5E271F12 74BEFC26799FF500000023ED01BD271B102F2D18F008B9AD2815 //[BLOCK: 10] : 3F003E00DB0200002667BF5F010028010000000000000000280100000 10428AF6 498804E4 F4749784 89F9CFD8 D2D8FEFD6 5E271F12 74BEFC26799FF500000023ED01BD27BA727F92EBB505C6AD2815 //3F003E00DB0200002667BF5F01002801000000000000000028010000 01 04 28 AF 64 98 80 4E 4F 47 49 78 48 9F 9C FD 8D2D8FEFD6 5E271F12 74BEFC26799FF500000023ED01BD27BA727F92EBB505C6AD2815 //byte[] responseData = Utility.StringToByteArray("0100000098020000672CBF5F00C91900FEFDC17779000000200000000A2E0100300001000000000044550000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000200970E41CBE8616ACEA95F9839B80D10000000057494E0001000101B751DEC81A5FF70D65298AF38FF99335"); //byte[] responseData = Utility.StringToByteArray("02000100230000008F28BF5F01C9190000000000000000002000000098C50558238A946B04A25B4846873FF50030620B1C1478C23E"); //int checksum = Utility.Checksum(responseData.Skip(0x1C).ToArray(),responseData.Length - (28 + 16), responseData.Skip(responseData.Length-16).ToArray()); //List<UInt32> vec = new List<uint>(); //bool test = Compression.ReadToVector("decompress.dat", ref vec); Compression.Initialize(); int checksum = 0; //string packetbytes = "3F003E00DB0200002667BF5F0100280100000000000000002801000073B0330A31924FB8738FDB7A3FB7E6B0BA0ADA8BEDDF1D113A80B364929AA24715002ACF3D9F1A381B102F2D18F008B9AD2815"; //byte[] packet = Utility.StringToByteArray(packetbytes); //string keybytes = "0000000000000000000000000000000058E05DAD"; //byte[] key = Utility.StringToByteArray(keybytes); //Blowfish blowfish = new Blowfish(); //Buffer.BlockCopy(key,0,blowfish.key,0,20); //byte[] hash = Crypto.SetupHashKey(key); //Crypto.BlowfishInitialize(hash, 16, ref blowfish.P, ref blowfish.S); //Crypto.DecryptPacket(blowfish, ref packet); //string decryptedstr = Utility.ByteArrayToString(packet,"").ToUpper(); //buffsize: 81, refpos: 61, packetdatasize: 259 //actual packetdatasize: 40 //string decryptedData = "03000100880200001D60C45F013C212C0000000000000000A213870401884C6EF7C0BCCEFF083039BF0EB8B33A34C09DD5A5289DF3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8B6C70960E0D37F9F6FF9F009D31831D58D0D351121ED26724A0CBC9E3BBA577E9EB4732C95E2260CEAA72FF1FFAC76481F3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFDCB12F2A08901CEE13C970D1F0E8F7C87FE7FFFFFF7F34FAE6BCFAFFFFFFEEFD07EEFC7E729D9B89040000C0AF10A966C06B51C37220BF9B45A814"; string compressed = "013207E6362929024486EFA52835296A7D87817F11500202BB34A4E6160DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1100DD1900E0D478463B29F63F0F7FDF7BD8C9C912D411FCAF0FFFF4DBF08DFDC2EDA26E9D0F0FFFF7F892AE930709BFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2F1D066EA7FAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1F2BB81B6D1F2F1A45A368148DA251348A46D1281A45A368148DA251348A46D1281A45A368148DA251348A46D1281A45A368148DA251348A46D1F8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F3CFFDDD2A1616B1AE17F132EF37FB3444EA8A52135B7688886688886688886688886688886688886688886688886688886688886688886688886688886688886688886688886746838221C93FD1C83BFEFBFEF65E48C6C09FA5086FFFF6FFA3130B9A1390626B707DD3130B975A63130B9097C0C4C6E84BC636072A3771C0393DB9F48636072038E760C4C6E239BC7C0E4F6BD5163607203056C0C4C6E09E51F03935B06C31898DC887BF4C60C0000"; //01C633A96F16ABD2A1E1A65ACF02428E7A4A50923CA44FFAA2F5BCC7DFCF7EC6C7EE5FFD7EF94B518AD882480590469761BF342929024486EFA52835296A7D87817F115002022B8112288112F8FFAA91E4086DE4BE3FFFEF5E8AC8402C2000C4E2FFFF2F3FB4B47D2F23E7BF7AF34E1F888647954450430DB5447E97086A0F4A249144124924979EE5D533B6D74670D19624D19043D19024D1908468F81F5120B7A62578F5FF57471259F50C837F8CE611E69E933B3A982F09D18088864CD2C3AA077CE723E0FE145962504B0DA39E2915C9FEFFCDFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF byte[] compressedbytes = Utility.StringToByteArray(compressed); string compressedstr = Utility.ByteArrayToString(compressedbytes); Console.WriteLine(compressedstr); string decompressedtarget = "090008004F010000AF18CB5F01009803000000000000000098030000010488B6670121473DBD92F0903EE98BD6F31EDFAD3DB6A4AD3FBFBFFC26B45743010100006F70858A5657768ABF76357636619243"; byte[] decompressedtargetbytes = Utility.StringToByteArray(decompressedtarget); //0800080098030000AF18CB5F010000000000000000000000000000002016080001000000000000005A000107000000000000000000000000000000000000000000000000000000002016080001000000000000005A000108000000000000000000000000000000000000000000000000000000002016080001000000000000005A000109000000000000000000000000000000000000000000000000000000002016080001000000000000005A00010A000000000000000000000000000000000000000000000000000000002016080001000000000000005A00010B000000000000000000000000000000000000000000000000000000002016080001000000000000006A00010C00004000000000080012000000000000000000000000000000000000201608000100000000000000E100010D000040000000000A0013000000000000000000000000000000000000201608000100000000000000DE00010E000000000000000000000000000000000000000000000000000000001F080800010000009244001B050000001F08080001000000B1450032050000001F08080001000000E93A0018050000001F080800010000002C380015050000001F08080001000000073B001D050000001F08080001000000163B001E050000001F08080001000000C73B001F050000001F0808000100000068330033050000001F08080001000000BD3B0026050000001F08080001000000D5390020050000001F0808000100000085390021050000001F0808000100000097340022050000001F0808000100000032390023050000001F0808000100000034350027050000001D04080001000000417C0800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000005E5A08001500000000000000000000000000000000000000000015154004151540041515400415154004151540041515400429294004151540041515400415154004151540041515400415154004151540041515400415154004151540041515400415154004000000000000000000000000000000000000000000000000000000000000000000000000000033000000000000006400000000000000010000001011000400000000000000000000000000000000716608000201C400010000000000000063180700AA0014040A1028084A267FC54A2693C94A2683C74A267FC54A2693C942124400FA0000002C912400338924C80A0001F398581280D88825C8030000FA96581200E34722C8000000FA913C128068C91DC8070000FA96F81100C8122D00000000FA2C1D3300688921C80B0000FA96581200E450420000FA001F2CB134006A891EC8000000FA96381200848822C8002E00F28C2812006B0923C8000000FA9658128064091DC8001900EE96201200CA123500000000FA2C053400716608000201C400010D00000000000063180700AA0014040A1028084A267FC54A2693C94A2683C74A267FC54A2693C96AC91FC800D100C896581200C69244000000FA002CB13400964924C80000FA009958120066091FC80000F8B49658120066490DC80000DBCB96581200164900C8000026E296381200CC5224C8000000FA2CB13200170200C8000000FA649011808E8C22C8000000FAC89412008E0C1EBB000000FAC8381200AE8F3EC8000000FAFAE81300AE8F3EC3000000FAFAE813008E3EFA00000000FAE8A33F000B0E0800010000000000000000000000000000000000000000000000 // 2016080001000000000000005A000107000000000000000000000000000000000000000000000000000000002016080001000000000000005A000108000000000000000000000000000000000000000000000000000000002016080001000000000000005A000109000000000000000000000000000000000000000000000000000000002016080001000000000000005A00010A000000000000000000000000000000000000000000000000000000002016080001000000000000005A00010B000000000000000000000000000000000000000000000000000000002016080001000000000000006A00010C00004000000000080012000000000000000000000000000000000000201608000100000000000000E100010D000040000000000A0013000000000000000000000000000000000000201608000100000000000000DE00010E000000000000000000000000000000000000000000000000000000001F080800010000009244001B050000001F08080001000000B1450032050000001F08080001000000E93A0018050000001F080800010000002C380015050000001F08080001000000073B001D050000001F08080001000000163B001E050000001F08080001000000C73B001F050000001F0808000100000068330033050000001F08080001000000BD3B0026050000001F08080001000000D5390020050000001F0808000100000085390021050000001F0808000100000097340022050000001F0808000100000032390023050000001F0808000100000034350027050000001D04080001000000417C0800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000005E5A08001500000000000000000000000000000000000000000015154004151540041515400415154004151540041515400429294004151540041515400415154004151540041515400415154004151540041515400415154004151540041515400415154004000000000000000000000000000000000000000000000000000000000000000000000000000033000000000000006400000000000000010000001011000400000000000000000000000000000000716608000201C400010000000000000063180700AA0014040A1028084A267FC54A2693C94A2683C74A267FC54A2693C942124400FA0000002C912400338924C80A0001F398581280D88825C8030000FA96581200E34722C8000000FA913C128068C91DC8070000FA96F81100C8122D00000000FA2C1D3300688921C80B0000FA96581200E450420000FA001F2CB134006A891EC8000000FA96381200848822C8002E00F28C2812006B0923C8000000FA9658128064091DC8001900EE96201200CA123500000000FA2C053400716608000201C400010D00000000000063180700AA0014040A1028084A267FC54A2693C94A2683C74A267FC54A2693C96AC91FC800D100C896581200C69244000000FA002CB13400964924C80000FA009958120066091FC80000F8B49658120066490DC80000DBCB96581200164900C8000026E296381200CC5224C8000000FA2CB13200170200C8000000FA649011808E8C22C8000000FAC89412008E0C1EBB000000FAC8381200AE8F3EC8000000FAFAE81300AE8F3EC3000000FAFAE813008E3EFA00000000FAE8A33F000B0E0800010000000000000000000000000000000000000000000000 //01000100EC000000D70ECB5F000000000000000000000000000000004F040100000000000A8201004455000000040040E17A9541AAF168C1AE478542000000002828640000840000000000000001000000000000F800000000000000D70ECB5F67919A230000F800040153106520533053408750486100704C80700070007000700000000000000000000000000001007824362A000000001C0000000000000000000000020000005365747A6F72000000000000000000000000000000000000000000002E5A400020A003000000000000000001000000000A000003FEFF0F00014B2525254B314B25324B4B4B4B4B1941003F0041003A00400045004700F1FFF5FFF1FFF0FF12001100360080040000AA0000000100000000000000000000000000000001000000081A0100BF0E54970FC07D2E56C00000F0FF5FFF8CB8E7A68762051F1A352530C9FDE31700000000000000000000000000000000 // 4F040100000000000A8201004455000000040040E17A9541AAF168C1AE478542000000002828640000840000000000000001000000000000F800000000000000D70ECB5F67919A230000F800040153106520533053408750486100704C80700070007000700000000000000000000000000001007824362A000000001C0000000000000000000000020000005365747A6F72000000000000000000000000000000000000000000002E5A400020A003000000000000000001000000000A000003FEFF0F00014B2525254B314B25324B4B4B4B4B1941003F0041003A00400045004700F1FFF5FFF1FFF0FF12001100360080040000AA0000000100000000000000000000000000000001000000081A0100BF0E54970FC07D2E56C00000F0FF5FFF8CB8E7A68762051F1A352530C9FDE317000000000000000000000000000000000015000000 //01C633A96F16ABD2A1E1A65ACF02428E7A4A50923CA44FFAA2F5BCC7DFCF7EC6C7EE5FFD7EF943C5801888080C7D28C37E69525204880CDF4B516A52D4FA0E03FF22A00404560225500225F0FF55A03904C8C87D87FEDFBD14918158400088C5FFFF5FEE7ED2F6BD8C9CFFEACD3B7D201A1E5512410D35D412F95D22A83D289144124924915C7A9657CFD85E1BC1455B9244430E454392444312A2E17F4481DC9A96E0D5FF5F1D4964D5330CFE319A47987B4EEEE860BE244403221A32490FAB1EF09D8F80FB536489412D358C7AA65424FBFFCF9B060000 ByteRef packetref = new ByteRef(compressedbytes); uint PacketDataSize = packetref.GetUInt32(packetref.Length - 4); //byte[] decompressed = Utility.Decompress(cb); byte[] decompressed = new byte[2520]; int count = Compression.Decompress(compressedbytes, PacketDataSize, ref decompressed, 2520); string decompressedstr = Utility.ByteArrayToString(decompressed.Take(count).ToArray()); Console.WriteLine(decompressedstr); byte[] recompress = new byte[2520]; int countb = Compression.Compress(decompressed.Take(count).ToArray(), (uint)count, ref recompress, (uint)2520); int compressedsize = Compression.CompressedSize(countb); ByteRef compressedData = new ByteRef(recompress.Take(compressedsize).ToArray()); compressedData.Append(BitConverter.GetBytes(count)); string recompressedstr = Utility.ByteArrayToString(compressedData.Get()); Console.WriteLine(recompressedstr); checksum = Utility.Checksum(decompressedtargetbytes.Skip(0x1C).ToArray(), decompressedtargetbytes.Length - (28 + 16), decompressedtargetbytes.Skip(decompressedtargetbytes.Length - 16).ToArray()); //checksum = Utility.Checksum(packet.Skip(0x1C).ToArray(), packet.Length - (28 + 16), packet.Skip(packet.Length - 16).ToArray()); if (checksum != 0) { Logger.Error("checksum f****d"); } }
public static byte[] SendQuery(byte[] payload) { ByteRef response = new ByteRef(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00 }); TcpClient client = null; //bool success = false; try { client = new TcpClient("127.0.0.1", 50505); } catch (SocketException e) { Logger.Error("Unable to connect to Database Server : {0}", new object[] { e.Message }); Environment.Exit(0); } NetworkStream stream = client.GetStream(); stream.Write(payload, 0, payload.Length); bool receivedData = false; while (!receivedData) { if (stream.CanRead) { response.Resize(0); int bytesRead = 0; byte[] readbuffer = new byte[4096]; if (stream.DataAvailable) { do { bytesRead = stream.Read(readbuffer, 0, readbuffer.Length); response.Append(readbuffer.Take(bytesRead).ToArray()); } while (stream.DataAvailable && bytesRead > 0); } if (response.Length > 0) { receivedData = true; } //success = true; } } //if (client.Connect()) //{ // int retryCounter = 3; // while (success == false && retryCounter > 0) // { // try // { // response = client.SendData(payload); // if (response != null && response.Length > 0) // { // success = true; // } // else // { // retryCounter--; // } // } // catch(Exception e) // { // Logger.Error("Error trying to query results from database server : {0}", new object[] { e.Message }); // } // } //} client.Client.Disconnect(true); client.Dispose(); return(response.Get()); }
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); }