private void ADDOS_function(ref TClient arg) { int timeout = 5; while (true) { try { Thread.Sleep(timeout * 100); //timeout*seconds int rec_d = arg.received_data; Interlocked.Add(ref treceived_data, rec_d); if (rec_d >= (1024 * timeout)) //512 bytes /sec { blacklist.Add(GetIP(arg.sock)); Console.WriteLine("cdc::" + arg.session + " s.t: " + (rec_d / 1024) + "kbytes "); CloseSocket(arg); break; } else { lock (locker) arg.received_data = 0; } } catch (Exception ex) { BaseAccessors.BALog.Log(ex.Message); break; } } #endregion AntiDDOS }
/// <summary> /// accepts incoming connections /// </summary> private void AcceptSockets() { blacklist = new List<String>(); Console.WriteLine("WAITING FOR CONNECTIONS..."); while (!stop) { //wait 1ms (to avoid DDOS on connecting and disconnecting multiple clients and overloading CPU) Thread.Sleep(10); //Max connections if (LConnected_clients.Count >= maxConnections) { //wait 500ms (to avoid looping the while and overloading the CPU) Thread.Sleep(500); goto endline; } TClient SClient = new TClient(listener.AcceptSocket()); //Blocks unwanted connections if (!blacklist.Contains(GetIP(SClient.sock))) { //Creates session SClient.session = SessionFactory.GenerateSession(); //Packet Receiving Task receivesocket_t = new Task(() => ReceivePackets(ref SClient)); receivesocket_t.Start(); //AntiDDOS Task function_t = new Task(() => ADDOS_function(ref SClient)); function_t.Start(); lock (locker) LConnected_clients.Add(SClient); BaseAccessors.BALog.Log("new connection: " + SClient.session + " :: " + SClient.sock.RemoteEndPoint); } //end line (empty code) endline:; } }
public int GetClientIndex(TClient client) { return LConnected_clients.IndexOf(client); }
public void StopReceivePackets(TClient arg) { arg.stop = true; }
public void CloseSocket(TClient arg) { try { arg.stop = true; lock (locker) LConnected_clients.Remove(arg); arg.sock.Close(); } catch { } }
/// <summary> /// receive data for a connection /// </summary> private void ReceivePackets(ref TClient SClient) { SClient.handler = new PacketHandler(ref SClient); lock (locker) total_clients++; { while (!SClient.stop) { //Security sleep (Anti CPU overload), adds 10ms to packet transfer time :P Thread.Sleep(15); //variable definitions byte[] data = new byte[256]; //posible size change (maximum data per stream read) int length; //real received data size try { length = SClient.sock.Receive(data); } catch { CloseSocket(SClient); break; } if (length != 0) { SClient.handler.processPacket(data, length); lock (locker) SClient.received_data += data.Length; } } } }
public PacketHandler(ref TClient arg_client) { ref_client = arg_client; ref_sock = arg_client.sock; }