HoneyListener.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 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. *
  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. pref = service.getApplicationContext().getSharedPreferences(
  66. MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  67. editor = pref.edit();
  68. conReg = new ConnectionRegister(service);
  69. }
  70. public void run() {
  71. while (!thread.isInterrupted()) {
  72. addHandler();
  73. }
  74. for (HoneyHandler handler : handlers) {
  75. handler.kill();
  76. }
  77. }
  78. /**
  79. * Starts the listener. Creates a server socket runs itself in a new Thread
  80. * and notifies the background service.
  81. */
  82. public void start() {
  83. try {
  84. server = new MyServerSocketFactory().createServerSocket(protocol
  85. .getPort());
  86. (this.thread = new Thread(this)).start();
  87. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  88. editor.commit();
  89. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  90. running = true;
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. /**
  96. * Stops the listener. Closes the server socket, interrupts the Thread its
  97. * running in and notifies the background service.
  98. */
  99. public void stop() {
  100. try {
  101. server.close();
  102. thread.interrupt();
  103. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  104. editor.commit();
  105. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  106. running = false;
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. /**
  112. * Determine the name of the protocol the listener is running on.
  113. *
  114. * @return Name of the protocol
  115. */
  116. public String getProtocolName() {
  117. return protocol.toString();
  118. }
  119. /**
  120. * Remove all terminated handlers from its internal ArrayList.
  121. */
  122. public void refreshHandlers() {
  123. for (Iterator<HoneyHandler> iterator = handlers.iterator(); iterator
  124. .hasNext();) {
  125. HoneyHandler handler = iterator.next();
  126. if (handler.isTerminated()) {
  127. conReg.closeConnection();
  128. iterator.remove();
  129. }
  130. }
  131. }
  132. /**
  133. * Waits for an incoming connection, accepts it and starts a
  134. * {@link HoneyHandler}
  135. */
  136. private void addHandler() {
  137. if (conReg.isConnectionFree()) {
  138. try {
  139. Socket client = server.accept();
  140. conReg.newOpenConnection();
  141. if (protocol.isSecure()) {
  142. startSecureHandler(client);
  143. } else {
  144. startHandler(client);
  145. }
  146. int handlerCount = pref.getInt(protocol
  147. + MainActivity.HANDLER_COUNT, 0);
  148. editor.putInt(protocol + MainActivity.HANDLER_COUNT,
  149. handlerCount + 1);
  150. editor.commit();
  151. service.notifyUI(protocol.toString(),
  152. MainActivity.HANDLER_COUNT);
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. }
  158. /**
  159. * Creates a SSLSocket out of the given socket and starts a
  160. * {@link HoneyHandler}.
  161. *
  162. * @param client
  163. * The socket with the accepted connection.
  164. * @throws Exception
  165. */
  166. private void startSecureHandler(Socket client) throws Exception {
  167. SSLContext sslContext = ((SSLProtocol) protocol).getSSLContext();
  168. SSLSocketFactory factory = sslContext.getSocketFactory();
  169. SSLSocket sslClient = (SSLSocket) factory.createSocket(client, null,
  170. client.getPort(), false);
  171. sslClient.setUseClientMode(false);
  172. handlers.add(newInstance(service, this, protocol.getClass()
  173. .newInstance(), sslClient));
  174. }
  175. /**
  176. * Starts a {@link HoneyHandler} with the given socket.
  177. *
  178. * @param client
  179. * The socket with the accepted connection.
  180. * @throws Exception
  181. */
  182. private void startHandler(Socket client) throws Exception {
  183. handlers.add(newInstance(service, this, protocol.getClass()
  184. .newInstance(), client));
  185. }
  186. /**
  187. * Creates a new instance of an {@link HoneyHandler}.
  188. *
  189. * @param service
  190. * The background service
  191. * @param listener
  192. * The listener that created the handler
  193. * @param protocol
  194. * The Protocol the handler will run on
  195. * @param client
  196. * The Socket the handler uses
  197. * @return A Instance of a {@link HoneyHandler} with the specified
  198. * parameter.
  199. */
  200. private HoneyHandler newInstance(HoneyService service,
  201. HoneyListener listener, Protocol protocol, Socket client) {
  202. return new HoneyHandler(service, listener, protocol, client);
  203. }
  204. }