summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/taskmanager/tasks/RemoteDebug.java
blob: fd4bec34d64eb4334a5160cb266efff5a2b09344 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package org.openslx.taskmanager.tasks;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import java.security.KeyStore;
import java.security.cert.X509Certificate;

import java.util.Date;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.openslx.taskmanager.api.AbstractTask;

import com.google.gson.annotations.Expose;

/**
 * @author Christoph Schulthess
 *
 */
public class RemoteDebug extends AbstractTask 
{
	
	protected Output status = new Output();
	
	@Expose
	public String ip;
	@Expose 
	public int port;
	
	/**
	 * Run debug task
	 */
	@Override
	protected boolean execute()
	{
		status.addMessage( "INFO: Executing." );
		SSLContext ctx = getSSLContext();
		if ( ctx == null ) {
			status.addMessage( "ERROR: SSLContext is null." );
			return false;
		}
		status.addMessage( "INFO: SSLContext successfully created." );
		
		try ( SSLSocket dbgSock = getDbgSock( ctx );
				SSLServerSocket srvSock = getSrvSock( ctx ) ) {
			connectToDbg( dbgSock );
			bindToPort( srvSock );
			try ( SSLSocket poolSock = ( SSLSocket ) srvSock.accept() ) {
				status.addMessage( "INFO: Connection from pool client established." );
				status.setCltAddr( poolSock.getInetAddress() );
				relay( dbgSock, poolSock );
			} catch ( Exception ex ) {
				throw( ex );
			}
		} catch ( Exception e ) {
			status.addMessage( "ERROR: " + e.getMessage() );
			return false;
		}
		status.addMessage( "INFO: Task finished properly." );
		return true;		
	}
	
	/**
	 * @param SSLSocket dbgSock
	 * @throws IOException
	 * Connect SSLSocket dbgSock to debug server at ip:port specified by the JSON object
	 */
	protected void connectToDbg ( SSLSocket dbgSock ) throws IOException {
		InetSocketAddress addr = new InetSocketAddress( ip, port );
		dbgSock.connect( addr );
		status.addMessage( "INFO: Connected to debug server at " + ip + ":" + port + "." );
	}
	
	/**
	 * @param SSLServerSocket srvSock
	 * @throws IOException
	 * Bind SSLServerSocket srvSock to local port and update status object accordingly.
	 */
	protected void bindToPort ( SSLServerSocket srvSock ) throws IOException {
		srvSock.bind( null );
		status.setListenPort( srvSock.getLocalPort() );
		status.addMessage( "INFO: Listening on localhost:" + status.getListenPort() + "." );
	}
	
	/**
	 * @return SSLContext
	 * Get all-trusting SSLContext via the trustAll() method
	 */
	protected SSLContext getSSLContext() {
		SSLContext ctx = null;
		try {
			ctx = trustAll();
		} catch ( Exception e ) {
			status.addMessage( "ERROR: Failed to create SSLContext." );
			//status.addMessage( "DEBUG: " + getStrStackTrace(e) );
		}
		return ctx;
	}

	/**
	 * @param dbgSock - SSLSocket connected to debug server
	 * @param poolSock - SSLSocket retrieved via srvSock.accept()
	 * Create and start the two necessary relays. Join them, so that proper postcondition is enforced.
	 */
	protected void relay( SSLSocket dbgSock, SSLSocket poolSock ) {
		Relay toDbg = new Relay( poolSock, dbgSock, status );
		Relay toPool = new Relay( dbgSock, poolSock, status );
		toDbg.setName( "PoolToDebug" );
		toPool.setName( "DebugToPool" );
		toDbg.start();
		toPool.start();
		status.addMessage( "INFO: Threads started." );
		
		try {
			for ( Relay r : new Relay[]{ toDbg, toPool })
				r.join();
		} catch ( InterruptedException ix ) {
			status.addMessage( "INFO: Relay closed: " + Thread.currentThread().getName() );
		}
	}
	
