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; } }
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; } }