Handler.java 8.3 KB

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