Exemplo n.º 1
0
        private void PacketReceived(object sender, PacketEventArgs e)
        {
            //check bans, then floods, a flood will cause a ban
            if (CheckBanned(e.RemoteEndPoint.Address) ||
                !CheckFloodCounter(e.RemoteEndPoint.Address))
            {
                return;
            }

            switch ((UdpId)e.Packet.Id)
            {
            case UdpId.OP_SERVERLIST_ACKINFO: {
                AckInfo info = (AckInfo)e.Packet;

                AckInfoHits++;

                // sent to client from server...
                // add channel list support for server?

                ParseServers(info.Servers);
            }
            break;

            case UdpId.OP_SERVERLIST_ADDIPS: {
                AddIps       add    = (AddIps)e.Packet;
                ServerRecord server = null;

                lock (servers) {
                    server = (ServerRecord)servers.Find((s) => s.ExternalIp.Equals(e.RemoteEndPoint.Address));

                    if (server != null)
                    {
                        server.Port = add.Port;
                    }
                    else
                    {
                        server = new ServerRecord(e.RemoteEndPoint.Address, add.Port);
                        servers.Add(server);
                    }
                }

                uint num = 6;
                ParseServers(add.Servers);

                socket.SendAsync(new AckIps()
                    {
                        Port    = server.Port,
                        Servers = GetSendServers(server.ExternalIp, ref num),
                    }, e.RemoteEndPoint);
            }
            break;

            case UdpId.OP_SERVERLIST_ACKIPS: {
                AckIps       ips    = (AckIps)e.Packet;
                ServerRecord server = null;

                AckIpHits++;

                lock (servers)
                    server = (ServerRecord)servers.Find((s) => s.ExternalIp.Equals(e.RemoteEndPoint.Address));

                if (server != null)
                {
                    server.Port = ips.Port;
                    server.AckReceived();
                }

                ParseServers(ips.Servers);
            }
            break;

            case UdpId.OP_SERVERLIST_CHECKFIREWALLBUSY: {
                CheckFirewallBusy busy = (CheckFirewallBusy)e.Packet;

                lock (myfirewalltests)
                    myfirewalltests.RemoveAll((s) => s.ExternalIp.Equals(e.RemoteEndPoint.Address));

                ParseServers(busy.Servers);
            }
            break;

            case UdpId.OP_SERVERLIST_PROCEEDCHECKFIREWALL: {
                CheckFirewall check = (CheckFirewall)e.Packet;
                FirewallTest  test  = null;

                lock (firewalltests)
                    test = firewalltests.Find(s => s.GetHashCode() == check.Cookie);

                if (test != null)
                {
                    test.RemoteEndPoint.Port = check.Port;
                    test.Begin();
                }
            }
            break;

            case UdpId.OP_SERVERLIST_READYTOCHECKFIREWALL: {
                CheckFirewallReady reader = (CheckFirewallReady)e.Packet;

                if (!reader.Target.IsLocal())
                {
                    ((AresServer)server).ExternalIp = reader.Target;

                    socket.SendAsync(new CheckFirewall()
                        {
                            Port   = (ushort)socket.LocalEndPoint.Port,
                            Cookie = reader.Cookie,
                        }, e.RemoteEndPoint);
                }
            }
            break;

            case UdpId.OP_SERVERLIST_WANTCHECKFIREWALL: {
                CheckFirewallWanted want = (CheckFirewallWanted)e.Packet;

                CheckFirewallHits++;

                if (firewalltests.Count < 5)
                {
                    FirewallTest test = new FirewallTest(e.RemoteEndPoint.Address, want.Port);

                    lock (firewalltests) firewalltests.Add(test);

                    socket.SendAsync(
                        new CheckFirewallReady((uint)test.GetHashCode(), e.RemoteEndPoint.Address),
                        e.RemoteEndPoint);
                }
                else
                {
                    uint num = 6;

                    socket.SendAsync(new CheckFirewallBusy()
                        {
                            Port    = (ushort)socket.LocalEndPoint.Port,
                            Servers = GetSendServers(e.RemoteEndPoint.Address, ref num)
                        }, e.RemoteEndPoint);
                }
            }
            break;

            case UdpId.OP_SERVERLIST_SENDINFO: {
                SendInfo sendInfo = (SendInfo)e.Packet;

                SendInfoHits++;

                AckInfo ackinfo = new AckInfo()
                {
                    Language = server.Config.Language,
                    Name     = server.Config.Name,
                    Topic    = server.Config.Topic,
                    Version  = Strings.VersionLogin,
                    Port     = (ushort)socket.LocalEndPoint.Port,
                    //Users = (ushort)server.Users.Count,
                    Users = (ushort)Math.Max(server.Users.Count, (server.Users.Count + new Random().Next(15, 20))),
                };

                uint num = 6;

                ackinfo.Servers    = GetSendServers(e.RemoteEndPoint.Address, ref num);
                ackinfo.ServersLen = (byte)num;

                socket.SendAsync(ackinfo, e.RemoteEndPoint);
            }
            break;

            case UdpId.OP_SERVERLIST_SENDNODES: {
                uint      num       = 20;
                SendNodes sendnodes = (SendNodes)e.Packet;

                AckNodes acknodes = new AckNodes()
                {
                    Port  = server.Config.Port,
                    Nodes = GetSendServers(e.RemoteEndPoint.Address, ref num)
                };

                socket.SendAsync(acknodes, e.RemoteEndPoint);
            }
            break;

            case UdpId.OP_SERVERLIST_ACKNODES: {
                AckNodes ackNodes = (AckNodes)e.Packet;
                ParseServers(ackNodes.Nodes);
            }
            break;
            }
        }