summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/PortScan.java
blob: b525df67449bb5fdf8f97d99bb2a579e3cd8fbea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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;
		}
	}

}