Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace GCKELogin
- {
- class GCKLoginParser1453 : GCKPacketFilterLog, GCKILoginParser
- {
- private Dictionary<byte, GCKLoginServer.LoginParseMethod> ParsingDict;
- public Dictionary<byte, GCKLoginServer.LoginParseMethod> ParsingDictionary()
- {
- return ParsingDict;
- }
- private int packetDataReadStartIndex(long user)
- {
- if (IsClientEncrypted(user))
- {
- return 9;
- }
- else
- {
- return 5;
- }
- }
- private int packetDataWriteStartIndex(long user)
- {
- if (IsClientEncrypted(user))
- {
- return 4;
- }
- else
- {
- return 4;
- }
- }
- private const byte PacketVersionRequest = 0x01;
- private const byte PacketDownloadInfoRequest = 0x02;
- private const byte PacketCreateCharacter = 0x03;
- private const byte PacketNationselect = 0x05;
- private const byte PacketGetCharacters = 0x0c;
- private const byte PacketGetEncryptionKey = 0xf2;
- private const byte PacketLogInRequest = 0xf3;
- private const byte PacketMGameLogInRequest = 0xf4;
- private const byte PacketServerList = 0xf5;
- private const byte PacketIntroNotice = 0xf6;
- private Dictionary<long, byte[]> clientEncryptionKeys = new Dictionary<long, byte[]>();
- private Dictionary<long, byte[]> serverEncryptionKeys = new Dictionary<long, byte[]>();
- private List<long> encryptedUsers = new List<long>();
- public GCKLoginParser1453()
- {
- ParsingDict = new Dictionary<byte, GCKLoginServer.LoginParseMethod>();
- ParsingDict.Add(PacketVersionRequest, VersionRequest);
- ParsingDict.Add(PacketDownloadInfoRequest, DownloadInfoRequest);
- ParsingDict.Add(PacketCreateCharacter, CreateCharacter);
- ParsingDict.Add(PacketNationselect, Nationselect);
- ParsingDict.Add(PacketGetCharacters, GetCharacters);
- ParsingDict.Add(PacketGetEncryptionKey, GetEncryptionKey);
- ParsingDict.Add(PacketLogInRequest, LogInRequest);
- ParsingDict.Add(PacketMGameLogInRequest, MGameLogInRequest);
- ParsingDict.Add(PacketServerList, ServerList);
- ParsingDict.Add(PacketIntroNotice, IntroNotice);
- }
- public void RemoveUser(long userId)
- {
- lock (clientEncryptionKeys)
- {
- clientEncryptionKeys.Remove(userId);
- }
- lock (serverEncryptionKeys)
- {
- serverEncryptionKeys.Remove(userId);
- }
- lock (encryptedUsers)
- {
- encryptedUsers.Remove(userId);
- }
- }
- /*
- public static void CreateNewServerListPacketData()
- {
- GCKAccountDatabaseConnection.GetNewGameServerList();
- GCKAccountDatabaseConnection.GameServerInfo[] gameServers= GCKAccountDatabaseConnection.GetGameServerList();
- List<byte[]> serverPacketBytes = new List<byte[]>();
- foreach (GCKAccountDatabaseConnection.GameServerInfo gameServer in gameServers)
- {
- byte[] thisServerBytes = new byte[528];
- thisServerBytes[0] = (byte)gameServer.LoginPacketName.Length;
- byte[] nameBytes = UTF8Encoding.UTF8.GetBytes(gameServer.LoginPacketName);
- Array.Copy(nameBytes, 0, thisServerBytes, 1, nameBytes.Length);
- thisServerBytes[256] = (byte)gameServer.Address.Length;
- byte[] addressBytes = UTF8Encoding.UTF8.GetBytes(gameServer.Address);
- Array.Copy(addressBytes, 0, thisServerBytes, 257, addressBytes.Length);
- byte[] serverNumberBytes = BitConverter.GetBytes(gameServer.Number);
- Array.Copy(serverNumberBytes, 0, thisServerBytes, 514, serverNumberBytes.Length);
- byte[] serverGroupBytes = BitConverter.GetBytes(gameServer.GroupNumber);
- Array.Copy(serverGroupBytes, 0, thisServerBytes, 516, serverGroupBytes.Length);
- byte[] serverSlotBytes = BitConverter.GetBytes(gameServer.Slots);
- Array.Copy(serverSlotBytes, 0, thisServerBytes, 520, serverSlotBytes.Length);
- byte[] serverAvailableBytes = BitConverter.GetBytes(gameServer.Available);
- Array.Copy(serverAvailableBytes, 0, thisServerBytes, 524, serverAvailableBytes.Length);
- serverPacketBytes.Add(thisServerBytes);
- }
- if (serverPacketBytes.Count > 0)
- {
- byte[] newPacketBytes = new byte[(528 * serverPacketBytes.Count) + 7];
- newPacketBytes[0] = 0x00;
- newPacketBytes[1] = 0x02;
- newPacketBytes[2] = 0x1E;
- newPacketBytes[3] = 0xFC;
- newPacketBytes[4] = PacketServerList;
- newPacketBytes[6] = (byte)serverPacketBytes.Count;
- for (int i = 0; i < serverPacketBytes.Count; i++)
- {
- Array.Copy(serverPacketBytes[i], 0, newPacketBytes, (528 * i) + 7, 528);
- }
- serverListPacketData = newPacketBytes;
- }
- }
- */
- private bool PreParse(long user, Socket socket, byte[] packetData, ref int packetLength, string text)
- {
- Log(packetData, packetLength, String.Format("User: {0} {1}", user, text));
- return true;
- }
- public bool VersionRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Version Request")) return false;
- byte[] versionBytes = BitConverter.GetBytes(Settings.LoginVersion);
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = PacketVersionRequest;
- packetData[outIndex + 1] = versionBytes[0];
- packetData[outIndex + 2] = versionBytes[1];
- packetLength = 3;
- return true;
- }
- public bool DownloadInfoRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Download Info Request")) return false;
- return false;
- }
- public bool CreateCharacter(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Create Character")) return false;
- return false;
- }
- public bool Nationselect(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Nation Select")) return false;
- int outIndex = packetDataWriteStartIndex(user) ;
- packetData[outIndex] = PacketNationselect;
- packetData[outIndex + 1]++;
- packetLength = 2;
- return true;
- }
- public bool GetCharacters(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Get Characters")) return false;
- return false;
- }
- public bool IsClientEncrypted(long user)
- {
- lock (encryptedUsers)
- {
- return encryptedUsers.Contains(user);
- }
- }
- public byte[] GetClientEncryptionKey(long user)
- {
- lock (clientEncryptionKeys)
- {
- if (clientEncryptionKeys.ContainsKey(user))
- return clientEncryptionKeys[user];
- else
- return null;
- }
- }
- public byte[] GetServerEncryptionKey(long user)
- {
- lock (serverEncryptionKeys)
- {
- if (serverEncryptionKeys.ContainsKey(user))
- return serverEncryptionKeys[user];
- else
- return null;
- }
- }
- private static Random encKeyRand = new Random();
- public bool GetEncryptionKey(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Encryption Key Request")) return false;
- //9
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = PacketGetEncryptionKey; outIndex++;
- byte[] clientKey = new byte[] { 0xaf, 0x6f, 0x80, 0x1f, 0xa7, 0x50, 0xb5, 0x3a };
- encKeyRand.NextBytes(clientKey);
- byte[] serverKey = Settings.GetServerEncryptionKey(clientKey);
- packetData[outIndex] = clientKey[0]; outIndex++;
- packetData[outIndex] = clientKey[1]; outIndex++;
- packetData[outIndex] = clientKey[2]; outIndex++;
- packetData[outIndex] = clientKey[3]; outIndex++;
- packetData[outIndex] = clientKey[4]; outIndex++;
- packetData[outIndex] = clientKey[5]; outIndex++;
- packetData[outIndex] = clientKey[6]; outIndex++;
- packetData[outIndex] = clientKey[7]; outIndex++;
- packetLength = outIndex - packetDataWriteStartIndex(user);
- lock (clientEncryptionKeys)
- {
- clientEncryptionKeys.Add(user, clientKey);
- }
- lock (serverEncryptionKeys)
- {
- serverEncryptionKeys.Add(user, serverKey);
- }
- lock (encryptedUsers)
- {
- encryptedUsers.Add(user);
- }
- return true;
- }
- public bool LogInRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Log In Request")) return false;
- int currentPacketIndex = packetDataReadStartIndex(user);
- ushort usernameLength = BitConverter.ToUInt16(packetData, currentPacketIndex);
- currentPacketIndex += 2;
- if ((usernameLength > Settings.MaxUsernameLength) || (usernameLength < Settings.MinUsernameLength))
- {
- return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 2);
- }
- string username = UTF8Encoding.UTF8.GetString(packetData, currentPacketIndex, usernameLength);
- currentPacketIndex += usernameLength;
- ushort passwordLength = BitConverter.ToUInt16(packetData, currentPacketIndex);
- currentPacketIndex += 2;
- if ((passwordLength > Settings.MaxPasswordLength) || (passwordLength < Settings.MinPasswordLength))
- {
- return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 2);
- }
- string password = UTF8Encoding.UTF8.GetString(packetData, currentPacketIndex, passwordLength);
- currentPacketIndex += passwordLength;
- Log(String.Format("Login: {0} : {1}", username, password));
- int loginRetVal = 0;
- if ((loginRetVal = GCKAccountDatabaseConnection.Login(username, password, true)) == 1)
- {
- //need to check if the person is a current user
- string serverIp = "";
- int serverNumber = 0;
- if (GCKAccountDatabaseConnection.IsCurrentUser(username, ref serverIp, ref serverNumber))
- {
- return sendLoginReturnPacket(user, socket, packetData, ref packetLength, 5);
- }
- else
- {
- //successful login
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = 0xFC; outIndex++;
- packetData[outIndex] = 0x1E; outIndex++;
- packetData[outIndex] = 0x01; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = PacketLogInRequest; outIndex++;
- packetData[outIndex] = 0x01; outIndex++;
- packetData[outIndex] = 0xFF; outIndex++;
- packetData[outIndex] = 0xFF; outIndex++;
- byte[] nameLengthBytes = BitConverter.GetBytes(usernameLength);
- packetData[outIndex] = nameLengthBytes[0]; outIndex++;
- packetData[outIndex] = nameLengthBytes[1]; outIndex++;
- byte[] usernameBytes = UTF8Encoding.UTF8.GetBytes(username);
- Array.Copy(usernameBytes, 0, packetData, outIndex, usernameBytes.Length);
- packetLength = outIndex + usernameLength - packetDataWriteStartIndex(user);
- return true;
- }
- }
- else
- {
- return sendLoginReturnPacket(user, socket, packetData, ref packetLength, (byte)loginRetVal);
- }
- }
- private bool sendLoginReturnPacket(long user, Socket socket, byte[] packetData, ref int packetLength, byte value)
- {
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = 0xfc; outIndex++;
- packetData[outIndex] = 0x1e; outIndex++;
- packetData[outIndex] = 0x01; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0xf3; outIndex++;
- packetData[outIndex] = value; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetLength = outIndex - packetDataWriteStartIndex(user);
- return true;
- }
- public bool MGameLogInRequest(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "MGame Log In Request")) return false;
- return false;
- }
- public bool ServerList(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Server List Request")) return false;
- GCKAccountDatabaseConnection.GameServerInfo[] gameServers = GCKAccountDatabaseConnection.GameServers;
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = 0xfc; outIndex++;
- packetData[outIndex] = 0x1e; outIndex++;
- packetData[outIndex] = 0x02; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = PacketServerList; outIndex++;
- packetData[outIndex] = (byte)gameServers.Length; outIndex++;
- //uint lastServerGroup = 0;
- for (int i = 0; i < gameServers.Length; i++)
- {
- byte[] ipLengthBytes = BitConverter.GetBytes((short)gameServers[i].Address.Length);
- Array.Copy(ipLengthBytes, 0, packetData, outIndex, ipLengthBytes.Length);
- outIndex += ipLengthBytes.Length;
- byte[] ipBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].Address);
- Array.Copy(ipBytes, 0, packetData, outIndex, ipBytes.Length);
- outIndex += ipBytes.Length;
- byte[] nameLengthBytes = BitConverter.GetBytes((short)gameServers[i].LoginPacketName.Length);
- Array.Copy(nameLengthBytes, 0, packetData, outIndex, nameLengthBytes.Length);
- outIndex += nameLengthBytes.Length;
- byte[] nameBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].LoginPacketName);
- Array.Copy(nameBytes, 0, packetData, outIndex, nameBytes.Length);
- outIndex += nameBytes.Length;
- byte[] availableBytes = BitConverter.GetBytes((ushort)(gameServers[i].Slots - gameServers[i].Available));
- packetData[outIndex] = availableBytes[0]; packetData[outIndex + 1] = availableBytes[1];
- outIndex += 2;
- byte[] numberBytes = BitConverter.GetBytes((ushort)gameServers[i].Number);
- packetData[outIndex] = numberBytes[0]; packetData[outIndex + 1] = numberBytes[1];
- outIndex += 2;
- byte[] groupNumberBytes = BitConverter.GetBytes((ushort)gameServers[i].GroupNumber);
- packetData[outIndex] = groupNumberBytes[0]; packetData[outIndex + 1] = groupNumberBytes[1];
- outIndex += 2;
- //if (lastServerGroup != gameServers[i].GroupNumber)
- //{
- byte[] karusKingLengthBytes = BitConverter.GetBytes((short)gameServers[i].KarusKing.Length);
- Array.Copy(karusKingLengthBytes, 0, packetData, outIndex, karusKingLengthBytes.Length);
- outIndex += karusKingLengthBytes.Length;
- byte[] karusKingBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].KarusKing);
- Array.Copy(karusKingBytes, 0, packetData, outIndex, karusKingBytes.Length);
- outIndex += karusKingBytes.Length;
- byte[] karusTextLengthBytes = BitConverter.GetBytes((short)gameServers[i].KarusText.Length);
- Array.Copy(karusTextLengthBytes, 0, packetData, outIndex, karusTextLengthBytes.Length);
- outIndex += karusTextLengthBytes.Length;
- byte[] karusTextBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].KarusText);
- Array.Copy(karusTextBytes, 0, packetData, outIndex, karusTextBytes.Length);
- outIndex += karusTextBytes.Length;
- byte[] elmoKingLengthBytes = BitConverter.GetBytes((short)gameServers[i].ElmoKing.Length);
- Array.Copy(elmoKingLengthBytes, 0, packetData, outIndex, elmoKingLengthBytes.Length);
- outIndex += elmoKingLengthBytes.Length;
- byte[] elmoKingBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].ElmoKing);
- Array.Copy(elmoKingBytes, 0, packetData, outIndex, elmoKingBytes.Length);
- outIndex += elmoKingBytes.Length;
- byte[] elmoTextLengthBytes = BitConverter.GetBytes((short)gameServers[i].ElmoText.Length);
- Array.Copy(elmoTextLengthBytes, 0, packetData, outIndex, elmoTextLengthBytes.Length);
- outIndex += elmoTextLengthBytes.Length;
- byte[] elmoTextBytes = UTF8Encoding.UTF8.GetBytes(gameServers[i].ElmoText);
- Array.Copy(elmoTextBytes, 0, packetData, outIndex, elmoTextBytes.Length);
- outIndex += elmoTextBytes.Length;
- //lastServerGroup = gameServers[i].GroupNumber;
- //}
- /*byte[] unknownBytes = BitConverter.GetBytes((ushort)0);
- packetData[outIndex] = unknownBytes[0]; packetData[outIndex + 1] = unknownBytes[1];
- outIndex += 2;*/
- /*byte[] slotsBytes = BitConverter.GetBytes((ushort)gameServers[i].Slots);
- packetData[outIndex] = slotsBytes[0]; packetData[outIndex + 1] = slotsBytes[1];
- outIndex += 2;*/
- }
- packetLength = outIndex - packetDataWriteStartIndex(user);
- return true;
- }
- public bool IntroNotice(long user, Socket socket, byte[] packetData, ref int packetLength)
- {
- if (!PreParse(user, socket, packetData, ref packetLength, "Intro Notice Request")) return false;
- //12:21:31 PM 12: Server To Client 127.0.0.1:19100 - 127.0.0.1:1508 Len 39: AA 55 21 00 F6 0E 00 4F 6D 66 67 20 49 72 6F 6E 20 70 77 6E 73 0E 00 4F 6D 66 67 20 49 72 6F 6E 20 70 77 6E 73 55 AA
- int outIndex = packetDataWriteStartIndex(user);
- packetData[outIndex] = 0xfc; outIndex++;
- packetData[outIndex] = 0x1e; outIndex++;
- packetData[outIndex] = 0x03; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = PacketIntroNotice; outIndex++;
- byte[] introNoticeLengthBytes = BitConverter.GetBytes((ushort)Settings.IntroNoticeString.Length);
- Array.Copy(introNoticeLengthBytes, 0, packetData, outIndex, introNoticeLengthBytes.Length);
- outIndex += introNoticeLengthBytes.Length;
- byte[] introNoticeBytes = UTF8Encoding.UTF8.GetBytes(Settings.IntroNoticeString);
- Array.Copy(introNoticeBytes, 0, packetData, outIndex, introNoticeBytes.Length);
- outIndex += introNoticeBytes.Length;
- int newsLength = 0;
- string[] newsItems = GCKAccountDatabaseConnection.NewsTexts;
- for (int i = 0; i < newsItems.Length; i++)
- {
- newsLength += newsItems[i].Length;
- }
- newsLength += ((newsItems.Length - 1) * 2);
- byte[] newsLengthBytes = BitConverter.GetBytes((ushort)newsLength);
- Array.Copy(newsLengthBytes, 0, packetData, outIndex, newsLengthBytes.Length);
- outIndex += newsLengthBytes.Length;
- for (int i = 0; i < newsItems.Length; i++)
- {
- byte[] newsItemStringBytes = UTF8Encoding.UTF8.GetBytes(newsItems[i]);
- Array.Copy(newsItemStringBytes, 0, packetData, outIndex, newsItemStringBytes.Length);
- outIndex += newsItemStringBytes.Length;
- packetData[outIndex] = 0x00; outIndex++;
- packetData[outIndex] = 0x0a; outIndex++;
- }
- packetLength = outIndex - packetDataWriteStartIndex(user) - 2; //the 2 is for the last 0x00 0x0a that doesn't get put there
- return true;
- }
- }
- }
Add Comment
Please, Sign In to add comment