HoneyListener.java 4.6 KB

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