HoneyListener.java 6.2 KB

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