summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSimon Rettberg2015-05-04 19:12:29 +0200
committerSimon Rettberg2015-05-04 19:12:29 +0200
commit864a99099e774c7726dd4a5901556414c3c49c3c (patch)
tree52b642962c1a497f7cc56a04f5f223ca984ecc6f /src
parent[mount-store] Don't use unix extensions, so permission checking will happen s... (diff)
downloadtmlite-bwlp-864a99099e774c7726dd4a5901556414c3c49c3c.tar.gz
tmlite-bwlp-864a99099e774c7726dd4a5901556414c3c49c3c.tar.xz
tmlite-bwlp-864a99099e774c7726dd4a5901556414c3c49c3c.zip
Add PortScan task
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/openslx/taskmanager/tasks/PortScan.java152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/taskmanager/tasks/PortScan.java b/src/main/java/org/openslx/taskmanager/tasks/PortScan.java
new file mode 100644
index 0000000..b525df6
--- /dev/null
+++ b/src/main/java/org/openslx/taskmanager/tasks/PortScan.java
@@ -0,0 +1,152 @@
+package org.openslx.taskmanager.tasks;
+
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.openslx.satserver.util.Exec;
+import org.openslx.taskmanager.api.AbstractTask;
+
+import com.google.gson.annotations.Expose;
+
+public class PortScan extends AbstractTask
+{
+ @Expose
+ private String host;
+ @Expose
+ private int[] ports;
+
+ private Output status = new Output();
+
+ @Override
+ protected boolean initTask()
+ {
+ this.setStatusObject( this.status );
+
+ if ( this.host == null || this.host.isEmpty() ) {
+ status.addMessage( "No host given!" );
+ return false;
+ }
+ if ( this.ports == null || this.ports.length == 0 ) {
+ status.addMessage( "No ports given!" );
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean execute()
+ {
+ ExecutorService tp = Executors.newFixedThreadPool( ports.length > 6 ? 6 : ports.length );
+ for ( final int port : ports ) {
+ tp.submit( new Callable<Object>() {
+ @Override
+ public Object call() throws Exception
+ {
+ status.ports.add( testPort( port ) );
+ return null;
+ }
+ } );
+ }
+ tp.shutdown();
+ try {
+ tp.awaitTermination( ports.length * 2, TimeUnit.SECONDS );
+ } catch ( InterruptedException e ) {
+ // ...
+ }
+ return true;
+ }
+
+ private Result testPort( int port )
+ {
+ boolean open = false;
+ final AtomicReference<String> fingerprint = new AtomicReference<>();
+ final AtomicReference<String> notAfter = new AtomicReference<>();
+ final StringBuffer sb = new StringBuffer();
+
+ try {
+ Socket sock = new Socket();
+ sock.connect( new InetSocketAddress( this.host, port ), 1200 );
+ open = true;
+ sb.append( "Found open port " + port );
+ sock.close();
+ } catch ( Exception e ) {
+ if ( !open ) {
+ sb.append( "Found closed port " + port );
+ }
+ }
+ if ( open ) {
+ String str = this.host.replaceAll( "[^a-zA-Z0-9\\.\\-_]", "" ) + ":" + port;
+ // Is open, see if it is running SSL
+ Exec.syncAt( 2, new Exec.ExecCallback() {
+
+ @Override
+ public void processStdOut( String line )
+ {
+ if ( line.startsWith( "notAfter=" ) ) {
+ notAfter.set( line.substring( 9 ) );
+ sb.append( "\nCertificate valid until " + notAfter.get() );
+ }
+ if ( line.startsWith( "SHA1 Fingerprint=" ) ) {
+ fingerprint.set( line.substring( 17 ) );
+ sb.append( "\nCertificate fingerprint: " + fingerprint.get() );
+ }
+ }
+
+ @Override
+ public void processStdErr( String line )
+ {
+ // Nothing will be here
+ }
+
+ }, "/", "/bin/sh", "-c",
+ "openssl s_client -showcerts -connect " + str + " </dev/null 2> /dev/null "
+ + " | openssl x509 -noout -enddate -fingerprint -sha1 2>&1" );
+ }
+ status.addMessage( sb.toString() );
+ return new Result( port, open, fingerprint.get(), notAfter.get() );
+ }
+
+ /**
+ * Output - contains additional status data of this task
+ */
+ private static class Output
+ {
+ protected String messages = null;
+ protected List<Result> ports = Collections.synchronizedList( new ArrayList<Result>() );
+
+ private void addMessage( String str )
+ {
+ if ( messages == null ) {
+ messages = str;
+ } else {
+ messages += "\n" + str;
+ }
+ }
+ }
+
+ @SuppressWarnings( "unused" )
+ private static class Result
+ {
+ protected final int port;
+ protected final boolean open;
+ protected final String certFingerprint;
+ protected final String notAfter;
+
+ public Result( int port, boolean open, String fingerprint, String notAfter )
+ {
+ this.port = port;
+ this.open = open;
+ this.certFingerprint = fingerprint;
+ this.notAfter = notAfter;
+ }
+ }
+
+}