Handler.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.util.List;
  7. import android.content.Context;
  8. import android.content.SharedPreferences;
  9. import android.content.SharedPreferences.Editor;
  10. import android.preference.PreferenceManager;
  11. import android.util.Log;
  12. import de.tudarmstadt.informatik.hostage.location.MyLocationManager;
  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.nio.Reader;
  17. import de.tudarmstadt.informatik.hostage.nio.Writer;
  18. import de.tudarmstadt.informatik.hostage.protocol.GHOST;
  19. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  20. import de.tudarmstadt.informatik.hostage.protocol.Protocol.TALK_FIRST;
  21. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  22. /**
  23. * Abstract class for a connection handler using a given protocol.
  24. *
  25. * @author Mihai Plasoianu
  26. * @author Wulf Pfeiffer
  27. */
  28. public class Handler implements Runnable {
  29. /** Time until the socket throws a time out. The time is in milliseconds. */
  30. private int TIMEOUT;
  31. private Hostage service;
  32. protected Protocol protocol;
  33. private Socket client;
  34. protected Thread thread;
  35. private int attack_id;
  36. private int message_id = 0;
  37. private String externalIP;
  38. private String BSSID;
  39. private String SSID;
  40. private Listener listener;
  41. /**
  42. * Constructor of the class. Initializes class variables for communication
  43. * and logging. Then starts itself in a new Thread.
  44. *
  45. * @param service
  46. * The background service.
  47. * @param listener
  48. * The Listener that called the service.
  49. * @param protocol
  50. * The protocol on which the handler is running.
  51. * @param client
  52. * A Socket for the communication with a remote client.
  53. */
  54. public Handler(Hostage service, Listener listener, Protocol protocol, Socket client) {
  55. this.service = service;
  56. this.listener = listener;
  57. this.protocol = protocol;
  58. if (protocol.toString().equals("GHOST")) {
  59. ((GHOST) protocol).setAttackerIP(client.getInetAddress());
  60. ((GHOST) protocol).setCurrentPort(listener.getPort());
  61. }
  62. this.client = client;
  63. this.thread = new Thread(this);
  64. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(service);
  65. TIMEOUT = pref.getInt("timeout", 30) * 1000;
  66. // TODO ThreadSicher?
  67. getAndIncrementAttackID(pref);
  68. SharedPreferences connInfo = service.getSharedPreferences(service.getString(R.string.connection_info), Context.MODE_PRIVATE);
  69. BSSID = connInfo.getString(service.getString(R.string.connection_info_bssid), null);
  70. SSID = connInfo.getString(service.getString(R.string.connection_info_ssid), null);
  71. externalIP = connInfo.getString(service.getString(R.string.connection_info_external_ip), null);
  72. setSoTimeout(client);
  73. thread.start();
  74. }
  75. /**
  76. * Determines if the interrupt flag of the thread is set.
  77. *
  78. * @return True when the flag is set, else false.
  79. */
  80. public boolean isTerminated() {
  81. return thread.isInterrupted();
  82. }
  83. /**
  84. * Sets the interrupt flag of the thread and tries to close the socket.
  85. */
  86. public void kill() {
  87. service.notifyUI(this.getClass().getName(),
  88. new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(listener.getPort()) });
  89. thread.interrupt();
  90. try {
  91. client.close();
  92. Log.i("HoneyHandler", "Socket closed: " + client.isClosed());
  93. } catch (IOException e) {
  94. }
  95. listener.refreshHandlers();
  96. }
  97. /**
  98. * Creates InputStream and OutputStream for the socket. Starts communication
  99. * with client. When the client closes the connection or a time out occurs
  100. * the handler is finished.
  101. */
  102. @Override
  103. public void run() {
  104. service.notifyUI(this.getClass().getName(),
  105. new String[] { service.getString(R.string.broadcast_started), protocol.toString(), Integer.toString(listener.getPort()) });
  106. InputStream in;
  107. OutputStream out;
  108. try {
  109. in = client.getInputStream();
  110. out = client.getOutputStream();
  111. talkToClient(in, out);
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. kill();
  116. }
  117. /**
  118. * Gets attack ID for the attack. Also increases the attack ID counter by
  119. * one. Method is synchronized for thread safety.
  120. *
  121. * @param pref
  122. * The default SharedPreference of the application
  123. * @return Unique integer attack ID
  124. */
  125. private synchronized void getAndIncrementAttackID(SharedPreferences pref) {
  126. Editor editor = pref.edit();
  127. attack_id = pref.getInt("ATTACK_ID_COUNTER", 0);
  128. editor.putInt("ATTACK_ID_COUNTER", attack_id + 1);
  129. editor.commit();
  130. }
  131. /**
  132. * Set the timeout of the socket to the hard coded time out variable.
  133. *
  134. * @param client
  135. * The socket
  136. * @see #TIMEOUT
  137. */
  138. private void setSoTimeout(Socket client) {
  139. try {
  140. client.setSoTimeout(TIMEOUT);
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. }
  144. }
  145. /**
  146. * Creates a Record for a message exchanged with a client.
  147. *
  148. * @param type
  149. * The type of the message.
  150. * @param packet
  151. * The content of the message.
  152. * @return The Record representing the communication message.
  153. */
  154. protected Record createRecord(TYPE type, String packet) {
  155. Record record = new Record();
  156. record.setId(message_id++);
  157. record.setAttack_id(attack_id);
  158. record.setProtocol(protocol.toString());
  159. record.setType(type);
  160. record.setTimestamp(System.currentTimeMillis());
  161. record.setExternalIP(externalIP);
  162. record.setLocalIP(client.getLocalAddress().getHostAddress());
  163. record.setLocalPort(protocol.getPort());
  164. record.setRemoteIP(client.getInetAddress().getHostAddress());
  165. record.setRemotePort(client.getPort());
  166. record.setBssid(BSSID);
  167. record.setSsid(SSID);
  168. record.setPacket(packet);
  169. if (MyLocationManager.getNewestLocation() != null) {
  170. record.setLatitude(MyLocationManager.getNewestLocation().getLatitude());
  171. record.setLongitude(MyLocationManager.getNewestLocation().getLongitude());
  172. record.setAccuracy(MyLocationManager.getNewestLocation().getAccuracy());
  173. record.setTimestampLocation(MyLocationManager.getNewestLocation().getTime());
  174. } else {
  175. record.setLatitude(0.0);
  176. record.setLongitude(0.0);
  177. record.setAccuracy(Float.MAX_VALUE);
  178. record.setTimestampLocation(0);
  179. }
  180. return record;
  181. }
  182. /**
  183. * Communicates with a client using the corresponding protocol
  184. * implementation.
  185. *
  186. * @param in
  187. * InputStream of the socket.
  188. * @param out
  189. * OutputStream of the socket.
  190. * @throws IOException
  191. */
  192. protected void talkToClient(InputStream in, OutputStream out) throws IOException {
  193. Reader reader = new Reader(in, protocol.toString());
  194. Writer writer = new Writer(out);
  195. Packet inputLine;
  196. List<Packet> outputLine;
  197. if (protocol.whoTalksFirst() == TALK_FIRST.SERVER) {
  198. outputLine = protocol.processMessage(null);
  199. writer.write(outputLine);
  200. for (Packet o : outputLine) {
  201. Logger.log(Hostage.getContext(), createRecord(TYPE.SEND, o.toString()));
  202. }
  203. }
  204. while (!thread.isInterrupted() && (inputLine = reader.read()) != null) {
  205. outputLine = protocol.processMessage(inputLine);
  206. Logger.log(Hostage.getContext(), createRecord(TYPE.RECEIVE, inputLine.toString()));
  207. if (outputLine != null) {
  208. writer.write(outputLine);
  209. for (Packet o : outputLine) {
  210. Logger.log(Hostage.getContext(), createRecord(TYPE.SEND, o.toString()));
  211. }
  212. }
  213. if (protocol.isClosed()) {
  214. break;
  215. }
  216. }
  217. }
  218. }