Exemple #1
0
        private async Task ProcessIncoming(NetState ns)
        {
            var inPipe = ns.Connection.Transport.Input;

            while (true)
            {
                if (NetState.AsyncState.Paused)
                {
                    continue;
                }

                try
                {
                    ReadResult result = await inPipe.ReadAsync();

                    if (result.IsCanceled || result.IsCompleted)
                    {
                        return;
                    }

                    ReadOnlySequence <byte> seq = result.Buffer;

                    if (seq.IsEmpty)
                    {
                        break;
                    }

                    int pos = PacketHandlers.ProcessPacket(_messagePumpService, ns, seq);

                    if (pos <= 0)
                    {
                        break;
                    }

                    inPipe.AdvanceTo(seq.Slice(0, pos).End);
                }
                catch
                {
                    // ignored
                }
            }

            inPipe.Complete();
        }
 public static void Initialize()
 {
     PacketHandlers.Register(0xBD, 0, true, new OnPacketReceive(ClientVersion));
 }
        public bool HandleReceive(NetState ns)
        {
            ByteQueue buffer = ns.Buffer;

            if (buffer == null || buffer.Length <= 0)
            {
                return(true);
            }

            lock ( buffer )
            {
                int length = buffer.Length;

                if (!ns.Seeded)
                {
                    if (buffer.Length >= 4)
                    {
                        buffer.Dequeue(m_Peek, 0, 4);

                        int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];

                        if (seed == 0)
                        {
                            Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
                            ns.Dispose();
                            return(false);
                        }

                        ns.m_Seed = seed;
                        ns.Seeded = true;

                        length = buffer.Length;
                    }
                    else
                    {
                        return(true);
                    }
                }

                while (length > 0 && ns.Running)
                {
                    int packetID = buffer.GetPacketID();

                    if (!ns.SentFirstPacket && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4)
                    {
                        Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", ns);
                        ns.Dispose();
                        break;
                    }

                    PacketHandler handler = PacketHandlers.GetHandler(packetID);

                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        new PacketReader(data, length, false).Trace(ns);

                        break;
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                ns.Dispose();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length >= packetLength)
                    {
                        if (handler.Ingame && ns.Mobile == null)
                        {
                            Console.WriteLine("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns, packetID);
                            ns.Dispose();
                            break;
                        }
                        else if (handler.Ingame && ns.Mobile.Deleted)
                        {
                            ns.Dispose();
                            break;
                        }
                        else
                        {
                            ThrottlePacketCallback throttler = handler.ThrottleCallback;

                            if (throttler != null && !throttler(ns))
                            {
                                m_Throttled.Enqueue(ns);
                                return(false);
                            }

                            PacketProfile profile = PacketProfile.GetIncomingProfile(packetID);
                            DateTime      start   = (profile == null ? DateTime.MinValue : DateTime.Now);

                            byte[] packetBuffer;

                            if (BufferSize >= packetLength)
                            {
                                packetBuffer = m_Buffers.AcquireBuffer();
                            }
                            else
                            {
                                packetBuffer = new byte[packetLength];
                            }

                            packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                            PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                            handler.OnReceive(ns, r);
                            length = buffer.Length;

                            if (BufferSize >= packetLength)
                            {
                                m_Buffers.ReleaseBuffer(packetBuffer);
                            }

                            if (profile != null)
                            {
                                profile.Record(packetLength, DateTime.Now - start);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(true);
        }
Exemple #4
0
 public static void Initialize()
 {
     PacketHandlers.RegisterEncoded(0x1E, true, new OnEncodedPacketReceive(EquipLastWeaponRequest));
 }
Exemple #5
0
 public GameServer(NetServer server, PacketHandlers handlers)
 {
     m_Server   = server;
     m_Handlers = handlers;
 }
 public static void Initialize()
 {
     PacketHandlers.RegisterExtended(0x2C, true, new OnPacketReceive(BandageRequest));
 }
Exemple #7
0
        private void OnReceived(UOSocket state, ByteQueue buffer, out bool throttle)
        {
            throttle = false;
            NetState client;

            if (!m_Clients.TryGetValue(state, out client))
            {
                log.Error("Inconsistent game server state: game client for {0} not found", state);
                return;
            }

            if (!client.Seeded)
            {
                if (buffer.GetPacketID() == 0xEF)
                {
                    client.Seeded = true;
                }
                else if (buffer.Length >= 4)
                {
                    var peek = new byte[4];
                    buffer.Dequeue(peek, 0, 4);

                    var seed = (peek[0] << 24) | (peek[1] << 16) | (peek[2] << 8) | peek[3];

                    if (seed == 0)
                    {
                        log.Info("Login: {0}: Invalid client detected, disconnecting", client);
                        client.Dispose();

                        return;
                    }

                    client.m_Seed = seed;
                    client.Seeded = true;
                }
                else
                {
                    return;                     // Need at least 4 bytes for the seed
                }
            }

            var length = buffer.Length;

            while (length > 0 && buffer.Length > 0)
            {
                int packetID = buffer.GetPacketID();

                if (!client.SentFirstPacket && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4 && packetID != 0xEF)
                {
                    log.Info("Client: {0}: Encrypted client detected, disconnecting", client);
                    client.Dispose();
                    return;
                }

                var handler = PacketHandlers.GetHandler(packetID);

                if (handler == null)
                {
                    var data = new byte[length];
                    length = buffer.Dequeue(data, 0, length);

                    if (Core.Logging)
                    {
                        var reader = PacketReader.CreateInstance(data, length, false);
                        reader.Trace(client);
                        PacketReader.ReleaseInstance(reader);
                    }

                    return;
                }

                var packetLength = handler.Length;

                if (packetLength == 0)
                {
                    // Dynamic length packet. Need at leaset 3 bytes (1 packet cmd + 2 length)

                    if (length >= 3)
                    {
                        packetLength = buffer.GetPacketLength();

                        if (packetLength < 3)
                        {
                            client.Dispose();
                            return;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                if (length >= packetLength)
                {
                    if (handler.Ingame && client.Mobile == null)
                    {
                        log.Info("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", client, packetID);
                        client.Dispose();
                        return;
                    }
                    else if (handler.Ingame && client.Mobile.Deleted)
                    {
                        client.Dispose();
                        return;
                    }
                    else
                    {
                        var throttler = handler.ThrottleCallback;

                        if (throttler != null && !throttler(client))
                        {
                            throttle = true;
                            return;
                        }

                        var profile = PacketProfile.GetIncomingProfile(packetID);
                        var start   = (profile == null ? DateTime.MinValue : DateTime.UtcNow);

                        byte[] packetBuffer;

                        if (BufferSize >= packetLength)
                        {
                            packetBuffer = m_Buffers.AcquireBuffer();
                        }
                        else
                        {
                            packetBuffer = new byte[packetLength];
                        }

                        packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                        var reader = PacketReader.CreateInstance(packetBuffer, packetLength, handler.Length != 0);

                        try
                        {
                            handler.OnReceive(client, reader);
                        }
                        catch (Exception e)
                        {
                            log.Error("Exception disarmed in HandleReceive from {0}: {1}", client.Address, e);
                        }

                        PacketReader.ReleaseInstance(reader);

                        length = buffer.Length;

                        if (BufferSize >= packetLength)
                        {
                            m_Buffers.ReleaseBuffer(packetBuffer);
                        }

                        if (profile != null)
                        {
                            profile.Record(packetLength, DateTime.UtcNow - start);
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
Exemple #8
0
 public static void Initialize()
 {
     PacketHandlers.RegisterExtended(0x33, true, new OnPacketReceive(MultiMouseMovementRequest));
 }
Exemple #9
0
 public static void Initialize()
 {
     PacketHandlers.Register(108, 19, true, new OnPacketReceive(AntiEUOTargeting));
 }
Exemple #10
0
 public PacketHandler GetHandler(int packetID)
 {
     return(PacketHandlers.GetHandler(packetID));
 }
Exemple #11
0
        public bool HandleReceive(NetState ns)
        {
            lock ( ns )
            {
                ByteQueue buffer = ns.Buffer;

                if (buffer == null)
                {
                    return(true);
                }

                int length = buffer.Length;

                if (!ns.Seeded)
                {
                    if (length >= 4)
                    {
                        buffer.Dequeue(m_Peek, 0, 4);

                        int seed = (m_Peek[0] << 24) | (m_Peek[1] << 16) | (m_Peek[2] << 8) | m_Peek[3];

                        //Console.WriteLine( "Login: {0}: Seed is 0x{1:X8}", ns, seed );

                        if (seed == 0)
                        {
                            log.Warn(String.Format("Login: {0}: Invalid client detected, disconnecting", ns));
                            ns.Dispose();
                            return(false);
                        }

                        ns.m_Seed = seed;
                        ns.Seeded = true;
                    }

                    return(true);
                }

                //Console.WriteLine( "{" );

                while (length > 0 && ns.Running)
                {
                    int packetID = buffer.GetPacketID();

                    if (!ns.SentFirstPacket && packetID != 0xF1 && packetID != 0xCF && packetID != 0x80 && packetID != 0x91 && packetID != 0xA4 && packetID != 0xBF)
                    {
                        log.Warn(String.Format("Client: {0}: Encrypted client detected, disconnecting", ns));
                        ns.Dispose();
                        break;
                    }

                    PacketHandler handler = PacketHandlers.GetHandler(packetID);

                    if (handler == null)
                    {
                        byte[] data = new byte[length];
                        length = buffer.Dequeue(data, 0, length);

                        new PacketReader(data, length, false).Trace(ns);

                        break;
                    }

                    int packetLength = handler.Length;

                    if (packetLength <= 0)
                    {
                        if (length >= 3)
                        {
                            packetLength = buffer.GetPacketLength();

                            if (packetLength < 3)
                            {
                                ns.Dispose();
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (length >= packetLength)
                    {
                        if (handler.Ingame && ns.Mobile == null)
                        {
                            log.Warn(String.Format("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns, packetID));
                            ns.Dispose();
                            break;
                        }
                        else if (handler.Ingame && ns.Mobile.Deleted)
                        {
                            ns.Dispose();
                            break;
                        }
                        else
                        {
                            ThrottlePacketCallback throttler = handler.ThrottleCallback;

                            if (throttler != null && !throttler(ns))
                            {
                                m_Throttled.Enqueue(ns);
                                //Console.WriteLine( "}" );
                                return(false);
                            }

                            //Console.WriteLine( handler.OnReceive.Method.Name );

                            PacketProfile profile = PacketProfile.GetIncomingProfile(packetID);
                            DateTime      start   = (profile == null ? DateTime.MinValue : DateTime.Now);

                            byte[] packetBuffer;

                            if (BufferSize >= packetLength)
                            {
                                packetBuffer = m_Buffers.AquireBuffer();
                            }
                            else
                            {
                                packetBuffer = new byte[packetLength];
                            }

                            packetLength = buffer.Dequeue(packetBuffer, 0, packetLength);

                            PacketReader r = new PacketReader(packetBuffer, packetLength, handler.Length != 0);

                            try {
                                handler.OnReceive(ns, r);
                            } catch (Exception e) {
                                log.Fatal(String.Format("Exception disarmed in HandleReceive from {0}",
                                                        ns.Address), e);
                            }

                            length = buffer.Length;

                            if (BufferSize >= packetLength)
                            {
                                m_Buffers.ReleaseBuffer(packetBuffer);
                            }

                            if (profile != null)
                            {
                                profile.Record(packetLength, DateTime.Now - start);
                            }

                            //Console.WriteLine( "Client: {0}: Unhandled packet 0x{1:X2}", ns, packetID );
                            //Utility.FormatBuffer( Console.Out, new System.IO.MemoryStream( r.Buffer ), r.Buffer.Length );
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                //Console.WriteLine( "}" );
            }

            return(true);
        }
Exemple #12
0
 public GameServer( NetServer server, PacketHandlers handlers )
 {
     m_Server = server;
     m_Handlers = handlers;
 }
Exemple #13
0
        public bool HandleReceive(NetState ns)
        {
            ByteQueue     queue1;
            int           num1;
            int           num2;
            int           num3;
            PacketHandler handler1;

            byte[] buffer1;
            int    num4;
            ThrottlePacketCallback callback1;
            PacketProfile          profile1;
            DateTime     time1;
            PacketReader reader1;
            NetState     state1 = ns;

            lock (ns)
            {
                queue1 = ns.Buffer;
                if (queue1 == null)
                {
                    return(true);
                }
                num1 = queue1.Length;
                if (ns.Seeded)
                {
                    goto Label_0250;
                }
                if (num1 >= 4)
                {
                    queue1.Dequeue(this.m_Peek, 0, 4);
                    num2 = ((((this.m_Peek[0] << 24) | (this.m_Peek[1] << 16)) | (this.m_Peek[2] << 8)) | this.m_Peek[3]);
                    if (num2 == 0)
                    {
                        Console.WriteLine("Login: {0}: Invalid client detected, disconnecting", ns);
                        ns.Dispose();
                        return(false);
                    }
                    ns.m_Seed = num2;
                    ns.Seeded = true;
                }
                return(true);

Label_009D:
                queue1.Peek(this.m_Peek, 0, 1);
                num3 = this.m_Peek[0];
                if (((!ns.SentFirstPacket && (num3 != 241)) && ((num3 != 207) && (num3 != 128))) && ((num3 != 145) && (num3 != 164)))
                {
                    Console.WriteLine("Client: {0}: Encrypted client detected, disconnecting", ns);
                    ns.Dispose();
                    goto Label_0269;
                }
                handler1 = PacketHandlers.GetHandler(num3);
                if (handler1 == null)
                {
                    buffer1 = queue1.Dequeue(num1);
                    new PacketReader(buffer1, 0).Trace(ns);
                    goto Label_0269;
                }
                num4 = handler1.Length;
                if (num4 <= 0)
                {
                    if (num1 < 3)
                    {
                        goto Label_0269;
                    }
                    queue1.Peek(this.m_Peek, 0, 3);
                    num4 = ((this.m_Peek[1] << 8) | this.m_Peek[2]);
                    if (num4 < 3)
                    {
                        ns.Dispose();
                        goto Label_0269;
                    }
                }
                if (num1 < num4)
                {
                    goto Label_0269;
                }
                if (handler1.Ingame && (ns.Mobile == null))
                {
                    Console.WriteLine("Client: {0}: Sent ingame packet (0x{1:X2}) before having been attached to a mobile", ns, num3);
                    ns.Dispose();
                    goto Label_0269;
                }
                if (handler1.Ingame && ns.Mobile.Deleted)
                {
                    ns.Dispose();
                    goto Label_0269;
                }
                callback1 = handler1.ThrottleCallback;
                if ((callback1 != null) && !callback1.Invoke(ns))
                {
                    this.m_Throttled.Enqueue(ns);
                    return(false);
                }
                profile1 = PacketProfile.GetIncomingProfile(num3);
                time1    = ((profile1 == null) ? DateTime.MinValue : DateTime.Now);
                reader1  = new PacketReader(queue1.Dequeue(num4), (handler1.Length != 0));
                handler1.OnReceive(ns, reader1);
                num1 = queue1.Length;
                if (profile1 != null)
                {
                    profile1.Record(num4, ((TimeSpan)(DateTime.Now - time1)));
                }
Label_0250:
                if (num1 > 0)
                {
                    if (ns.Running)
                    {
                        goto Label_009D;
                    }
                }
            }
Label_0269:
            return(true);
        }
Exemple #14
0
 public static void Initialize()
 {
     PacketHandlers.RegisterEncoded(0x1E, true, EquipLastWeaponRequest);
 }