/// <summary> /// Returns character name from an encrypted packet /// </summary> public static string GetCharName(byte[] packet) { return(Encoding.Default.GetString(Crypt.Decrypt(packet)).Substring(12, 12).Trim().TrimEnd('\0')); }
/// <summary> /// Creates all character display packet /// </summary> public static byte[] CreateCharacterPacket(string[] chars, string[] levels, string[] types, string[] wears) { if (chars[0] == " ") { return(CreateNewAccountPacket()); } var toReturn = new byte[] { 0x00 }; for (var i = 0; i < chars.Length; i++) { toReturn = i == 0 ? CreateCharDetailPacket(chars[i], Convert.ToInt32(levels[i]), Convert.ToInt32(types[i]), wears[i]) : CombineByteArray(toReturn, chars[i] != " " ? CreateCharDetailPacket(chars[i], Convert.ToInt32(levels[i]), Convert.ToInt32(types[i]), wears[i]) : CreateEmptyCharSlot()); } return(CombineByteArray(new byte[] { 0xb8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x03, 0xff, 0x05, 0x11 }, Crypt.Encrypt(toReturn))); }
/// <summary> /// Callback for the read opertaion. /// </summary> private void OnDataRead(IAsyncResult asyncResult) { var client = asyncResult.AsyncState as Client; var clientUsername = ""; try { if (client == null) { return; } var networkStream = client.NetworkStream; var newClientEp = (IPEndPoint)client.TcpClient.Client.RemoteEndPoint; // Check if IP is banned if (!IsIpBanned(newClientEp.Address.ToString())) { var read = networkStream.EndRead(asyncResult); if (read == 0) { lock (_clients) { _clients.Remove(client); return; } } clientUsername = CleanUsername(Encoding.Default.GetString(client.Buffer).Substring(14, 14).Trim().TrimEnd('\0')); switch (read) { case 12: PreparedPlayers.UnPrepare(clientUsername); break; case 56: if (PreparedPlayers.IsPrepared(clientUsername)) { client.Username = clientUsername; string[] chars = new string[5], levels = new string[5], types = new string[5], wears = new string[5]; _db.GetCharacters(client.Username, ref chars, ref levels, ref types, ref wears); Write(client.TcpClient, Packet.CreateCharacterPacket(chars, levels, types, wears)); } else { MyLogger.WriteGameServerLog("Unprepared user entered. Username : "******", IP : " + newClientEp.Address); Write(client.TcpClient, Packet.CreatePopup("User not prepared!")); } break; case 37: var charName = Packet.GetCharName(client.Buffer); if (_db.CharacterExists(client.Username, charName)) { PreparedPlayers.UnPrepare(client.Username); Write(client.TcpClient, Packet.CreatePopup("Selected " + charName)); } else { PreparedPlayers.UnPrepare(client.Username); Write(client.TcpClient, Packet.CreatePopup("Invalid character name!")); } break; case 33: var trimPacket33 = client.Buffer; Array.Resize(ref trimPacket33, read); if (Packet.CheckCharDeletePacket(trimPacket33)) { var characterName = Packet.GetCharName(client.Buffer); if (_db.CharacterExists(client.Username, characterName)) { if (_db.DeleteCharacter(characterName)) { Write(client.TcpClient, trimPacket33); } else { MyLogger.WriteGameServerLog("Error while deleting character : " + characterName); Write(client.TcpClient, Packet.CreatePopup("Internal server error!")); } } else { MyLogger.WriteGameServerLog("Hacker trying to delete " + Packet.GetCharName(trimPacket33) + ". Username : "******", IP : " + newClientEp.Address); Write(client.TcpClient, Packet.CreatePopup("Invalid character name!")); } } else { MyLogger.WriteGameServerLog("World enter packet got!"); File.WriteAllBytes("p", client.Buffer); } break; default: MyLogger.WriteGameServerLog("Got packet of length of " + read); var trimPacket = client.Buffer; Array.Resize(ref trimPacket, read); MyLogger.WriteGameServerLog("Seems like I got something new here : " + Encoding.Default.GetString(Crypt.Decrypt(trimPacket))); Write(client.TcpClient, Packet.CreatePopup("Emulator under construction!")); break; } } else { Write(client.TcpClient, Packet.CreatePopup("Your IP is banned.Contact gamemaster!")); } networkStream.BeginRead(client.Buffer, 0, client.Buffer.Length, OnDataRead, client); } catch (Exception e) { MyLogger.WriteLog("OnDataRead error : " + e.Message); if (client != null) { if (client.Username != null) { PreparedPlayers.UnPrepare(client.Username); } if (clientUsername != "") { PreparedPlayers.UnPrepare(clientUsername); } lock (_clients) { _clients.Remove(client); return; } } } }