Skip to content

Commit

Permalink
v1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
StupidRepo committed Jul 20, 2023
1 parent 483d654 commit 3f5a2e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ All notable changes to this project will be documented in `CHANGELOG.md`.
Nothing has been added.

## Modified
* Stopping & resuming handling.
* `ServerList` refreshes every 10 seconds.
* Fixed bug where when an offset got to ~252-255, it would stay there and miss out on ***a lot*** of IPs.

## Removed
Nothing has been removed.
* The thing that tells you how many IPs are left to scan. It was inaccurate and I'm pretty sure getting & drawing that number to the screen made it lag a lot.

## TODOs
- [ ] Optimise IP generation and inital scanning code.[¹][1]
- [ ] Optimise IP generation and initial 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] ~~Make it save what IP it got to when qutting so that it can resume from that IP on next startup.~~
- [ ] Separate tool for viewing servers in DB. (code is already here (`ServerList` class), just need to make it separate)
- [x] ~~Make it save what IP it got to when quitting so that it can resume from that IP on next startup.~~
- [x] ~~Add a GUI for viewing servers in a nice friendly grid.~~

[1]: https://github.com/StupidRepo/MCScanner/blob/main/src/com/stupidrepo/mcscanner/MCScanner.java#L126
75 changes: 42 additions & 33 deletions src/com/stupidrepo/mcscanner/MCScanner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.stupidrepo.mcscanner;

import java.awt.BorderLayout;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -34,7 +37,7 @@ public static void main(String[] var0) {

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

float version = 1.17f;
float version = 1.19f;

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

Expand All @@ -61,10 +64,9 @@ public static void main(String[] var0) {
frame.setSize(300, 100);
frame.setLayout(new BorderLayout());

double progressThing = (maxRange - minimumRange + 1) * 256 * 256;
ArrayList < Thread > threadList = new ArrayList < Thread > ();

JLabel scannedLabel = new JLabel("Scanned: 0/" + progressThing * 256);
JLabel scannedLabel = new JLabel("Scanned: 0");
scannedLabel.setHorizontalAlignment(0);

frame.add(scannedLabel, "Center");
Expand All @@ -86,7 +88,7 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
logger.log(Level.INFO, "Making an '.mcscanner'...");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(".mcscanner"));
writer.write(String.valueOf(offsetI + "\n" + offsetJ + "\n" + offsetK + "\n" + offsetL));
writer.write(offsetI + "\n" + offsetJ + "\n" + offsetK + "\n" + offsetL);
writer.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to write '.mcscanner'!");
Expand Down Expand Up @@ -132,25 +134,25 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
int thisOffsetJ = offsetJ;
int thisOffsetK = offsetK;
int thisOffsetL = offsetL;
for (int i = thisOffsetI; i <= maxRange; ++i) {
for (int i = 0; i <= (maxRange-thisOffsetI); ++i) {
if(stopping) {
break;
} else {
offsetI = i;
}
for (int j = thisOffsetJ; j <= 255; ++j) {
for (int j = minimumRange; j <= (255-thisOffsetJ); ++j) {
if(stopping) {
break;
} else {
offsetJ = j;
}
for (int k = thisOffsetK; k <= 255; ++k) {
for (int k = 0; k <= (255-thisOffsetK); ++k) {
if(stopping) {
break;
} else {
offsetK = k;
}
for (int l = thisOffsetL; l <= 255; ++l) {
for (int l = 0; l <= (255-thisOffsetL); ++l) {
if(stopping) {
break;
} else {
Expand All @@ -168,7 +170,7 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
try {
nextThread.join();
++scanned;
scannedLabel.setText("Scanned: " + scanned + "/" + progressThing * 256 + " (" + Math.round((scanned / (progressThing * 256)) * 100) / 100 + "%)");
scannedLabel.setText("Scanned: " + scanned);
} catch (InterruptedException timeout2) {
// Timed out or smth
}
Expand All @@ -184,15 +186,17 @@ public void windowClosing(java.awt.event.WindowEvent windowEvent) {
try {
nextThreadAgain.join();
++scanned;
scannedLabel.setText("Scanned: " + scanned + "/" + progressThing * 256);
scannedLabel.setText("Scanned: " + scanned);
} catch (InterruptedException timeout1) {
// Timeout, again!
}
}

frame.setVisible(false);
frame.dispatchEvent(new WindowEvent(frame, 201));
logger.log(Level.INFO, "Scan completed!");
if(!stopping) {
frame.setVisible(false);
frame.dispatchEvent(new WindowEvent(frame, 201));
logger.log(Level.INFO, "Scan completed!");
}
}
}

Expand Down Expand Up @@ -232,7 +236,7 @@ public void run() {

int packetId = inputStream.read();

if (packetId == -1 || packetId != 0xFF) {
if (packetId != 0xFF) {
socket.close();
throw new IOException("Invalid packet: (" + packetId + ")");
}
Expand Down Expand Up @@ -281,7 +285,7 @@ public ServerList(DatabaseHandler dbHandler) {
this.dbHandler = dbHandler;
this.frame = new JFrame("MCScanner - Servers (" + this.dbHandler.getServerCount() + ")");
this.frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.frame.setSize(500, 500);
this.frame.setSize(720, 500);
this.frame.setLayout(new BorderLayout());

JTable table = new JTable();
Expand All @@ -300,31 +304,20 @@ public ServerList(DatabaseHandler dbHandler) {
table.setFillsViewportHeight(true);
table.setRowHeight(20);
table.setRowSelectionAllowed(false);
table.setDefaultEditor(Object.class, null);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

ArrayList < Document > documents = this.dbHandler.getServers();
for (Document document: documents) {
String ip = document.getString("ip");
String motd = document.getString("motd");
String version = document.getString("version");
int players = document.getInteger("maxPlayers");

((DefaultTableModel) table.getModel()).addRow(new Object[] {
ip, motd, version, players
});
}

JButton refreshButton = new JButton("Refresh");

refreshButton.addActionListener(e -> {
Timer timer = new Timer(10000, e -> {
((DefaultTableModel) table.getModel()).setRowCount(0);

ArrayList < Document > documents1 = this.dbHandler.getServers();
this.frame.setTitle("MCScanner - Servers (" + documents1.size() + ")");
for (Document document: documents1) {
String ip = document.getString("ip");
String motd = document.getString("motd");
Expand All @@ -336,12 +329,28 @@ public ServerList(DatabaseHandler dbHandler) {
});
}
});
timer.setRepeats(true);
timer.start();
timer.getListeners(ActionListener.class)[0].actionPerformed(null);

// copy IP to clipboard when clicked
table.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = table.rowAtPoint(evt.getPoint());
String ip = table.getModel().getValueAt(row, 0).toString();
StringSelection stringSelection = new StringSelection(ip);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
// make message box appear that saying "copied ip to clipboard"
JOptionPane.showMessageDialog(null, "Copied " + ip + " to clipboard!", "Copied!", JOptionPane.INFORMATION_MESSAGE);
}
});

TableRowSorter < TableModel > sorter = new TableRowSorter < > (table.getModel());
table.setRowSorter(sorter);

this.frame.add(scrollPane, BorderLayout.CENTER);
this.frame.add(refreshButton, BorderLayout.SOUTH);
}

public boolean toggleGUI() {
Expand Down

0 comments on commit 3f5a2e3

Please sign in to comment.