HoneyListener.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /**
  21. * Protocol listener class:<br>
  22. * Creates a Socket on the port of a given protocol and listens for incoming connections.<br>
  23. * For each connection creates a Socket and instantiate an {@link AbstractHandler}.
  24. * @author Mihai Plasoianu
  25. *
  26. */
  27. public class HoneyListener implements Runnable {
  28. private ArrayList<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
  29. /**
  30. * Determines the amount of active handlers.
  31. * @return The number of active handlers.
  32. */
  33. public int getHandlerCount() {
  34. return handlers.size();
  35. }
  36. private Protocol protocol;
  37. private ServerSocket server;
  38. private Thread thread;
  39. private HoneyService service;
  40. // Shared Preferences
  41. private SharedPreferences pref;
  42. // Editor for Shared preferences
  43. private Editor editor;
  44. private boolean running = false;
  45. /**
  46. * Determines if the service is running.
  47. * @return True if the service is running, else false.
  48. */
  49. public boolean isRunning() {
  50. return running;
  51. }
  52. /**
  53. * Constructor for the class. Instantiate class variables.
  54. * @param service The Background service that started the listener.
  55. * @param protocol The Protocol on which the listener is running.
  56. */
  57. public HoneyListener(HoneyService service, Protocol protocol) {
  58. this.service = service;
  59. this.protocol = protocol;
  60. pref = service.getApplicationContext().getSharedPreferences(
  61. MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  62. editor = pref.edit();
  63. }
  64. @Override
  65. public void run() {
  66. while (!thread.isInterrupted()) {
  67. addHandler();
  68. }
  69. for (AbstractHandler handler : handlers) {
  70. handler.kill();
  71. }
  72. }
  73. /**
  74. * Starts the listener. Creates a server socket runs itself in a new Thread and notifies the background service.
  75. */
  76. public void start() {
  77. try {
  78. server = new MyServerSocketFactory().createServerSocket(protocol
  79. .getPort());
  80. (this.thread = new Thread(this)).start();
  81. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  82. editor.commit();
  83. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  84. running = true;
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. /**
  90. * Stops the listener. Closes the server socket, interrupts the Thread its running in and notifies the background service.
  91. */
  92. public void stop() {
  93. try {
  94. server.close();
  95. thread.interrupt();
  96. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  97. editor.commit();
  98. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  99. running = false;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. /**
  105. * Determine the name of the protocol the listener is running on.
  106. * @return Name of the protocol
  107. */
  108. public String getProtocolName() {
  109. return protocol.toString();
  110. }
  111. /**
  112. * Remove all terminated handlers from its internal ArrayList.
  113. */
  114. public void refreshHandlers() {
  115. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator
  116. .hasNext();) {
  117. AbstractHandler handler = iterator.next();
  118. if (handler.isTerminated()) {
  119. iterator.remove();
  120. }
  121. }
  122. }
  123. /**
  124. * Waits for an incoming connection, accepts it and starts a {@link AbstractHandler}
  125. */
  126. private void addHandler() {
  127. try {
  128. Socket client = server.accept();
  129. if (protocol.isSecure()) {
  130. startSecureHandler(client);
  131. } else {
  132. startHandler(client);
  133. }
  134. int handlerCount = pref.getInt(protocol
  135. + MainActivity.HANDLER_COUNT, 0);
  136. editor.putInt(protocol + MainActivity.HANDLER_COUNT,
  137. handlerCount + 1);
  138. editor.commit();
  139. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. }
  144. /**
  145. * Creates a SSLSocket out of the given socket and starts a {@link AbstractHandler}.
  146. * @param client The socket with the accepted connection.
  147. * @throws Exception
  148. */
  149. private void startSecureHandler(Socket client) throws Exception {
  150. SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext();
  151. SSLSocketFactory factory = sslContext.getSocketFactory();
  152. SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null,
  153. client.getPort(), false);
  154. sslClient.setUseClientMode(false);
  155. handlers.add(newInstance(service, this, protocol.getClass()
  156. .newInstance(), sslClient));
  157. }
  158. /**
  159. * Starts a {@link AbstractHandler} with the given socket.
  160. * @param client The socket with the accepted connection.
  161. * @throws Exception
  162. */
  163. private void startHandler(Socket client) throws Exception {
  164. handlers.add(newInstance(service, this, protocol.getClass()
  165. .newInstance(), client));
  166. }
  167. /**
  168. * Creates a new instance of an {@link AbstractHandler}.
  169. * @param service The background service
  170. * @param listener The listener that created the handler
  171. * @param protocol The Protocol the handler will run on
  172. * @param client The Socket the handler uses
  173. * @return A Instance of a {@link AbstractHandler} with the specified parameter.
  174. */
  175. private AbstractHandler newInstance(HoneyService service,
  176. HoneyListener listener, Protocol protocol, Socket client) {
  177. if (protocol.getType().equals(String.class)) {
  178. return new StringHandlerImpl(service, listener, protocol, client);
  179. } else if (protocol.getType().equals(ByteArray.class)) {
  180. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  181. } else {
  182. return null;
  183. }
  184. }
  185. }