summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/serverconnection/ConnectionHandler.java
blob: 498e058008906771ef95361bee7bbdd1e4a26956 (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
package org.openslx.imagemaster.serverconnection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import org.apache.log4j.Logger;
import org.openslx.filetransfer.Downloader;
import org.openslx.filetransfer.IncomingEvent;
import org.openslx.filetransfer.Listener;
import org.openslx.filetransfer.Uploader;
import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.db.DbImage;
import org.openslx.imagemaster.util.Tuple;

/**
 * Class to handle all incoming and outgoing connections.
 */
public class ConnectionHandler implements IncomingEvent
{
	private static Logger log = Logger.getLogger( ConnectionHandler.class );
	private static SSLContext sslContext;
	/**
	 * Key: token, 
	 * Value: Tuple of the listener and the filepath.
	 */
	private static Map<String, Tuple<Thread, String>> activeListeners = new HashMap<>();
	private static List<Integer> possiblePorts = new LinkedList<>();
	private static IncomingEvent eventHandler = new ConnectionHandler();
	
	static {
		possiblePorts.add( 1234 );
		possiblePorts.add( 1235 );
		possiblePorts.add( 1236 );
		possiblePorts.add( 1237 );
		possiblePorts.add( 1238 );
		possiblePorts.add( 1239 );
		possiblePorts.add( 1240 );
		possiblePorts.add( 1241 );
		possiblePorts.add( 1242 );
		possiblePorts.add( 1243 );
		possiblePorts.add( 1244 );
		
		try {
   		String pathToKeyStore = Globals.getSslKeystoreFile();
   	   char[] passphrase = Globals.getSslKeystorePassword().toCharArray();
   	   KeyStore keystore = KeyStore.getInstance("JKS");
   	   keystore.load(new FileInputStream(pathToKeyStore), passphrase);
   	   KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
   	   kmf.init(keystore, passphrase);
   	   sslContext = SSLContext.getInstance("SSLv3");
   	   KeyManager[] keyManagers = kmf.getKeyManagers();
   	   
   	   sslContext.init(keyManagers, null, null);
		} catch (FileNotFoundException e) {
			log.error( "Could not find keystore." );
		} catch ( KeyStoreException e ) {
			log.error( "KeyStore implemenation not supported." );
		} catch ( NoSuchAlgorithmException e ) {
			log.error( "Could not find such Algorithm" );
		} catch ( CertificateException e ) {
			log.error( "Certificate unvalid." );
		} catch ( IOException e ) {
			log.error( "Could not read keyfile" );
		} catch ( UnrecoverableKeyException e ) {
			log.error( "Key in keystore is not valid" );
		} catch ( KeyManagementException e ) {
			log.error( "Context initialization failed." );
		}
	}
	
	public static void addConnection(String token, String filepath)
	{
		int port = possiblePorts.remove( 0 );
		Listener listener = new Listener( eventHandler, sslContext, port );
		
		listener.start();
		
		activeListeners.put( token, new Tuple<Thread, String>(listener, filepath) );
	}
	
	public static boolean hasConnection( String token )
	{
		return activeListeners.containsKey( token );
	}
	
	public static void removeConnection( String token )
	{
		Listener l = (Listener)activeListeners.remove( token ).x;
		l.interrupt();
		possiblePorts.add(l.getPort());		// add port back to possible's list
	}

	/**
	 * Server is uploading - client is downloading!
	 */
	@Override
	public void incomingUploader( Uploader uploader ) throws IOException
	{
		// TODO: Handle incoming uploads (client download requests)
	}
	
	/**
	 * Server is downloading - client is uploading!
	 */
	@Override
	public void incomingDownloader( Downloader downloader ) throws IOException
	{
		// try to read meta data
		while (downloader.readMetaData()) {
			// check token to identify the client
			String token = downloader.getToken();
			if (!activeListeners.containsKey( token )) {
				return;
			}
			downloader.setOutputFilename( activeListeners.get( token ).y );
			downloader.readBinary();
			downloader.close();
		}
	}
}