package de.tudarmstadt.informatik.hostage; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import de.tudarmstadt.informatik.hostage.handler.AbstractHandler; import de.tudarmstadt.informatik.hostage.handler.ByteArrayHandlerImpl; import de.tudarmstadt.informatik.hostage.handler.StringHandlerImpl; import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory; import de.tudarmstadt.informatik.hostage.protocol.Protocol; import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol; import de.tudarmstadt.informatik.hostage.ui.MainActivity; import de.tudarmstadt.informatik.hostage.wrapper.ByteArray; public class HoneyListener implements Runnable { private ArrayList handlers = new ArrayList(); public int getHandlerCount() { return handlers.size(); } private Protocol protocol; private ServerSocket server; private Thread thread; private HoneyService service; // Shared Preferences private SharedPreferences pref; // Editor for Shared preferences private Editor editor; private boolean running = false; public boolean isRunning() { return running; } public HoneyListener(HoneyService service, Protocol protocol) { this.service = service; this.protocol = protocol; pref = service.getApplicationContext().getSharedPreferences( MainActivity.SESSION_DATA, Context.MODE_PRIVATE); editor = pref.edit(); } @Override public void run() { while (!thread.isInterrupted()) { addHandler(); } for (AbstractHandler handler : handlers) { handler.kill(); } } public void start() { try { server = new MyServerSocketFactory().createServerSocket(protocol .getPort()); (this.thread = new Thread(this)).start(); editor.putBoolean(protocol + MainActivity.LISTENER, true); editor.commit(); service.notifyUI(protocol.toString(), MainActivity.LISTENER); running = true; } catch (Exception e) { e.printStackTrace(); } } public void stop() { try { server.close(); thread.interrupt(); editor.putBoolean(protocol + MainActivity.LISTENER, false); editor.commit(); service.notifyUI(protocol.toString(), MainActivity.LISTENER); running = false; } catch (Exception e) { e.printStackTrace(); } } public String getProtocolName() { return protocol.toString(); } public void refreshHandlers() { for (Iterator iterator = handlers.iterator(); iterator .hasNext();) { AbstractHandler handler = iterator.next(); if (handler.isTerminated()) { iterator.remove(); } } } private void addHandler() { try { Socket client = server.accept(); if (protocol.isSecure()) { startSecureHandler(client); } else { startHandler(client); } int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0); editor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount + 1); editor.commit(); service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT); } catch (Exception e) { e.printStackTrace(); } } private void startSecureHandler(Socket client) throws Exception { SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext(); SSLSocketFactory factory = sslContext.getSocketFactory(); SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null, client.getPort(), false); sslClient.setUseClientMode(false); handlers.add(newInstance(service, this, protocol.getClass() .newInstance(), sslClient)); } private void startHandler(Socket client) throws Exception { handlers.add(newInstance(service, this, protocol.getClass() .newInstance(), client)); } private AbstractHandler newInstance(HoneyService service, HoneyListener listener, Protocol protocol, Socket client) { if (protocol.getType().equals(String.class)) { return new StringHandlerImpl(service, listener, protocol, client); } else if (protocol.getType().equals(ByteArray.class)) { return new ByteArrayHandlerImpl(service, listener, protocol, client); } else { return null; } } }