HoneyListener.java 6.4 KB

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