protected virtual bool CheckCounters(AresClient user, IPacket packet) { var rules = flood_rules.FindAll((s) => s.Id == packet.Id); foreach (var rule in rules) { FloodCounter counter = null; DateTime now = TimeBank.CurrentTime; if (!user.Counters.ContainsKey(packet.Id)) { counter = new FloodCounter(0, now); user.Counters.Add(packet.Id, counter); } else { counter = user.Counters[packet.Id]; if (now.Subtract(counter.Last).TotalMilliseconds > rule.ResetTimeout) { counter.Count = 0; } } if (++counter.Count >= rule.Count) { stats.FloodsTriggered++; if (!plugins.OnFlood(user, packet)) { return(false); } } else { counter.Last = now; } } return(true); }
protected virtual void ClientDisconnected(object sender, DisconnectEventArgs e) { AresClient user = null; var socket = sender as ISocket; if (socket == null) { return; } lock (pending) pending.RemoveAll((s) => s.Equals(socket)); lock (users.SyncRoot) { int uindex = users.FindIndex(s => s.Socket == sender); if (uindex == -1) { return; } user = (AresClient)users[uindex]; users.RemoveAt(uindex); } if (user.LoggedIn) { history.Add(user); SendPacket((s) => !s.Guid.Equals(user.Guid) && //ghost? s.Vroom == user.Vroom, new Parted(user.Name)); } plugins.OnPart(user, e.UserToken); idpool.Push(user.Id); user.Dispose(); }
protected virtual void ClientException(object sender, ExceptionEventArgs e) { AresClient user = null; lock (users.SyncRoot) user = users.Find(s => s.Socket == sender); if (user == null) { return; } Logging.WriteLines(new[] { "----------", string.Format("Exception occured in client: {0}", user.Name), string.Format("Message: {0}", e.Exception.Message), e.Exception.StackTrace, "----------" }); user.Disconnect(); }
public static int Create(AresClient client) { Random r = new Random(); int total = 40; int top = r.Next(1, 10); int emote = r.Next(0, emoticons.Length); int count = r.Next(5, 12); string tag = emoticons[emote]; string name = names[emote]; string noun = nouns[r.Next(0, nouns.Length)]; string end = ends[r.Next(0, ends.Length)]; string question = String.Format("How many {0} {1} {2}?", name, noun, end); int[] random = new int[count]; List <string> captcha = new List <string>(); for (int i = 0; i < count; i++) { int index = r.Next(0, total); while (random.Contains(index)) { index = r.Next(0, total); } random[i] = index; } client.SendPacket(new Announce("Welcome to the room " + client.Name)); client.SendPacket(new Announce("Please answer the following question:")); if (top < 5) { client.SendPacket(new Announce("")); client.SendPacket(new Announce(question)); } client.SendPacket(new Announce("")); StringBuilder sb = new StringBuilder(); int current = 0; for (int i = 0; i < total; i++) { if (random.Contains(i)) { sb.Append(emoticons[emote]); } else { int decoy = r.Next(0, emoticons.Length); while (decoy == emote) { decoy = r.Next(0, emoticons.Length); } sb.Append(emoticons[decoy]); } sb.Append(" "); if (++current >= 8) { client.SendPacket(new Announce(sb.ToString())); sb.Clear(); current = 0; } } if (current > 0) { client.SendPacket(new Announce(sb.ToString())); } if (top >= 5) { client.SendPacket(new Announce("")); client.SendPacket(new Announce(question)); } return(count); }
protected virtual void ClientPacketReceived(object sender, PacketEventArgs e) { stats.PacketsReceived++; stats.AddInput(e.Transferred); var socket = sender as ISocket; if (socket == null) { return; } AresClient user = null; lock (users.SyncRoot) user = users.Find(s => s.Socket == socket); if (user == null) { if ((AresId)e.Packet.Id == AresId.MSG_CHAT_CLIENT_LOGIN) { if (HandlePending(socket)) { int id = idpool.Pop(); user = new AresClient(this, socket, (ushort)id); if (user.ExternalIp.IsLocal() || user.ExternalIp.Equals(ExternalIp)) { user.LocalHost = true; } lock (users.SyncRoot) { users.Add(user); users.Sort(sorter); stats.PeakUsers = Math.Max(users.Count, stats.PeakUsers); } user.HandleJoin(e); } else { socket.Disconnect(); } } } else if ((AresId)e.Packet.Id == AresId.MSG_CHAT_CLIENT_LOGIN) { //sending too many login packets SendAnnounce(user, Errors.LoginFlood); user.Disconnect(); } else if (user.LoggedIn && CheckCounters(user, e.Packet)) { user.LastUpdate = TimeBank.CurrentTime; if (!user.HandlePacket(e) && plugins.OnBeforePacket(user, e.Packet)) { user.HandleOverridePacket(e); plugins.OnAfterPacket(user, e.Packet); } } }