Commit 46a0fedd authored by abdullh.alsoleman's avatar abdullh.alsoleman

Executor_HW

parent 2e5fa1cb
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="ec7ff361-e951-4939-a0ef-dbe3a87ebd85" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/IPValidator.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/InvalidException.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/PortValidator.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Scanner/IpAddress.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Scanner/IpPortsScanner.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Scanner/Port.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/Scanner/PortScanner.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/ScannerService.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2XzN86mvwXPNdzu4fnexKxu14dF" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "C:/Users/Abdullah/IdeaProjects/Executor_HW"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="ec7ff361-e951-4939-a0ef-dbe3a87ebd85" name="Changes" comment="" />
<created>1699627788644</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1699627788644</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Executor_HW</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
public class IPValidator {
public static void validateIPAddress(String ipAddress) throws InvalidException {
// Perform IP address validation logic
if (!isValidIPAddress(ipAddress)) {
throw new InvalidException("Invalid IP address: " + ipAddress + "\n It should be 4 bytes like xxx.xxx.xxx.xxx" );
}
}
private static boolean isValidIPAddress(String ipAddress) {
// Split the IP address into octets
String[] octets = ipAddress.split("\\.");
// Check if the IP address consists of four octets
if (octets.length != 4) {
return false;
}
// Check each octet for validity
for (String octet : octets) {
try {
// Parse the octet as an integer
int value = Integer.parseInt(octet);
// Check if the octet is within the valid range (0-255)
if (value < 0 || value > 255) {
return false;
}
} catch (NumberFormatException e) {
// If the octet is not a valid integer
return false;
}
}
// If all checks pass, the IP address is valid
return true;
}
}
\ No newline at end of file
public class InvalidException extends Exception{
public InvalidException(String message) {
super(message);
}
}
public class PortValidator {
public static void validatePort(int port) throws InvalidException {
// Perform port validation logic
if (!isValidPort(port)) {
throw new InvalidException("Invalid port number: " + port + "\n it should be between 0 amd 65536");
}
}
private static boolean isValidPort(int port) {
if (port < 0 || port > 65536) {
return false;
} else return true;
}
}
package Scanner;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class IpAddress {
private String ipAddress;
private List<Port> openPortList;
private List<Port> closedPortList;
public IpAddress(String ipAddress) {
this.ipAddress = ipAddress;
closedPortList = new ArrayList<>();
openPortList = new ArrayList<>();
}
public String getIpAddress() {
return ipAddress;
}
public List<Port> getOpenPortList() {
return openPortList;
}
public List<Port> getClosedPortList() {
return closedPortList;
}
public static InetAddress incrementIpAddress(InetAddress ip) {
byte[] address = ip.getAddress();
for (int i = address.length - 1; i >= 0; i--) {
if ((address[i] & 0xFF) < 255) {
address[i]++;
break;
} else {
address[i] = 0;
}
}
try {
ip = InetAddress.getByAddress(address);
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
return ip;
}
}
package Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class IpPortsScanner {
public static Future<IpAddress> ipScanner(ExecutorService executorService, String ipAddress, int startPort, int endPort) {
return executorService.submit(new Callable<IpAddress>() {
@Override
public IpAddress call() throws Exception {
List<Future<Port>> futureList = new ArrayList<>();
List<Port> resultList = new ArrayList<>();
int threadCount = Runtime.getRuntime().availableProcessors();
ExecutorService executorService1 = Executors.newFixedThreadPool(threadCount);
for (int currPort = startPort; currPort <= endPort; currPort++) {
futureList.add(PortScanner.isPortOpen(executorService1, ipAddress, currPort));
}
executorService1.shutdown();
for (Future<Port> eachFuture : futureList) {
try {
resultList.add(eachFuture.get(2, TimeUnit.MINUTES));
} catch (Exception e) {
System.err.println("Timeout reached");
System.exit(1);
}
}
IpAddress IP = new IpAddress(ipAddress);
for (Port port : resultList) {
if (port.isOpen) {
IP.getOpenPortList().add(port);
} else {
IP.getClosedPortList().add(port);
}
}
return IP;
}
});
}
}
package Scanner;
import java.util.List;
public class Port {
int numOfPort;
boolean isOpen;
public int getNumOfPort() {
return numOfPort;
}
public boolean isOpen() {
return isOpen;
}
public Port(int numOfPort, boolean isOpen) {
this.numOfPort = numOfPort;
this.isOpen = isOpen;
}
public void print (){
System.out.println("port: "+this.numOfPort);
}
}
package Scanner;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
public class PortScanner {
public static Future<Port> isPortOpen(ExecutorService executorService, String ipAddress, int portNumber) {
return executorService.submit(new Callable<Port>() {
@Override
public Port call() {
Port port = new Port(portNumber,false);
try (Socket sfd = new Socket()) {
sfd.connect(new InetSocketAddress(ipAddress, portNumber), 100);
port.isOpen=true;
} catch (Exception e) {}
return port;
}
});
}
}
import Scanner.Port;
import Scanner.IpPortsScanner;
import Scanner.IpAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ScannerService {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String startIP;
//Enter start IP address of range and validate it
while (true) {
try {
System.out.println("Enter Start IP");
startIP = input.next();
IPValidator.validateIPAddress(startIP);
break;
} catch (InvalidException I) {
System.out.println("Exception occurred: " + I.getMessage());
}
}
String endIP;
//Enter end IP address of range and validate it
while (true) {
try {
System.out.println("Enter End IP");
endIP = input.next();
IPValidator.validateIPAddress(endIP);
break;
} catch (InvalidException I) {
System.out.println("Exception occurred: " + I.getMessage());
}
}
int startPort;
//Enter start Port number of range and validate it
while (true) {
try {
System.out.println("Enter Start port");
startPort = input.nextInt();
PortValidator.validatePort(startPort);
break;
}catch (InvalidException I){
System.out.println("Exception occurred: " + I.getMessage());
}
}
int endPort;
//Enter end Port number of range and validate it
while (true) {
try {
System.out.println("Enter end port");
endPort = input.nextInt();
PortValidator.validatePort(endPort);
break;
}catch (InvalidException I){
System.out.println("Exception occurred: " + I.getMessage());
}
}
int threadCount = Runtime.getRuntime().availableProcessors();
List<Future<IpAddress>> futureList = new ArrayList<>();
//List of ip objects each of it contain list of open ports
List<IpAddress> ipResultList = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
try {
InetAddress startAddress = InetAddress.getByName(startIP);
InetAddress endAddress = InetAddress.getByName(endIP);
// pass over ip range
while (!startAddress.equals(endAddress)) {
String ip = startAddress.getHostAddress();
// submit task to executor when we call the function IpPortsScanner.ipScanner(executorService, ip, startPort, endPort)
futureList.add(IpPortsScanner.ipScanner(executorService, ip, startPort, endPort));
startAddress = IpAddress.incrementIpAddress(startAddress);
}
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
executorService.shutdown();
// cast future<Ipaddress> to List<Ipaddress>
for (Future<IpAddress> eachFuture : futureList) {
try {
ipResultList.add(eachFuture.get(2, TimeUnit.MINUTES));
} catch (Exception e) {
System.err.println("Timeout reached");
System.exit(1);
}
}
for (IpAddress ip : ipResultList) {
for (Port port : ip.getOpenPortList()) {
System.out.println("for ip address " + ip.getIpAddress());
port.print();
}
}
}
}
package org.example;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment