Jump to content

{Share-Java Code}DDoS Protection


Recommended Posts

Note: This does not invoke you to use any god damn firewall, or for you to do a lot of work. This is probably one of the ONLY ways you can fully stop SYI and any other DDoS'r until they learn how to send Packet 14.

 

If this gets locked, it gets locked. I don't know how long it'll take you to do it, and it's kind of hard to avoid c|p. At least you get one good thing out of it: No more SYI pissing us off.

 

 

Seriously guys, the shitty tutorials about firewalls and incorrectly blocking this and your lame-brain questions have got to stop.

Not sure if this was ever made before, but this is my version. I changed everything around to make it look a lot more readable, and removed unnecessary junk (useless comments, etc, etc).

 

This will: Correctly protect your server against DDoS attacks and deal with them right. We will NOT use any Firewalls, and instead use what you people should have been using.

 

Before you start, you should know: Well, you're going to have to work. You will have to add all of the handlers you use to the class provided here. Only ones added are the obvious ones: clientHandler and playerHandler.

 

Base: Any wL base. You probably use it, right?

 

This has been tested: Yes.. But not extensively. KEEP BACKUPS.

 

Alright, well there isnt a lot of stuff to do, so let's start.

 

Open the client class.

Now, search for:

 

if(inStream.readUnsignedByte() != 14) {

 

This will be found in your run method.

 

Comment or delete the block of code. It looks similar to:

 

if(inStream.readUnsignedByte() != 14) {
			shutdownError("Expected login Id 14 from client.");
			disconnected = true;
			return;
		}

 

This block will check if packet 14 exists, but of course when we're checking for the packet in the socket process it makes it mysteriously disappear. Big Grin

 

 

Now we have the fun part, we get to redo your server class! Make a backup of your server class, just incase. Smiley

Replace your WHOLE class with the following: (Yes, I did say REPLACE.)

 

/*
* server.java
* 
* Version 1.0a
*
* Date: Tuesday June 19th, 2008.
* 
* Author: M@n80$
*
*  Correctly blocking SYI, and useless **** taken out.
*/

import java.sql.*;
import java.io.*;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class server implements Runnable {

public server() { }

public static void main(java.lang.String args[]) {
	clientHandler = new server();
	(new Thread(clientHandler)).start();
	playerHandler = new PlayerHandler();
	int waitFails = 0;
	long lastTicks = System.currentTimeMillis();
	long totalTimeSpentProcessing = 0;
	int cycle = 0;
	while(!shutdownServer) {
		if(updateServer)
			calcTime();
		playerHandler.process();
		System.gc();
		long timeSpent = System.currentTimeMillis() - lastTicks;
		totalTimeSpentProcessing += timeSpent;
		if(timeSpent >= cycleTime) {
			timeSpent = cycleTime;
			if(++waitFails > 100) {
				misc.println("[KERNEL]: machine is too slow to run this server!");
			}
		}
		try {
			Thread.sleep(cycleTime-timeSpent);
		} catch(java.lang.Exception _ex) { }
		lastTicks = System.currentTimeMillis();
		cycle++;
		if(cycle % 100 == 0) {
			float time = ((float)totalTimeSpentProcessing)/cycle;
		}
		if (cycle % 3600 == 0) {
			System.gc();
		}
		if (ShutDown == true) {
			if (ShutDownCounter >= 100) {
				shutdownServer = true;
			}
			ShutDownCounter++;
		}
	}
	playerHandler.destruct();
	clientHandler.killServer();
	clientHandler = null;
}

public void run() {
	try {
		shutdownClientHandler = false;
		clientListener = new java.net.ServerSocket(serverListenerPort, 1, null);
		misc.println("Running server on port: "+serverListenerPort);
		misc.println("     Server class revamped by Hidendra.");
		while(true) {
			Socket s = clientListener.accept();
			s.setTcpNoDelay(true);
			String connectingHost = s.getInetAddress().getHostName();
			int type = 0;
			type = s.getInputStream().read();
			if(clientListener != null) {
				int Found = -1;
				for (int i = 0; i < MaxConnections; i++) {
					if (Connections[i] == connectingHost) {
						Found = ConnectionCount[i];
						break;
					}
				}
				if (Found < 3) {
					if(type == 14) {
						misc.println("ClientHandler: Accepted from "+connectingHost+":"+s.getPort());
						playerHandler.newPlayerClient(s, connectingHost);
					} else {
						// if you want to do anything for INVALID clients, add it here.
						//  don't just ipban them box-side (ie: iptables).. 
						//  because possible connections include, but not limited to:
						//    - DDoS'ers / SYI
						//    - Server Status checkers (Mopar's, ETC!)
						PrintWriter out = new PrintWriter(s.getOutputStream());
						out.println("HTTP/1.0 200 OK");
						out.println("Content-Type: text/html");
						out.println("Server: Bot");
						out.println("");
						out.println("You are connecting to this private server VIA a web browser. <br/> <br/>   Please connect using a <b>VALID</b> client, like MoparScape. <br/>Thank you!");
						out.flush();
						s.close();
					}
				}
			}
		}
	} catch(java.io.IOException ioe) {
		if(!shutdownClientHandler) {
			misc.println("[FATAL]: Port: "+serverListenerPort+" already in use?");
		} else {
			misc.println("ClientHandler was shut down.");
		}
	}
}

public static void calcTime() {
	long curTime = System.currentTimeMillis();
	updateSeconds = 180 - ((int)(curTime - startTime) / 1000);
	if(updateSeconds == 0) {
		shutdownServer = true;
	}
}

public void killServer() {
	try {
		shutdownClientHandler = true;
		if(clientListener != null) clientListener.close();
		clientListener = null;
	} catch(java.lang.Exception __ex) {
		__ex.printStackTrace();
	}
}

public static int EnergyRegian = 60;
public static int MaxConnections = 999000;
public static String[] Connections = new String[MaxConnections];
public static int[] ConnectionCount = new int[MaxConnections];
public static boolean ShutDown = false;
public static int ShutDownCounter = 0;
public static final int cycleTime = 500;
public static boolean updateServer = false;
public static int updateSeconds = 180;
public static long startTime;
public static Connection connection = null;
public static server clientHandler = null;
public static java.net.ServerSocket clientListener = null;
public static boolean shutdownServer = false;
public static boolean shutdownClientHandler;
public static int serverListenerPort = 43594;
public static PlayerHandler playerHandler = null;


}

 

Now, it should work.. You shouldn't be invadedz by nulls and weird connecting from..'s

I added a sorta-cool twist to the server class, when your server is running, in a web browser type http://localhostort in your address bar, where port is the port of the server, IE 43594.. I implemented a quick webserver to send that information, you can easily change or remove it by looking at the code. Smiley

 

Not really much of a webserver, just a 5 second thing xD.

 

 

Also, you will have to add your handlers yourself, including the public static <class> <othername> yourself, and the new <class>();

No, I am not doing it for you. If you want DDoS protection, then you're going to have to work for it.

 

If you have any questions, please post them here and I will of course try to answer them as best I can..

Cheers!

 

 

AND I DO REALIZE YOU CAN JUST USE FIREWALL VOIDS I JUST FIND THIS EASIER!

Link to comment
Share on other sites

Nice !

 

One question : where's the DDoS protection ?

 

jajajaja LOOOOOOOOOOL can't stop laughing xD :P

BTW if someone wants to dosS you then nothing can save you from the anger of a powerful hacker ;o

 

 

Link to comment
Share on other sites

jajajaja LOOOOOOOOOOL can't stop laughing xD :P

BTW if someone wants to dosS you then nothing can save you from the anger of a powerful hacker ;o

 

 

 

You can always unplug the PC and run around with your arms in the air, may not save you, but it'll be really fun to watch, you may even become an Internet meme.

Link to comment
Share on other sites

jajajaja LOOOOOOOOOOL can't stop laughing xD :P

BTW if someone wants to dosS you then nothing can save you from the anger of a powerful hacker ;o

 

 

Oh God.
Link to comment
Share on other sites

You can always unplug the PC and run around with your arms in the air, may not save you, but it'll be really fun to watch, you may even become an Internet meme.

I laughed hard to this one. I will be behind with a camera to take photo and post on facebook if that happens.

 

CLICK ME

 

Trolollololololol. H4Xl33t.

 

Locked.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Posts

    • L2 Insignia High Five 20x MID SERVER FOCUSED ON OLYMPIAD | PVP | AUTOFARM       OPEN BETA TEST SERVER 3 MAY 2024   GRAND OPENING 10 MAY 2024    Rates: 📜 XP/SP 20x | Spoil 15x | Drop 10x | Adena 10x 📜     Server Features: 🔥 No Olf-T Shirt, No big over-enchant, No over-power Donate 🔥 🔥 VIP Gold Color Chat, Unique Olympiad Extra Points Engine, GvG Event 🔥 🔥 Auto-Farm, LoA and DV scheduled PvP Zone, Calendar Daily Reward 🔥 🔥 Castle Instance, Solo Instance, PvP Solo Rift, Dress me system, Adena Boxes 🔥        Website: https://www.l2insignia.com  Discord: https://discord.com/invite/yEgsrHn2hQ      
    • I am selling the essence project which includes versions 388 and 439 that have been running for over 2 years or (447 as custom PVP like Pride). I have a test server for you to test them out. If you are really interested in it then contact my seller at discord: kiwi7106. Price: 4000 Euro P/s: This is a project that I have spent a lot of money and time developing, so if you are not interested in it, please get out of this topic, thank you. P/s 2: If you find the price too expensive, it's best to skip this article and find another project and don't comment negatively on my topic, thank you.
    • Someone ask me for this, it should work on any client that has Kamael race, preview:     Installation - there are two ways to install depending on how you want to use it:   Method 1: If you want to completely replace the original, do:   Copy all lines from your armorgrp to Notepad++, press Ctrl+H, check the "match whole word" option and replace:   kamael.Mkamael_m000_w_ad00   by:   AvengersKamaelWings.Avengers_MKamael_m001_w_ad00   Then replace:   MKamael.Mkamael_m000_t00_w   by:   AvengersKamaelWings.MKamael_m001_t00_w   Now repeat the same process with the female, replace:   kamael.Fkamael_m000_w_ad00   by:   AvengersKamaelWings.Avengers_FKamael_m001_w_ad00   Then replace:   FKamael.Fkamael_m000_t00_w   by:   AvengersKamaelWings.FKamael_m001_t00_w   You're done, paste everything back into File Edit and save!   Method 2: If you only want to replace in specific sets, execute the above process only on the armorgrp of those sets.   Repack by: AvengersTeamBr Password: LadrãoDeFrango      
  • Topics

×
×
  • Create New...