HoneyListener.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import javax.net.ssl.SSLServerSocket;
  7. import javax.net.ssl.SSLSocket;
  8. import android.content.Context;
  9. import android.content.SharedPreferences;
  10. import android.content.SharedPreferences.Editor;
  11. import de.tudarmstadt.informatik.hostage.handler.AbstractHandler;
  12. import de.tudarmstadt.informatik.hostage.handler.ByteArrayHandlerImpl;
  13. import de.tudarmstadt.informatik.hostage.handler.StringHandlerImpl;
  14. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  15. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  16. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  17. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  18. public class HoneyListener implements Runnable {
  19. private ArrayList<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
  20. public int getHandlerCount() {
  21. return handlers.size();
  22. }
  23. private Protocol protocol;
  24. private ServerSocket server;
  25. private Thread thread;
  26. private HoneyService service;
  27. // Shared Preferences
  28. private SharedPreferences pref;
  29. // Editor for Shared preferences
  30. private Editor editor;
  31. private boolean running = false;
  32. public boolean isRunning() {
  33. return running;
  34. }
  35. public HoneyListener(HoneyService service, Protocol protocol) {
  36. this.service = service;
  37. this.protocol = protocol;
  38. pref = service.getApplicationContext().getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  39. editor = pref.edit();
  40. }
  41. @Override
  42. public void run() {
  43. while (!thread.isInterrupted()) {
  44. addHandler();
  45. }
  46. for (AbstractHandler handler : handlers) {
  47. handler.kill();
  48. }
  49. }
  50. public void start() {
  51. try {
  52. if(protocol.isSecure())
  53. //TODO
  54. // server = new MyServerSocketFactory().createServerSocket(
  55. // protocol.getPort(), protocol.getSSLContext());
  56. server = (SSLServerSocket) protocol.getSSLContext().getServerSocketFactory().createServerSocket(protocol.getPort());
  57. else
  58. server = new MyServerSocketFactory().createServerSocket(protocol
  59. .getPort());
  60. (this.thread = new Thread(this)).start();
  61. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  62. editor.commit();
  63. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  64. running = true;
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. public void stop() {
  70. try {
  71. server.close();
  72. thread.interrupt();
  73. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  74. editor.commit();
  75. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  76. running = false;
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. public String getProtocolName() {
  82. return protocol.toString();
  83. }
  84. public void refreshHandlers() {
  85. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator.hasNext(); ) {
  86. AbstractHandler handler = iterator.next();
  87. if (handler.isTerminated()) {
  88. iterator.remove();
  89. }
  90. }
  91. }
  92. private void addHandler() {
  93. try {
  94. Socket client;
  95. if(protocol.isSecure())
  96. client = (SSLSocket) server.accept();
  97. else
  98. client = server.accept();
  99. handlers.add(newInstance(service, this, protocol.getClass()
  100. .newInstance(), client));
  101. int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  102. editor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount + 1);
  103. editor.commit();
  104. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. private AbstractHandler newInstance(HoneyService service,
  110. HoneyListener listener, Protocol protocol, Socket client) {
  111. if (protocol.getType().equals(String.class)) {
  112. return new StringHandlerImpl(service, listener, protocol, client);
  113. } else if (protocol.getType().equals(ByteArray.class)) {
  114. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  115. } else {
  116. return null;
  117. }
  118. }
  119. }