public void SendHandshake(ushort pVersion, string pPatchLocation, byte pLocale) { _encryptIV = new byte[4]; rnd.NextBytes(_encryptIV); _decryptIV = new byte[4]; rnd.NextBytes(_decryptIV); Packet packet = new Packet(); packet.WriteUShort(pVersion); packet.WriteString(pPatchLocation); packet.WriteBytes(_decryptIV); packet.WriteBytes(_encryptIV); packet.WriteByte(pLocale); SendPacket(packet); _mapleVersion = pVersion; _maplePatchLocation = pPatchLocation; _mapleLocale = pLocale; _rijndaelAES.Key = GMSKeys.GetKeyForVersion(_mapleVersion); _rijndaelAES.Mode = CipherMode.ECB; _rijndaelAES.Padding = PaddingMode.PKCS7; _transformer = _rijndaelAES.CreateEncryptor(); }
static void Main(string[] args) { GMSKeys.Initialize(); { // Quick test ITEMS = new Dictionary <short, ItemEquip>(); ITEMS.Add(1, new ItemEquip(1113017) { StatusFlags = 0x0714, Potential1 = 40356, Potential2 = 30041, Potential3 = 30044, Potential4 = 12011, Potential5 = 2014, Potential6 = 2014, SocketState = 0x00FF, Nebulite2 = 1001, Nebulite1 = 2001, Nebulite3 = 3400, }); //File.WriteAllText("import.xml", ITEMS.Serialize()); if (File.Exists("import.xml")) { ITEMS = File.ReadAllText("import.xml").Deserialize <Dictionary <short, ItemEquip> >(); } } if (ITEMS == null) { ITEMS = new Dictionary <short, ItemEquip>(); } { TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 8484); listener.Start(); AsyncCallback EndAccept = null; EndAccept = (a) => { new Client(listener.EndAcceptSocket(a)); Console.WriteLine("accepted"); listener.BeginAcceptSocket(EndAccept, null); }; listener.BeginAcceptSocket(EndAccept, null); } Console.ReadLine(); }
/// <summary> /// Used as IAsyncResult parser for ContinueReading(). /// </summary> /// <param name="pIAR">The result AsyncCallback makes</param> private void EndReading(IAsyncResult pIAR) { int amountReceived = 0; try { amountReceived = _socket.EndReceive(pIAR); } catch (Exception ex) { Console.WriteLine(TypeName + " : " + ex.ToString()); amountReceived = 0; } if (amountReceived == 0) { // We got a disWvsBeta.Common.Sessions here! OnDisconnectINTERNAL(); return; } // Add amount of bytes received to _bufferpos so we know if we got everything. _bufferpos += amountReceived; try { // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period. if (_bufferpos == _bufferlen) { // It seems we have all data we need // Now check if we got a header if (_header) { if (!_encryption && _receivingFromServer) { // Unencrypted packets have a short header with plain length. ushort length = (ushort)(_buffer[0] | _buffer[1] << 8); StartReading(length); } else { int length = GetHeaderLength(_buffer); StartReading(length); } } else { Packet packet; if (_encryption) { _buffer = Decrypt(_buffer); packet = new Packet(_buffer); DoAction((date) => { try { OnPacketInbound(packet); } catch (Exception ex) { Console.WriteLine("Handling Packet Error: {0}", ex.ToString()); } }); } else { _encryption = true; // First packet received or sent is unencrypted. All others are. packet = new Packet(_buffer); _mapleVersion = packet.ReadUShort(); _maplePatchLocation = _maplePatchLocation = packet.ReadString(); _encryptIV = packet.ReadBytes(4); _decryptIV = packet.ReadBytes(4); _mapleLocale = packet.ReadByte(); Console.WriteLine(TypeName + " MapleVersion: {0}; Patch Location: {1}; Locale: {2}", _mapleVersion, _maplePatchLocation, _mapleLocale); _rijndaelAES.Key = GMSKeys.GetKeyForVersion(_mapleVersion); _rijndaelAES.Mode = CipherMode.ECB; _rijndaelAES.Padding = PaddingMode.PKCS7; _transformer = _rijndaelAES.CreateEncryptor(); packet.Reset(); DoAction((date) => { try { OnHandshakeInbound(packet); } catch (Exception ex) { Console.WriteLine("Handling Packet Error: {0}", ex.ToString()); } }); } StartReading(4, true); } } else { ContinueReading(); } } catch (SocketException socketException) { Console.WriteLine(TypeName + " Socket Exception while receiving data: {0}", socketException.Message); OnDisconnectINTERNAL(); } catch (Exception ex) { Console.WriteLine(TypeName + " [ERROR] EndReading(): {0}", ex.ToString()); OnDisconnectINTERNAL(); } }