123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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.SSLServerSocket;
- import javax.net.ssl.SSLSocket;
- 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.ui.MainActivity;
- import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
- public class HoneyListener implements Runnable {
- private ArrayList<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
- 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 {
- if(protocol.isSecure())
- //TODO
- // server = new MyServerSocketFactory().createServerSocket(
- // protocol.getPort(), protocol.getSSLContext());
- server = (SSLServerSocket) protocol.getSSLContext().getServerSocketFactory().createServerSocket(protocol.getPort());
- else
- 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<AbstractHandler> iterator = handlers.iterator(); iterator.hasNext(); ) {
- AbstractHandler handler = iterator.next();
- if (handler.isTerminated()) {
- iterator.remove();
- }
- }
- }
- private void addHandler() {
- try {
- Socket client;
- if(protocol.isSecure())
- client = (SSLSocket) server.accept();
- else
- client = server.accept();
- handlers.add(newInstance(service, this, protocol.getClass()
- .newInstance(), 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 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;
- }
- }
- }
|