Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tons of documentation and cleaned up some code #40

Merged
merged 12 commits into from
Nov 30, 2023
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 19
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '19'
java-version: '21'
distribution: 'temurin'
cache: maven
- name: Build with Maven
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up JDK 19
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: "19"
java-version: "21"
distribution: "temurin"
cache: maven
- name: Build with Maven
Expand Down
28 changes: 11 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<properties>
<maven.compiler.target>19</maven.compiler.target>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<groupId>me.replydev</groupId>
<artifactId>quboscanner</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<repositories>
<repository>
Expand All @@ -27,7 +27,7 @@
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.2</version>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -38,28 +38,28 @@
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>5.3.4</version>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>slf4j-tinylog</artifactId>
<version>2.6.0</version>
<version>2.7.0-M1</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.6.0</version>
<version>2.7.0-M1</version>
</dependency>
<dependency>
<groupId>com.github.replydev</groupId>
Expand All @@ -77,7 +77,7 @@
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
Expand All @@ -95,13 +95,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>19</release>
<compilerArgs>--enable-preview</compilerArgs>
<source>19</source>
<target>19</target>
</configuration>
<version>3.11.0</version>
</plugin>
</plugins>
</build>
Expand Down
80 changes: 39 additions & 41 deletions src/main/java/me/replydev/qubo/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,19 @@
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

