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;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -0,0 +1,30 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import org.apache.commons.net.util.SubnetUtils;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class NetworkScannerTest {
|
||||
|
||||
public NetworkScannerTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScan() throws UnknownHostException {
|
||||
String ipAddress = InetAddress.getLocalHost().getHostAddress();
|
||||
SubnetUtils utils = new SubnetUtils(ipAddress, "255.255.255.0");
|
||||
String networkAddress = utils.getInfo().getNetworkAddress();
|
||||
|
||||
NetworkScanner networkScanner = new NetworkScanner (networkAddress+"/24"); // TODO get ip address from network device
|
||||
networkScanner.scan();
|
||||
assertFalse (networkScanner.getIpsInNetwork().isEmpty());
|
||||
System.out.println (networkScanner.getIpsInNetwork());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package de.muehlencord.shared.network;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class PortScannerTest {
|
||||
|
||||
public PortScannerTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMain() {
|
||||
PortScanner portScanner = new PortScanner();
|
||||
List<PortInformation> resultList = portScanner.scan("127.0.0.1");
|
||||
|
||||
int openPorts = 0;
|
||||
for (PortInformation result : resultList) {
|
||||
if (result.isOpen()) {
|
||||
System.out.println("Port " + result.getPortNumber() + " is open");
|
||||
openPorts++;
|
||||
}
|
||||
}
|
||||
System.out.println("There are " + openPorts + " open ports on host " + portScanner.getTimeout() + " (probed with a timeout of "
|
||||
+ portScanner.getTimeout() + "ms)");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,8 +2,11 @@
|
||||
* 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.BaseTest;
|
||||
import de.muehlencord.shared.network.whois.WhoisInformation;
|
||||
import de.muehlencord.shared.network.whois.WhoisException;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
@ -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.BaseTest;
|
||||
import de.muehlencord.shared.network.whois.Whois;
|
||||
import de.muehlencord.shared.network.whois.WhoisInformation;
|
||||
import de.muehlencord.shared.network.whois.WhoisException;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
2
pom.xml
2
pom.xml
@ -63,7 +63,7 @@
|
||||
<dependency>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
@ -4,18 +4,23 @@
|
||||
*/
|
||||
package de.muehlencord.shared.util.file;
|
||||
|
||||
import de.muehlencord.shared.util.StringUtil;
|
||||
import static de.muehlencord.shared.util.StringUtil.getStackTraceString;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.DigestInputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.Format;
|
||||
import java.util.Arrays;
|
||||
import static java.util.Arrays.asList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
import static org.apache.log4j.Logger.getLogger;
|
||||
|
||||
@ -25,7 +30,9 @@ import static org.apache.log4j.Logger.getLogger;
|
||||
*/
|
||||
public abstract class FileUtil {
|
||||
|
||||
/** the logging object */
|
||||
/**
|
||||
* the logging object
|
||||
*/
|
||||
private static final Logger logger = getLogger(FileUtil.class);
|
||||
|
||||
/**
|
||||
@ -153,4 +160,31 @@ public abstract class FileUtil {
|
||||
File location = new File(path);
|
||||
return location.exists();
|
||||
}
|
||||
|
||||
public static String md5Sum(String fileName) throws IOException {
|
||||
|
||||
MessageDigest md;
|
||||
try {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new IOException("Cannot get MD5 MessageDigest instance");
|
||||
}
|
||||
|
||||
try (InputStream is = Files.newInputStream(Paths.get(fileName))) {
|
||||
DigestInputStream dis = new DigestInputStream(is, md);
|
||||
int nread;
|
||||
byte[] buffer = new byte[4096];
|
||||
while ((nread = dis.read(buffer)) != -1) {
|
||||
md.update(buffer);
|
||||
}
|
||||
}
|
||||
byte[] digest = md.digest();
|
||||
|
||||
//convert the byte to hex format method 1
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < digest.length; i++) {
|
||||
sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,10 +6,13 @@ package de.muehlencord.shared.util.file;
|
||||
|
||||
import static de.muehlencord.shared.util.file.FileUtil.getFilesFromDirecotry;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@ -24,6 +27,7 @@ public class FileUtilTest {
|
||||
* @throws URISyntaxException if the testfile specification is wrong
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void testGetFilesFromDirecotry() throws FileHandlingException, URISyntaxException {
|
||||
System.out.println("testGetFilesFromDirectory");
|
||||
|
||||
@ -35,7 +39,16 @@ public class FileUtilTest {
|
||||
while (it.hasNext()) {
|
||||
System.out.println(it.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMd5Sum() throws IOException, URISyntaxException {
|
||||
System.out.println("testGetFilesFromDirectory");
|
||||
URL url = getClass().getResource("testfile.txt");
|
||||
File testFile = new File(url.getFile());
|
||||
String fileName = testFile.toString();
|
||||
|
||||
fileName = "h:/temp/01750229371_DVD001.iso";
|
||||
System.out.println (FileUtil.md5Sum(fileName));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user