HoneyListener.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.SSLSocket;
  8. import javax.net.ssl.SSLSocketFactory;
  9. import android.content.Context;
  10. import android.content.SharedPreferences;
  11. import android.content.SharedPreferences.Editor;
  12. import de.tudarmstadt.informatik.hostage.handler.AbstractHandler;
  13. import de.tudarmstadt.informatik.hostage.handler.ByteArrayHandlerImpl;
  14. import de.tudarmstadt.informatik.hostage.handler.StringHandlerImpl;
  15. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  16. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  17. import de.tudarmstadt.informatik.hostage.protocol.SSLProtocol;
  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. server = new MyServerSocketFactory().createServerSocket(protocol
  56. .getPort());
  57. (this.thread = new Thread(this)).start();
  58. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  59. editor.commit();
  60. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  61. running = true;
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. public void stop() {
  67. try {
  68. server.close();
  69. thread.interrupt();
  70. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  71. editor.commit();
  72. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  73. running = false;
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. public String getProtocolName() {
  79. return protocol.toString();
  80. }
  81. public void refreshHandlers() {
  82. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator
  83. .hasNext();) {
  84. AbstractHandler handler = iterator.next();
  85. if (handler.isTerminated()) {
  86. iterator.remove();
  87. }
  88. }
  89. }
  90. private void addHandler() {
  91. try {
  92. Socket client = server.accept();
  93. if (protocol.isSecure()) {
  94. startSecureHandler(client);
  95. } else {
  96. startHandler(client);
  97. }
  98. int handlerCount = pref.getInt(protocol
  99. + MainActivity.HANDLER_COUNT, 0);
  100. editor.putInt(protocol + MainActivity.HANDLER_COUNT,
  101. handlerCount + 1);
  102. editor.commit();
  103. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  104. } catch (Exception e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. private void startSecureHandler(Socket client) throws Exception {
  109. SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext();
  110. SSLSocketFactory factory = sslContext.getSocketFactory();
  111. SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null,
  112. client.getPort(), false);
  113. sslClient.setUseClientMode(false);
  114. handlers.add(newInstance(service, this, protocol.getClass()
  115. .newInstance(), sslClient));
  116. }
  117. private void startHandler(Socket client) throws Exception {
  118. handlers.add(newInstance(service, this, protocol.getClass()
  119. .newInstance(), client));
  120. }
  121. private AbstractHandler newInstance(HoneyService service,
  122. HoneyListener listener, Protocol protocol, Socket client) {
  123. if (protocol.getType().equals(String.class)) {
  124. return new StringHandlerImpl(service, listener, protocol, client);
  125. } else if (protocol.getType().equals(ByteArray.class)) {
  126. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  127. } else {
  128. return null;
  129. }
  130. }
  131. }