HoneyListener.java 4.5 KB

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