	/**
	 * @return SSLContext
	 * @throws Exception
	 * Create all-trusting X509 certificate manager to disable verification in the bwlp-environment.
	 */
	protected SSLContext trustAll () throws Exception 
	{
		TrustManager[] trustAllMgr = new TrustManager[] { new X509TrustManager() {
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				public void checkClientTrusted( X509Certificate[] certs, String authType ) {}
				public void checkServerTrusted( X509Certificate[] certs, String authType ) {}
			}
		};
		
		KeyStore ks = KeyStore.getInstance( "JKS" );
		try ( InputStream ksIs = new FileInputStream( "/opt/taskmanager/data/keystore.jks" ) ){
			ks.load( ksIs, "password".toCharArray() );
		}
		KeyManagerFactory kmf = KeyManagerFactory.getInstance( 
				KeyManagerFactory.getDefaultAlgorithm() );
		kmf.init( ks, "password".toCharArray() );
		
		SSLContext ctx = SSLContext.getInstance( "SSL" );
		ctx.init( kmf.getKeyManagers(), trustAllMgr, new java.security.SecureRandom() );
		return ctx;
	}

	/**
	 * @param ctx - SSLContext
	 * @return SSLServerSocket
	 * @throws IOException
	 */
	protected SSLServerSocket getSrvSock( SSLContext ctx ) throws IOException
	{
		status.addMessage( "INFO: Creating server socket." );
		SSLServerSocketFactory sssf = ctx.getServerSocketFactory();
		status.addMessage( "INFO: Server socket factory created." );
		SSLServerSocket s = ( SSLServerSocket ) sssf.createServerSocket();
		status.addMessage( "INFO: Server socket created." );
		return s;
	}

	/**
	 * @param ctx - SSLContext
	 * @return SSLSocket
	 * @throws IOException
	 */
	protected SSLSocket getDbgSock( SSLContext ctx ) throws IOException
	{
		status.addMessage( "INFO: Creating debug socket." );
		SSLSocketFactory ssf = ctx.getSocketFactory();
		SSLSocket s = ( SSLSocket ) ssf.createSocket();
		status.addMessage( "INFO: Debug socket created." );
		return s;
	}
	
	
	/**
	 * Set status class for this object and push a first message to it.
	 */
	@Override
	protected boolean initTask() 
	{
		this.setStatusObject( this.status );
		status.addMessage( "INFO: Initiating directed relay to debug server at: " + ip + ":" + port + "." );
		return true;
	}
	
	/**
	 * Status class that holds information about the current debug task.
	 * Most important is probably the listen port which is sent to the client to connect its VNC server in reverse mode.
	 */
	public static class Output 
	{		
		
		protected String messages = null;
		protected Date d = null;
		protected String cltAddr = null;
		protected int listenPort = -1;
		
		public void setListenPort ( int port ) { listenPort = port; }
		public int getListenPort ()	{ return listenPort; }
		public void setCltAddr ( InetAddress addr ) { cltAddr = addr.getHostAddress();	}
		
		public void addMessage( String str )
		{
			d = new Date();
			if ( messages == null ) {
				messages = d.toString() + "-" + str;
			} else {
				messages += "\n" + d.toString() + "-" + str;
			}
		}
	}
	
	/**
	 * Generic relay class to write data from one socket to another.
	 * For a working debug task, you need two of these - one for each direction.
	 */
	private class Relay extends Thread
	{
		boolean active = true;
		private byte[] buffer = new byte[16384];
		
		private SSLSocket srcSock;
		private SSLSocket destSock;
		
		private Output status;
		
		public Relay ( SSLSocket srcSock, SSLSocket destSock, Output status ) {
			this.srcSock = srcSock;
			this.destSock = destSock;
			this.status = status;
		}
		
		@Override
		public void run() {
			boolean first = true;
			int readBytes;
			try ( InputStream in = srcSock.getInputStream();
					OutputStream out = destSock.getOutputStream() ) {
				while( active ) {
					readBytes = in.read( buffer );
					out.write( buffer, 0, readBytes );
					if ( first ) {
						status.addMessage( "INFO: Relay operating: " + this.getName() );
						first = false;
					}
				}	
			} catch ( Exception e ) {
				//status.addMessage( "DEBUG: " + this.getName() + " - " + getStrStackTrace( e ) );
				active = false;
				return;
			}
		}	
	}

//	public static String getStrStackTrace(Throwable aThrowable) {
//		Writer result = new StringWriter();
//		PrintWriter printWriter = new PrintWriter(result);
//		aThrowable.printStackTrace(printWriter);
//		return result.toString();
//	}
}