Example #1
0
        public void RefreshPackets()
        {
            ListView.BeginUpdate();

            MaplePacket previous = ListView.SelectedIndices.Count > 0
                ? FilteredPackets[ListView.SelectedIndices[0]]
                : null;

            Opcodes.Clear();
            ListView.Clear();

            MainForm.DataForm.ClearHexBox();
            MainForm.StructureForm.Tree.Nodes.Clear();
            MainForm.PropertyForm.Properties.SelectedObject = null;

            if (!mViewOutboundMenu.Checked && !mViewInboundMenu.Checked)
            {
                return;
            }
            int previousIndex = -1;

            foreach (MaplePacket packet in mPackets)
            {
                if (packet.Outbound && !mViewOutboundMenu.Checked)
                {
                    continue;
                }
                if (!packet.Outbound && !mViewInboundMenu.Checked)
                {
                    continue;
                }
                if (!Opcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode))
                {
                    Opcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
                }

                Definition definition = Config.Instance.GetDefinition(packet);
                if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore)
                {
                    continue;
                }

                int index = ListView.AddPacket(packet);
                if (packet == previous)
                {
                    previousIndex = index;
                }
            }

            MainForm.SearchForm.RefreshOpcodes(true);

            ListView.EndUpdate();

            // This should be called after EndUpdate so VirtualListSize is set properly
            if (previous != null && previousIndex >= 0)
            {
                ListView.Items[previousIndex].Selected = true;
                ListView.Items[previousIndex].EnsureVisible();
            }
        }
Example #2
0
        private void AddPacket(MaplePacket packet, bool buffered = true, bool forceAdd = false)
        {
            mPackets.Add(packet);

            Definition definition =
                Config.Instance.GetDefinition(Build, Locale, packet.Outbound, packet.Opcode);

            if (!Opcodes.Exists(op => op.Outbound == packet.Outbound && op.Header == packet.Opcode))
            {
                Opcodes.Add(new Opcode(packet.Outbound, packet.Opcode));
            }

            if (!forceAdd)
            {
                if (definition != null && !mViewIgnoredMenu.Checked && definition.Ignore)
                {
                    return;
                }
                if (packet.Outbound && !mViewOutboundMenu.Checked)
                {
                    return;
                }
                if (!packet.Outbound && !mViewInboundMenu.Checked)
                {
                    return;
                }
            }

            if (buffered)
            {
                bufferedPackets.Add(packet);
            }
            else
            {
                ListView.AddPacket(packet);
            }
        }
Example #3
0
        private Results ProcessPacket(byte[] bytes, bool isOutbound, DateTime timestamp)
        {
            if (mTerminated)
            {
                return(Results.Terminated);
            }

            if (Build == 0)
            {
                var packet = new ByteReader(bytes);
                packet.Read <ushort>(); // rawSeq
                int length = packet.ReadInt();
                if (bytes.Length - 6 < length)
                {
                    logger.Debug($"Connection on port {mLocalEndpoint} did not have a MapleStory2 Handshake");
                    return(Results.CloseMe);
                }

                ushort opcode = packet.Read <ushort>();
                if (opcode != 0x01)
                {
                    // RequestVersion
                    logger.Debug($"Connection on port {mLocalEndpoint} did not have a valid MapleStory2 Connection Header");
                    return(Results.CloseMe);
                }

                uint version = packet.Read <uint>();
                uint siv     = packet.Read <uint>();
                uint riv     = packet.Read <uint>();
                uint blockIV = packet.Read <uint>();
                byte type    = packet.ReadByte();

                Build  = version;
                Locale = MapleLocale.UNKNOWN;

                outDecryptor = new MapleCipher.Decryptor(Build, siv, blockIV);
                inDecryptor  = new MapleCipher.Decryptor(Build, riv, blockIV);

                inDecryptor.Decrypt(bytes); // Advance the IV

                // Generate HandShake packet
                Definition definition = Config.Instance.GetDefinition(Build, Locale, false, opcode);
                if (definition == null)
                {
                    definition = new Definition {
                        Outbound = false,
                        Opcode   = opcode,
                        Name     = "RequestVersion",
                    };
                    SaveDefinition(Locale, Build, definition);
                }

                ArraySegment <byte> segment = new ArraySegment <byte>(packet.Buffer);
                var maplePacket             = new MaplePacket(timestamp, isOutbound, Build, opcode, segment);
                // Add to list of not exist (TODO: SortedSet?)
                if (!Opcodes.Exists(op => op.Outbound == maplePacket.Outbound && op.Header == maplePacket.Opcode))
                {
                    // Should be false, but w/e
                    Opcodes.Add(new Opcode(maplePacket.Outbound, maplePacket.Opcode));
                }

                AddPacket(maplePacket, false, true);

                logger.Info($"[CONNECTION] {mRemoteEndpoint} <-> {mLocalEndpoint}: MapleStory2 V{Build}");

                return(Results.Show);
            }

            try {
                MapleCipher.Decryptor decryptor = isOutbound ? outDecryptor : inDecryptor;
                ByteReader            packet    = decryptor.Decrypt(bytes);
                // It's possible to get an empty packet, just ignore it.
                // Decryption is still necessary to advance sequence number.
                if (packet.Available == 0)
                {
                    return(Results.Continue);
                }

                ushort opcode = packet.Peek <ushort>();
                ArraySegment <byte> segment = new ArraySegment <byte>(packet.Buffer, 2, packet.Length - 2);
                var maplePacket             = new MaplePacket(timestamp, isOutbound, Build, opcode, segment);
                AddPacket(maplePacket);

                return(Results.Continue);
            } catch (ArgumentException ex) {
                logger.Fatal(ex, "Exception while processing packets");
                return(Results.CloseMe);
            }
        }