From 3f5a2e32941920e80b9368e26e1b431867d42b5d Mon Sep 17 00:00:00 2001 From: Bradlee Barnes <69256931+StupidRepo@users.noreply.github.com> Date: Thu, 20 Jul 2023 17:34:19 +0100 Subject: [PATCH] v1.19 --- CHANGELOG.md | 11 +-- src/com/stupidrepo/mcscanner/MCScanner.java | 75 ++++++++++++--------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35f9ddf..e57910c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/src/com/stupidrepo/mcscanner/MCScanner.java b/src/com/stupidrepo/mcscanner/MCScanner.java index 6a9752f..8d76a37 100644 --- a/src/com/stupidrepo/mcscanner/MCScanner.java +++ b/src/com/stupidrepo/mcscanner/MCScanner.java @@ -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; @@ -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 uri = new AtomicReference<>("mongodb://localhost:27017"); @@ -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"); @@ -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'!"); @@ -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 { @@ -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 } @@ -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!"); + } } } @@ -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 + ")"); } @@ -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(); @@ -300,6 +304,9 @@ 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); @@ -307,24 +314,10 @@ public ServerList(DatabaseHandler dbHandler) { 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"); @@ -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() {