AbstractHandler.java 4.3 KB

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