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