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