/**
* The CLI class handles the command line interface functionality, including
* initializing the application, printing the logo, and starting the standard
* run process.
* @author ReplyDev
*/
@Slf4j
@UtilityClass
public class CLI {

private QuboInstance quboInstance;
private static final String VERSION = "1.1.0";

void init(String[] args) {
printLogo();
checkEncodingParameter();
standardRun(args);

log.info(
"Scan terminated - " +
quboInstance.getFoundServers().get() +
" (" +
quboInstance.getUnfilteredFoundServers().get() +
" in total)"
);
System.exit(0);
}

private void checkEncodingParameter() {
if (!isUTF8Mode()) {
log.info("The scanner isn't running in UTF-8 mode!");
log.info(
"Put \"-Dfile.encoding=UTF-8\" in JVM args in order to run the program correctly!"
);
System.exit(-1);
}
}

private void printLogo() {
log.info(
String.format(
"""
private static final String LOGO_TEMPLATE = """
____ _ _____ \s
/ __ \\ | | / ____| \s
| | | |_ _| |__ ___| (___ ___ __ _ _ __ _ __ ___ _ __\s
Expand All @@ -49,24 +27,44 @@ private void printLogo() {

By @replydev on Telegram
Version %s
""",
Info.VERSION
)
""";

private QuboInstance quboInstance;

/**
* Initializes the application with given command line arguments.
* @param args The command line arguments.
*/
void init(String[] args) {
printLogo();
standardRun(args);

log.info("Scan terminated - {} ({}) in total",
quboInstance.getFoundServers().get(),
quboInstance.getUnfilteredFoundServers().get()
);
System.exit(0);
}

/**
* Prints the application logo with the current version.
*/
private void printLogo() {
log.info(LOGO_TEMPLATE.replace("%s", VERSION));
}

/**
* Executes the standard application run process.
* @param args The command line arguments.
*/
private void standardRun(String[] args) {
CommandLineArgs commandLineArgs = new CommandLineArgs(args);
quboInstance = new QuboInstance(commandLineArgs);
try {
CommandLineArgs commandLineArgs = new CommandLineArgs(args);
quboInstance = new QuboInstance(commandLineArgs);
quboInstance.run();
} catch (NumberFormatException e) {
commandLineArgs.showHelpAndExit();
log.error("There was an error parsing the numbers.", e);
new CommandLineArgs(args).showHelpAndExit();
}
}

private boolean isUTF8Mode() {
List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
return arguments.contains("-Dfile.encoding=UTF-8");
}
}
100 changes: 43 additions & 57 deletions src/main/java/me/replydev/qubo/CommandLineArgs.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package me.replydev.qubo;

import lombok.Getter;
import lombok.Setter;
import lombok.Value;
import lombok.experimental.NonFinal;
import me.replydev.utils.IpList;
import me.replydev.utils.PortList;
import me.replydev.utils.SearchFilter;
import org.apache.commons.cli.*;

@Value
/**
* This class is responsible for parsing the command line arguments.
* @author ReplyDev, Swofty
*/
@Getter
public class CommandLineArgs {

Options options;
IpList ipList;
PortList portRange;
boolean skipCommon;
int timeout;
SearchFilter searchFilter;
int count;

@NonFinal
CommandLine cmd;

private final Options options;
private final IpList ipList;
private final PortList portRange;
private final boolean skipCommon;
private final int timeout;
private final SearchFilter searchFilter;
private final int count;

@Setter
private CommandLine cmd;

/**
* Constructor for CommandLineArgs.
* @param command The array of command line arguments to be parsed.
* @throws NumberFormatException If parsing of numeric values fails.
*/
public CommandLineArgs(String[] command) throws NumberFormatException {
options = buildOptions();
CommandLineParser parser = new DefaultParser();
Expand All @@ -32,7 +42,7 @@ public CommandLineArgs(String[] command) throws NumberFormatException {
ipList = new IpList(cmd.getOptionValue("i"));
portRange = new PortList(cmd.getOptionValue("p"));
skipCommon = !cmd.hasOption("all");
timeout = Integer.parseInt(cmd.getOptionValue("t"));
timeout = Integer.parseInt(cmd.getOptionValue("t", "1000"));

searchFilter =
SearchFilter
Expand All @@ -45,57 +55,33 @@ public CommandLineArgs(String[] command) throws NumberFormatException {
count = Integer.parseInt(cmd.getOptionValue("c", "1"));
}

/**
* Builds the command line options.
* @see Options
* @return Options The command line options.
*/
private static Options buildOptions() {
Option iprange = new Option("i", "iprange", true, "The IP range to scan");
iprange.setRequired(true);

Option portrange = new Option("p", "portrange", true, "The range of ports to scan");
portrange.setRequired(true);

Option timeout = new Option("t", "timeout", true, "TCP connection timeout");
timeout.setRequired(true);

Option count = new Option("c", "pingcount", true, "Number of ping retries");
count.setRequired(false);

Option all = new Option("a", false, "Force Qubo to scan broadcast IPs and common ports");
all.setRequired(false);

Option filterVersion = new Option(
"v",
"filterversion",
true,
"Show only hits with given version"
);
filterVersion.setRequired(false);

Option filterMotd = new Option("m", "filtermotd", true, "Show only hits with given motd");
filterMotd.setRequired(false);

Option filterOn = new Option(
"o",
"minonline",
true,
"Show only hits with at least <arg> players online"
);
filterOn.setRequired(false);

Options options = new Options();
options.addOption(iprange);
options.addOption(portrange);
options.addOption(timeout);
options.addOption(count);
options.addOption(all);
options.addOption(filterVersion);
options.addOption(filterMotd);
options.addOption(filterOn);

options.addRequiredOption("i", "iprange", true, "The IP range to scan");
options.addRequiredOption("p", "portrange", true, "The range of ports to scan");
options.addOption("t", "timeout", true, "TCP connection timeout");
options.addOption("c", "pingcount", true, "Number of ping retries");
options.addOption("a", "all", false, "Force to scan broadcast IPs and common ports");
options.addOption("v", "filterversion", true, "Show only hits with given version");
options.addOption("m", "filtermotd", true, "Show only hits with given motd");
options.addOption("o", "minonline", true, "Show only hits with at least <arg> players online");

return options;
}

/**
* Prints help information using the command line options and exits the program
* with exit code -1.
*/
public void showHelpAndExit() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("-range <arg> -ports <arg> -th <arg> -ti <arg>", options);
formatter.printHelp("Usage: -i <iprange> -p <portrange> -t <timeout> [-c <pingcount>] [...]", options);
System.exit(-1);
}
}
9 changes: 0 additions & 9 deletions src/main/java/me/replydev/qubo/Info.java

This file was deleted.

12 changes: 11 additions & 1 deletion src/main/java/me/replydev/qubo/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package me.replydev.qubo;

/**
* The Main class is the entry point of Qubo.
* It delegates the start of the application to the CLI class.
* @see CLI
*/
public class Main {

/**
* The main method that is executed when the program is started.
* It calls the init method of the CLI class, passing along any command line arguments.
* @param args Command line arguments passed to the program.
*/
public static void main(String[] args) {
CLI.init(args);
}
}
}
Loading