AbstractHandler.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package de.tudarmstadt.informatik.hostage.handler;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import android.content.SharedPreferences;
  7. import android.content.SharedPreferences.Editor;
  8. import android.preference.PreferenceManager;
  9. import de.tudarmstadt.informatik.hostage.HoneyListener;
  10. import de.tudarmstadt.informatik.hostage.HoneyService;
  11. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  12. import de.tudarmstadt.informatik.hostage.logging.Logger;
  13. import de.tudarmstadt.informatik.hostage.logging.Record;
  14. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  15. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  16. public abstract class AbstractHandler implements Runnable {
  17. /**
  18. * Time until the socket throws a time out. The time is in milliseconds.
  19. */
  20. private int TIMEOUT;
  21. protected Protocol protocol;
  22. private Socket client;
  23. protected Thread thread;
  24. private int attack_id;
  25. private String BSSID;
  26. private String SSID;
  27. private HoneyListener listener;
  28. protected Logger log;
  29. /**
  30. * Constructor of the class.
  31. * Initializes class variables for communication and logging.
  32. * Then starts itself in a new Thread.
  33. * @param service The background service.
  34. * @param listener The Listener that called the service.
  35. * @param protocol The protocol on which the handler is running.
  36. * @param client A Socket for the communication with a remote client.
  37. */
  38. public AbstractHandler(HoneyService service, HoneyListener listener,
  39. Protocol protocol, Socket client) {
  40. this.listener = listener;
  41. this.log = service.getLog();
  42. this.protocol = protocol;
  43. this.client = client;
  44. this.thread = new Thread(this);
  45. SharedPreferences pref = PreferenceManager
  46. .getDefaultSharedPreferences(service);
  47. Editor editor = pref.edit();
  48. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  49. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  50. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  51. editor.commit();
  52. BSSID = HelperUtils.getBSSID(service.getApplicationContext());
  53. SSID = HelperUtils.getSSID(service.getApplicationContext());
  54. setSoTimeout(client);
  55. thread.start();
  56. }
  57. /**
  58. * Set the timeout of the socket to the hard coded time out variable.
  59. * @param client The socket
  60. * @see #TIMEOUT
  61. */
  62. private void setSoTimeout(Socket client) {
  63. try {
  64. client.setSoTimeout(TIMEOUT);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * Creates InputStream and OutputStream for the socket.
  71. * Starts communication with client.
  72. * When the client closes the connection or a time out occurs the handler is finished.
  73. */
  74. @Override
  75. public void run() {
  76. InputStream in;
  77. OutputStream out;
  78. try {
  79. in = client.getInputStream();
  80. out = client.getOutputStream();
  81. talkToClient(in, out);
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. kill();
  86. }
  87. /**
  88. * Sets the interrupt flag of the thread and tries to close the socket.
  89. */
  90. public void kill() {
  91. thread.interrupt();
  92. try {
  93. client.close();
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. listener.refreshHandlers();
  98. }
  99. /**
  100. * Determines if the interrupt flag of the thread is set.
  101. * @return True when the flag is set, else false.
  102. */
  103. public boolean isTerminated() {
  104. return thread.isInterrupted();
  105. }
  106. /**
  107. * Communicates with a client using the corresponding protocol implementation.
  108. * @param in InputStream of the socket.
  109. * @param out OutputStream of the socket.
  110. * @throws IOException
  111. */
  112. abstract protected void talkToClient(InputStream in, OutputStream out)
  113. throws IOException;
  114. /**
  115. * Creates a Record for a message exchanged with a client.
  116. * @param type The type of the message.
  117. * @param packet The content of the message.
  118. * @return The Record representing the communication message.
  119. */
  120. protected Record createRecord(TYPE type, String packet) {
  121. Record record = new Record();
  122. record.setAttack_id(attack_id);
  123. record.setProtocol(protocol.toString());
  124. record.setType(type);
  125. record.setTimestamp(System.currentTimeMillis());
  126. record.setLocalIP(client.getLocalAddress());
  127. record.setLocalPort(protocol.getPort());
  128. record.setRemoteIP(client.getInetAddress());
  129. record.setRemotePort(client.getPort());
  130. record.setBSSID(BSSID);
  131. record.setSSID(SSID);
  132. record.setPacket(packet);
  133. return record;
  134. }
  135. }