HoneyListener.java 5.7 KB

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