Created
May 21, 2023 16:17
-
-
Save R3DHULK/a9d33eb35ec53dfad6d8a524585d61a8 to your computer and use it in GitHub Desktop.
GUI Based Port Scanner In Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class PortScannerGUI extends JFrame { | |
private JTextField ipField; | |
private JTextField startPortField; | |
private JTextField endPortField; | |
private JButton scanButton; | |
private JCheckBox showAllCheckBox; | |
private JTextArea logArea; | |
public PortScannerGUI() { | |
setTitle("Port Scanner"); | |
setSize(400, 400); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLocationRelativeTo(null); | |
JPanel panel = new JPanel(); | |
panel.setLayout(new GridLayout(5, 2)); | |
JLabel ipLabel = new JLabel("Target IP: "); | |
ipField = new JTextField("127.0.0.1"); | |
JLabel startPortLabel = new JLabel("Start Port: "); | |
startPortField = new JTextField("1"); | |
JLabel endPortLabel = new JLabel("End Port: "); | |
endPortField = new JTextField("1024"); | |
scanButton = new JButton("Scan"); | |
showAllCheckBox = new JCheckBox("Show all ports"); | |
logArea = new JTextArea(); | |
panel.add(ipLabel); | |
panel.add(ipField); | |
panel.add(startPortLabel); | |
panel.add(startPortField); | |
panel.add(endPortLabel); | |
panel.add(endPortField); | |
panel.add(showAllCheckBox); | |
panel.add(scanButton); | |
scanButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
scanPorts(); | |
} | |
}); | |
add(panel, BorderLayout.NORTH); | |
add(new JScrollPane(logArea), BorderLayout.CENTER); | |
} | |
private void scanPorts() { | |
String targetIP = ipField.getText(); | |
int startPort = Integer.parseInt(startPortField.getText()); | |
int endPort = Integer.parseInt(endPortField.getText()); | |
boolean showAllPorts = showAllCheckBox.isSelected(); | |
logArea.setText(""); // Clear previous log | |
ExecutorService executor = Executors.newFixedThreadPool(20); | |
List<ScanPortTask> tasks = new ArrayList<>(); | |
for (int port = startPort; port <= endPort; port++) { | |
ScanPortTask task = new ScanPortTask(targetIP, port); | |
tasks.add(task); | |
executor.execute(task); | |
} | |
executor.shutdown(); | |
while (!executor.isTerminated()) { | |
// Wait for all tasks to complete | |
} | |
for (ScanPortTask task : tasks) { | |
if (showAllPorts || task.isOpen()) { | |
logArea.append("Port " + task.getPort() + " is " + (task.isOpen() ? "open" : "closed") + "\n"); | |
} | |
} | |
} | |
private class ScanPortTask implements Runnable { | |
private String targetIP; | |
private int port; | |
private boolean open; | |
public ScanPortTask(String targetIP, int port) { | |
this.targetIP = targetIP; | |
this.port = port; | |
} | |
public boolean isOpen() { | |
return open; | |
} | |
public int getPort() { | |
return port; | |
} | |
@Override | |
public void run() { | |
try { | |
Socket socket = new Socket(); | |
socket.connect(new InetSocketAddress(targetIP, port), 1000); | |
socket.close(); | |
open = true; | |
} catch (IOException e) { | |
open = false; | |
} | |
} | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
PortScannerGUI gui = new PortScannerGUI(); | |
gui.setVisible(true); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment