HoneyListener.java 5.7 KB

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