Skip to content

Commit

Permalink
v1.17
Browse files Browse the repository at this point in the history
  • Loading branch information
StupidRepo committed Jul 19, 2023
1 parent faa891a commit 6208291
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 53 deletions.
12 changes: 6 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Changes
All notable changes to this project will be documented in `CHANGELOG.md`.
## Added
Nothing has been added.
* Resuming function!

## Modified
Nothing has been modified.
* ScannerThread checks if IP is in DB before even making a Socket for the IP.

## Removed
* Hits feature, again.
Nothing has been removed.

## TODOs
- [ ] Optimise IP generation and inital scanning code. [1]
- [ ] Optimise code in general.
- [ ] Seperate tool for viewing servers in DB. (code is already here (`ServerList` class), just need to make it seperate)
- [x] ~~Add a GUI for viewing servers in a nice friendly grid.~~

[todos]: https://github.com/StupidRepo/MCScanner/blob/main/CHANGELOG.md#todos
- [x] ~~Make it save what IP it got to when qutting so that it can resume from that IP on next startup.~~
- [x] ~~Add a GUI for viewing servers in a nice friendly grid.~~
4 changes: 4 additions & 0 deletions bin/main/com/stupidrepo/mcscanner/random.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- I've got an iframe tag in my HTML code. Into that there's a tag with a class: message_info. I want to modify the properties of this class but the css doesn't work. -->
<iframe src="https://www.tuttocampo.it/WidgetV2/Risultati/570ce2a7-5474-11e4-b2c1-448a5b2c3468" scrolling="no" class="iframe02" height="600" frameborder="0"></iframe>

<link rel="stylesheet" href="test.css" type="text/css" />
3 changes: 3 additions & 0 deletions bin/main/com/stupidrepo/mcscanner/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.message_info {
margin: 0px;
}
4 changes: 4 additions & 0 deletions offset.mcscan
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
0
0
59
9 changes: 9 additions & 0 deletions src/com/stupidrepo/mcscanner/DatabaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public ArrayList<Document> getServers() {
return servers;
}

public Long getServerCount() {
try {
return mainCollection.countDocuments();
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to get server count from database!");
return null;
}
}

