HoneyListener.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. stopHandler();
  96. thread.interrupt();
  97. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  98. editor.commit();
  99. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  100. running = false;
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. private void stopHandler(){
  106. for(AbstractHandler handler : handlers){
  107. handler.kill();
  108. }
  109. }
  110. /**
  111. * Determine the name of the protocol the listener is running on.
  112. * @return Name of the protocol
  113. */
  114. public String getProtocolName() {
  115. return protocol.toString();
  116. }
  117. /**
  118. * Remove all terminated handlers from its internal ArrayList.
  119. */
  120. public void refreshHandlers() {
  121. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator
  122. .hasNext();) {
  123. AbstractHandler handler = iterator.next();
  124. if (handler.isTerminated()) {
  125. iterator.remove();
  126. }
  127. }
  128. }
  129. /**
  130. * Waits for an incoming connection, accepts it and starts a {@link AbstractHandler}
  131. */
  132. private void addHandler() {
  133. try {
  134. Socket client = server.accept();
  135. if (protocol.isSecure()) {
  136. startSecureHandler(client);
  137. } else {
  138. startHandler(client);
  139. }
  140. int handlerCount = pref.getInt(protocol
  141. + MainActivity.HANDLER_COUNT, 0);
  142. editor.putInt(protocol + MainActivity.HANDLER_COUNT,
  143. handlerCount + 1);
  144. editor.commit();
  145. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. }
  149. }
  150. /**
  151. * Creates a SSLSocket out of the given socket and starts a {@link AbstractHandler}.
  152. * @param client The socket with the accepted connection.
  153. * @throws Exception
  154. */
  155. private void startSecureHandler(Socket client) throws Exception {
  156. SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext();
  157. SSLSocketFactory factory = sslContext.getSocketFactory();
  158. SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null,
  159. client.getPort(), false);
  160. sslClient.setUseClientMode(false);
  161. handlers.add(newInstance(service, this, protocol.getClass()
  162. .newInstance(), sslClient));
  163. }
  164. /**
  165. * Starts a {@link AbstractHandler} with the given socket.
  166. * @param client The socket with the accepted connection.
  167. * @throws Exception
  168. */
  169. private void startHandler(Socket client) throws Exception {
  170. handlers.add(newInstance(service, this, protocol.getClass()
  171. .newInstance(), client));
  172. }
  173. /**
  174. * Creates a new instance of an {@link AbstractHandler}.
  175. * @param service The background service
  176. * @param listener The listener that created the handler
  177. * @param protocol The Protocol the handler will run on
  178. * @param client The Socket the handler uses
  179. * @return A Instance of a {@link AbstractHandler} with the specified parameter.
  180. */
  181. private AbstractHandler newInstance(HoneyService service,
  182. HoneyListener listener, Protocol protocol, Socket client) {
  183. if (protocol.getType().equals(String.class)) {
  184. return new StringHandlerImpl(service, listener, protocol, client);
  185. } else if (protocol.getType().equals(ByteArray.class)) {
  186. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  187. } else {
  188. return null;
  189. }
  190. }
  191. }