Ejemplo n.º 1
0
        private AresChannel GetOldestContacted()
        {
            AresChannel oldest = null;

            if (Channels.Count == 0)
            {
                return(oldest);
            }

            DateTime now = DateTime.Now;

            foreach (var channel in Channels.ToArray())
            {
                if (Equals(ExternalIp, channel.ExternalIp) || channel.ExternalIp.IsLocalAreaNetwork())
                {
                    continue;
                }

                double last = now.Subtract(channel.LastSendIPs).TotalMinutes;
                double old  = now.Subtract(oldest?.LastSendIPs ?? now).TotalMinutes;

                if (last > 10 && last > old)
                {
                    oldest = channel;
                }
            }

            if (oldest != null)
            {
                oldest.TryCount++;
                oldest.LastSendIPs = DateTime.Now;
            }

            return(oldest);
        }
Ejemplo n.º 2
0
        private void OnTimer(object state)
        {
            if (running)
            {
                if (TestingFirewall)
                {
                    CheckFirewall();
                }

                else if (Listing)
                {
                    AresChannel tosend = GetOldestContacted();

                    if (tosend != null)
                    {
                        uint num = 6;
                        socket.SendAsync(
                            new AddIps(
                                (ushort)socket.LocalEndPoint.Port,
                                GetSendServers(tosend.ExternalIp, ref num)),
                            new IPEndPoint(tosend.ExternalIp, tosend.Port));
                    }

                    DateTime now = DateTime.Now;

                    if (now.Subtract(lastmars).TotalMinutes >= 10)
                    {
                        lastmars = now;
                        UpdateMars();
                        UpdateZorbo();
                    }

                    if (now.Subtract(lastexpire).TotalMinutes >= 1)
                    {
                        lastexpire = now;
                        PurgeOld();
                    }

                    if (now.Subtract(lastpurge).TotalMinutes >= 10)
                    {
                        lastpurge = now;
                        PurgeExceeding();
                    }
                }

                ExpireBans();
            }
        }
Ejemplo n.º 3
0
        private void CheckFirewall()
        {
            AresChannel channel = null;
            DateTime    now     = DateTime.Now;

            lock (Channels)
                channel = Channels.Find((s) => !s.ExternalIp.IsLocalAreaNetwork() && now.Subtract(s.LastAskedFirewall).TotalMinutes > 5);

            if (channel != null)
            {
                channel.LastAskedFirewall = now;
                IPAddress ip = channel.ExternalIp;

                lock (myfirewalltests) myfirewalltests.Add(channel);

                socket.SendAsync(
                    new CheckFirewallWanted(channel.Port),
                    new IPEndPoint(ip, channel.Port));
            }
            else
            {
                TestingFirewall = false;
            }
        }
Ejemplo n.º 4
0
        private void HandleUdpPacket(PacketEventArgs e)
        {
            if (CheckBanned(e.RemoteEndPoint.Address))
            {
                return;
            }

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

                AckInfoHits++;
                lastackinfo = DateTime.Now;

                AresChannel channel = FindChannel(e.RemoteEndPoint.Address, info.Port);

                if (channel == null)
                {
                    channel = FindLocalChannel(e.RemoteEndPoint.Address);
                }

                if (channel == null)
                {
                    return;
                }

                channel.Port     = info.Port;
                channel.Users    = info.Users;
                channel.Name     = info.Name;
                channel.Topic    = info.Topic;
                channel.Language = info.Language;
                channel.Version  = info.Version;

                ParseServers(info.Servers);
            }
            break;

            case AresUdpId.OP_SERVERLIST_ADDIPS: {
                AddIps add = (AddIps)e.Packet;

                AddIpHits++;

                AresChannel channel = FindChannel(e.RemoteEndPoint.Address, add.Port);

                if (channel != null)
                {
                    channel.Port = add.Port;
                }
                else
                {
                    var address = e.RemoteEndPoint.Address;

                    if (e.RemoteEndPoint.Address.IsLocalAreaNetwork())
                    {
                        address = ExternalIp ?? address;
                    }

                    channel = new AresChannel(address, add.Port);
                    Channels.Add(channel);
                }

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

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

            case AresUdpId.OP_SERVERLIST_ACKIPS: {
                AckIps ips = (AckIps)e.Packet;

                AckIpHits++;

                AresChannel channel = FindChannel(e.RemoteEndPoint.Address, ips.Port);

                if (channel != null)
                {
                    channel.Port = ips.Port;
                    channel.AckCount++;
                    channel.TryCount  = 0;
                    channel.LastAcked = DateTime.Now;
                }

                ParseServers(ips.Servers);
            }
            break;

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

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

                ParseServers(busy.Servers);
            }
            break;

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

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

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

            case AresUdpId.OP_SERVERLIST_READYTOCHECKFIREWALL: {
                CheckFirewallReady ready = (CheckFirewallReady)e.Packet;

                if (!ready.Target.IsLocalAreaNetwork())
                {
                    ExternalIp = ready.Target;
                    //Console.Write()
                    socket.SendAsync(new CheckFirewall()
                        {
                            Port  = (ushort)socket.LocalEndPoint.Port,
                            Token = ready.Token,
                        }, e.RemoteEndPoint);
                }
            }
            break;

            case AresUdpId.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 AresUdpId.OP_SERVERLIST_SENDINFO: {
                SendInfo sendInfo = (SendInfo)e.Packet;

                SendInfoHits++;

                if (Listing && Server != null)
                {
                    AckInfo ackinfo = new AckInfo()
                    {
                        Language = Server.Config.Language,
                        Name     = Server.Config.Name,
                        Topic    = Server.Config.Topic,
                        Version  = Strings.VersionChannels,
                        Port     = (ushort)socket.LocalEndPoint.Port,
                        Users    = (ushort)(Server.Users.Count + 1),
                    };

                    uint num = 6;

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

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