public void writeDetailsToDB(String ip, String version, String motd, int maxPlayers) {
try {
mainCollection
Expand Down
128 changes: 81 additions & 47 deletions src/com/stupidrepo/mcscanner/MCScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import org.bson.Document;

public class MCScanner {
private static int offsetI = 1;
private static int offsetJ = 0;
private static int offsetK = 0;
private static int offsetL = 0;
public static void main(String[] var0) {
AtomicInteger threads = new AtomicInteger(1024);
int timeout = 1000;
Expand All @@ -29,7 +33,7 @@ public static void main(String[] var0) {

Logger logger = Logger.getLogger("com.stupidrepo.mcscanner");

float version = 1.16f;
float version = 1.17f;

AtomicReference<String> uri = new AtomicReference<>("mongodb://localhost:27017");

Expand All @@ -52,7 +56,7 @@ public static void main(String[] var0) {
logger.log(Level.INFO, "Scanning IPs...");

JFrame frame = new JFrame("MCScanner v" + version);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.setSize(300, 100);
frame.setLayout(new BorderLayout());

Expand All @@ -66,22 +70,67 @@ public static void main(String[] var0) {

long scanned = 0;

JButton viewServersButton = new JButton("View Servers");
frame.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
logger.log(Level.INFO, "Stopping threads...");
for (Thread nextThread: threadList) {
nextThread.interrupt();
}
logger.log(Level.INFO, "Making an 'offset.mcscan'...");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("offset.mcscan"));
writer.write(String.valueOf(offsetI + "\n" + offsetJ + "\n" + offsetK + "\n" + offsetL));
writer.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to write 'offset.mcscan'!");
}
logger.log(Level.INFO, "Exiting...");
System.exit(0);
}
});

ServerList serverList = new ServerList(databaseHandler);

JButton viewServersButton = new JButton("Show Server List");

viewServersButton.addActionListener(e -> {
ServerList serverList = new ServerList(databaseHandler);
serverList.showGUI();
if(serverList.toggleGUI()) {
viewServersButton.setText("Hide Server List");
} else {
viewServersButton.setText("Show Server List");
}
});

frame.add(viewServersButton, "North");

frame.setVisible(true);

// TODO: Make this whole thing more efficient, and less ugly. [1]
for (int i = minimumRange; i <= maxRange; ++i) {
for (int j = 0; j <= 255; ++j) {
for (int k = 0; k <= 255; ++k) {
for (int l = 0; l <= 255; ++l) {
File offsetFile = new File("offset.mcscan");
if (offsetFile.exists()) {
try {
logger.log(Level.INFO, "Found 'offset.mcscan'!");
BufferedReader reader = new BufferedReader(new FileReader(offsetFile));
offsetI = Integer.parseInt(reader.readLine());
offsetJ = Integer.parseInt(reader.readLine());
offsetK = Integer.parseInt(reader.readLine());
offsetL = Integer.parseInt(reader.readLine());
scanned = (offsetI+offsetJ+offsetK+offsetL)-1;
logger.log(Level.INFO, "Continuing from " + offsetI + "." + offsetJ + "." + offsetK + "." + offsetL + "...");
reader.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to read 'offset.mcscan'!");
}
}

for (int i = offsetI; i <= maxRange; ++i) {
offsetI = i;
for (int j = offsetJ; j <= 255; ++j) {
offsetJ = j;
for (int k = offsetK; k <= 255; ++k) {
offsetK = k;
for (int l = offsetL; l <= 255; ++l) {
offsetL = l;
String ip = i + "." + j + "." + k + "." + l;

ScannerThread scannerThread = new ScannerThread(ip, port, timeout, databaseHandler);
Expand All @@ -94,9 +143,9 @@ public static void main(String[] var0) {
try {
nextThread.join();
++scanned;
scannedLabel.setText("Scanned: " + scanned + "/" + progressThing * 256 + " (" + Math.round((scanned / (progressThing * 256)) * 100) / 100 + "%)");
scannedLabel.setText("Scanned: " + scanned + "/" + progressThing * 256 + " (" + Math.round((scanned / (progressThing * 256)) * 100) / 100 + "%) (" + ip + ")");
} catch (InterruptedException timeout2) {
// eh
// Timed out or smth
}
}
threadList.clear();
Expand All @@ -110,10 +159,9 @@ public static void main(String[] var0) {
try {
nextThreadAgain.join();
++scanned;
// progressBar.setValue(scanned);
scannedLabel.setText("Scanned: " + scanned + "/" + progressThing * 256);
} catch (InterruptedException timeout1) {
// well
// Timeout, again!
}
}

Expand All @@ -140,6 +188,9 @@ public ScannerThread(String ip, int port, int timeout, DatabaseHandler dbHandler

public void run() {
try {
if(dbHandler.isIPInDB(ip)) {
return;
}
Socket socket = new Socket();
socket.connect(new InetSocketAddress(this.ip, this.port), this.timeout);

Expand All @@ -156,26 +207,16 @@ public void run() {

int packetId = inputStream.read();

if (packetId == -1) {
socket.close();
throw new IOException("Premature end of stream.");
}

if (packetId != 0xFF) {
if (packetId == -1 || packetId != 0xFF) {
socket.close();
throw new IOException("Invalid packet ID (" + packetId + ").");
throw new IOException("Invalid packet: (" + packetId + ")");
}

int length = inputStreamReader.read();

if (length == -1) {
if (length == -1 || length == 0) {
socket.close();
throw new IOException("Premature end of stream.");
}

if (length == 0) {
socket.close();
throw new IOException("Invalid string length.");
throw new IOException("Premature end of stream/invalid String length.");
}

char[] chars = new char[length];
Expand All @@ -185,21 +226,14 @@ public void run() {
throw new IOException("Premature end of stream.");
}

if(dbHandler.isIPInDB(ip)) {
socket.close();
return;
}

String string = new String(chars);

String[] data;
if (string.startsWith("§")) {
// This is for 1.8 and above
data = string.split("\0");

dbHandler.writeDetailsToDB(ip, data[2], data[3], Integer.parseInt(data[5]));
} else {
// This is for 1.7 and below
data = string.split("§");

dbHandler.writeDetailsToDB(ip, data[0], Integer.parseInt(data[2]));
Expand All @@ -216,16 +250,14 @@ public void run() {

class ServerList {
private final DatabaseHandler dbHandler;
private final JFrame frame;

public ServerList(DatabaseHandler dbHandler) {
this.dbHandler = dbHandler;
}

public void showGUI() {
JFrame frame = new JFrame("MCScanner - Servers");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new BorderLayout());
this.frame = new JFrame("MCScanner - Servers (" + dbHandler.getServerCount() + ")");
this.frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.frame.setSize(500, 500);
this.frame.setLayout(new BorderLayout());

JTable table = new JTable();
table.setModel(new DefaultTableModel(
Expand Down Expand Up @@ -262,7 +294,6 @@ public void showGUI() {
});
}

// refresh option
JButton refreshButton = new JButton("Refresh");

refreshButton.addActionListener(e -> {
Expand All @@ -284,9 +315,12 @@ public void showGUI() {
TableRowSorter < TableModel > sorter = new TableRowSorter < > (table.getModel());
table.setRowSorter(sorter);

// frame.add(table, BorderLayout.CENTER);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(refreshButton, BorderLayout.SOUTH);
frame.setVisible(true);
this.frame.add(scrollPane, BorderLayout.CENTER);
this.frame.add(refreshButton, BorderLayout.SOUTH);
}

public boolean toggleGUI() {
this.frame.setVisible(!this.frame.isVisible());
return this.frame.isVisible();
}
}
4 changes: 4 additions & 0 deletions src/com/stupidrepo/mcscanner/random.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- I've got an iframe tag in my HTML code. Into that there's a tag with a class: message_info. I want to modify the properties of this class but the css doesn't work. -->
<iframe src="https://www.tuttocampo.it/WidgetV2/Risultati/570ce2a7-5474-11e4-b2c1-448a5b2c3468" scrolling="no" class="iframe02" height="600" frameborder="0"></iframe>

<link rel="stylesheet" href="test.css" type="text/css" />
3 changes: 3 additions & 0 deletions src/com/stupidrepo/mcscanner/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.message_info {
margin: 0px;
}

0 comments on commit 6208291

Please sign in to comment.