HoneyListener.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. private ConnectionRegister conReg;
  43. // Editor for Shared preferences
  44. private Editor editor;
  45. private boolean running = false;
  46. /**
  47. * Determines if the service is running.
  48. * @return True if the service is running, else false.
  49. */
  50. public boolean isRunning() {
  51. return running;
  52. }
  53. /**
  54. * Constructor for the class. Instantiate class variables.
  55. * @param service The Background service that started the listener.
  56. * @param protocol The Protocol on which the listener is running.
  57. */
  58. public HoneyListener(HoneyService service, Protocol protocol) {
  59. this.service = service;
  60. this.protocol = protocol;
  61. pref = service.getApplicationContext().getSharedPreferences(
  62. MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  63. editor = pref.edit();
  64. conReg = new ConnectionRegister(service);
  65. }
  66. @Override
  67. public void run() {
  68. while (!thread.isInterrupted()) {
  69. addHandler();
  70. }
  71. for (AbstractHandler handler : handlers) {
  72. handler.kill();
  73. }
  74. }
  75. /**
  76. * Starts the listener. Creates a server socket runs itself in a new Thread and notifies the background service.
  77. */
  78. public void start() {
  79. try {
  80. server = new MyServerSocketFactory().createServerSocket(protocol
  81. .getPort());
  82. (this.thread = new Thread(this)).start();
  83. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  84. editor.commit();
  85. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  86. running = true;
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. /**
  92. * Stops the listener. Closes the server socket, interrupts the Thread its running in and notifies the background service.
  93. */
  94. public void stop() {
  95. try {
  96. server.close();
  97. thread.interrupt();
  98. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  99. editor.commit();
  100. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  101. running = false;
  102. } catch (Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. /**
  107. * Determine the name of the protocol the listener is running on.
  108. * @return Name of the protocol
  109. */
  110. public String getProtocolName() {
  111. return protocol.toString();
  112. }
  113. /**
  114. * Remove all terminated handlers from its internal ArrayList.
  115. */
  116. public void refreshHandlers() {
  117. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator
  118. .hasNext();) {
  119. AbstractHandler handler = iterator.next();
  120. if (handler.isTerminated()) {
  121. conReg.closeConnection();
  122. iterator.remove();
  123. }
  124. }
  125. }
  126. /**
  127. * Waits for an incoming connection, accepts it and starts a {@link AbstractHandler}
  128. */
  129. private void addHandler() {
  130. if(conReg.isConnectionFree()) {
  131. try {
  132. Socket client = server.accept();
  133. conReg.newOpenConnection();
  134. if (protocol.isSecure()) {
  135. startSecureHandler(client);
  136. } else {
  137. startHandler(client);
  138. }
  139. int handlerCount = pref.getInt(protocol
  140. + MainActivity.HANDLER_COUNT, 0);
  141. editor.putInt(protocol + MainActivity.HANDLER_COUNT,
  142. handlerCount + 1);
  143. editor.commit();
  144. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. }
  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. }