Handler.java 8.4 KB

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