HoneyListener.java 6.2 KB

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