added port scanner support
This commit is contained in:
@ -0,0 +1,33 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class IpInformation {
|
||||
|
||||
private List<PortInformation> portInformation;
|
||||
private String ipAddress;
|
||||
|
||||
public IpInformation(String ipAddress) {
|
||||
this.portInformation = new ArrayList<>();
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public List<PortInformation> getPortInformation() {
|
||||
return portInformation;
|
||||
}
|
||||
|
||||
public void setPortInformation(List<PortInformation> portInformation) {
|
||||
this.portInformation = portInformation;
|
||||
}
|
||||
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import de.muehlencord.shared.network.whois.NetworkInformation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.commons.net.util.SubnetUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class NetworkScanner {
|
||||
|
||||
private String networkString;
|
||||
private NetworkInformation networkInformation = null; // TODO add scan for information
|
||||
private List<IpInformation> ipinformationList = new ArrayList<>();
|
||||
|
||||
public NetworkScanner(String network) {
|
||||
this.networkString = network;
|
||||
}
|
||||
|
||||
public void scan() {
|
||||
SubnetUtils utils = new SubnetUtils(networkString);
|
||||
String[] addresses = utils.getInfo().getAllAddresses();
|
||||
|
||||
PortScanner portScanner = new PortScanner(20,1,1024);
|
||||
for (String currentAddress : addresses) {
|
||||
System.out.print ("Scanning "+currentAddress+"....");
|
||||
List<PortInformation> portInformation = portScanner.scan(currentAddress);
|
||||
if (atLeastOnePortOpen(portInformation)) {
|
||||
IpInformation ipInformation = new IpInformation(currentAddress);
|
||||
ipInformation.setPortInformation(portInformation);
|
||||
ipinformationList.add (ipInformation);
|
||||
System.out.println ("open ports found");
|
||||
} else {
|
||||
System.out.println ("not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<IpInformation> getIpsInNetwork() {
|
||||
return ipinformationList;
|
||||
}
|
||||
|
||||
private boolean atLeastOnePortOpen(List<PortInformation> portInformation) {
|
||||
for (PortInformation information : portInformation) {
|
||||
if (information.isOpen()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class PortInformation {
|
||||
|
||||
private int portNumber;
|
||||
private boolean open;
|
||||
|
||||
public PortInformation(int portNumber, boolean open) {
|
||||
this.portNumber = portNumber;
|
||||
this.open = open;
|
||||
}
|
||||
|
||||
public int getPortNumber() {
|
||||
return portNumber;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return open;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* History:
|
||||
*
|
||||
* $$Log$$
|
||||
*
|
||||
*/
|
||||
@ -0,0 +1,40 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class PortScan {
|
||||
|
||||
private PortScan() {
|
||||
// hide constructor
|
||||
}
|
||||
|
||||
public static Future<PortInformation> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) {
|
||||
return es.submit(new Callable<PortInformation>() {
|
||||
@Override
|
||||
public PortInformation call() {
|
||||
// System.out.println ("Probing "+ip+":"+port);
|
||||
try {
|
||||
Socket socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(ip, port), timeout);
|
||||
socket.close();
|
||||
return new PortInformation (port, true);
|
||||
} catch (Exception ex) {
|
||||
return new PortInformation (port, false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import static de.muehlencord.shared.network.PortScan.portIsOpen;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class PortScanner {
|
||||
|
||||
private final int startPort;
|
||||
private final int endPort;
|
||||
private final int timeout;
|
||||
|
||||
public PortScanner(int timeoutInMs, int startPort, int endPort) {
|
||||
this.startPort = startPort;
|
||||
this.endPort = endPort;
|
||||
this.timeout = timeoutInMs;
|
||||
}
|
||||
|
||||
public PortScanner() {
|
||||
this(20, 1, 65535);
|
||||
}
|
||||
|
||||
public List<PortInformation> scan(String ip) {
|
||||
|
||||
final List<PortInformation> resultList = new ArrayList<>();
|
||||
final ExecutorService es = Executors.newFixedThreadPool(20);
|
||||
final List<Future<PortInformation>> futures = new ArrayList<>();
|
||||
for (int port = startPort; port <= endPort; port++) {
|
||||
futures.add(portIsOpen(es, ip, port, timeout));
|
||||
}
|
||||
es.shutdown();
|
||||
|
||||
try {
|
||||
for (final Future<PortInformation> f : futures) {
|
||||
resultList.add(f.get());
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
ex.printStackTrace(); // FIXME add error handling
|
||||
} catch (ExecutionException ex) {
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/* *** getter *** */
|
||||
public int getStartPort() {
|
||||
return startPort;
|
||||
}
|
||||
|
||||
public int getEndPort() {
|
||||
return endPort;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,9 @@
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import de.muehlencord.shared.network.whois.WhoisParser;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -2,8 +2,12 @@
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import de.muehlencord.shared.network.whois.DefaultWhoisParser;
|
||||
import de.muehlencord.shared.network.whois.WhoisInformation;
|
||||
import de.muehlencord.shared.network.whois.WhoisException;
|
||||
import de.muehlencord.shared.network.whois.WhoisParser;
|
||||
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
|
||||
import de.muehlencord.shared.util.StringUtil;
|
||||
import static de.muehlencord.shared.util.StringUtil.getValueBetweenKeywords;
|
||||
@ -1,12 +1,6 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
|
||||
import de.muehlencord.shared.util.StringUtil;
|
||||
import java.text.ParseException;
|
||||
import java.util.Map;
|
||||
import org.apache.log4j.Logger;
|
||||
import static org.apache.log4j.Logger.getLogger;
|
||||
@ -1,8 +1,4 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@ -21,7 +17,7 @@ public class NetworkInformation implements Serializable {
|
||||
private String maintainedBy;
|
||||
private String status;
|
||||
|
||||
NetworkInformation(String netname, String descr, String country) {
|
||||
public NetworkInformation(String netname, String descr, String country) {
|
||||
this.netname = netname;
|
||||
this.description = descr;
|
||||
this.country = country;
|
||||
@ -1,4 +1,4 @@
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import org.apache.commons.net.whois.WhoisClient;
|
||||
import org.apache.log4j.Logger;
|
||||
@ -2,7 +2,7 @@
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -1,4 +1,4 @@
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -2,7 +2,7 @@
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.network;
|
||||
package de.muehlencord.shared.network.whois;
|
||||
|
||||
/**
|
||||
*
|
||||
Reference in New Issue
Block a user