HoneyListener.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import de.tudarmstadt.informatik.hostage.handler.AbstractHandler;
  10. import de.tudarmstadt.informatik.hostage.handler.ByteArrayHandlerImpl;
  11. import de.tudarmstadt.informatik.hostage.handler.StringHandlerImpl;
  12. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  13. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  14. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  15. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  16. public class HoneyListener implements Runnable {
  17. private ArrayList<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
  18. public int getHandlerCount() {
  19. return handlers.size();
  20. }
  21. private Protocol protocol;
  22. private ServerSocket server;
  23. private Thread thread;
  24. private HoneyService service;
  25. // Shared Preferences
  26. private SharedPreferences pref;
  27. // Editor for Shared preferences
  28. private Editor editor;
  29. private boolean running = false;
  30. public boolean isRunning() {
  31. return running;
  32. }
  33. public HoneyListener(HoneyService service, Protocol protocol) {
  34. this.service = service;
  35. this.protocol = protocol;
  36. pref = service.getApplicationContext().getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  37. editor = pref.edit();
  38. }
  39. @Override
  40. public void run() {
  41. while (!thread.isInterrupted()) {
  42. addHandler();
  43. }
  44. for (AbstractHandler handler : handlers) {
  45. handler.kill();
  46. }
  47. }
  48. public void start() {
  49. try {
  50. server = new MyServerSocketFactory().createServerSocket(protocol
  51. .getPort());
  52. (this.thread = new Thread(this)).start();
  53. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  54. editor.commit();
  55. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  56. running = true;
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. public void stop() {
  62. try {
  63. server.close();
  64. thread.interrupt();
  65. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  66. editor.commit();
  67. service.notifyUI(protocol.toString(), MainActivity.LISTENER);
  68. running = false;
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. public String getProtocolName() {
  74. return protocol.toString();
  75. }
  76. public void refreshHandlers() {
  77. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator.hasNext(); ) {
  78. AbstractHandler handler = iterator.next();
  79. if (handler.isTerminated()) {
  80. iterator.remove();
  81. }
  82. }
  83. }
  84. private void addHandler() {
  85. try {
  86. Socket client = server.accept();
  87. handlers.add(newInstance(service, this, protocol.getClass()
  88. .newInstance(), client));
  89. int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  90. editor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount + 1);
  91. editor.commit();
  92. service.notifyUI(protocol.toString(), MainActivity.HANDLER_COUNT);
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. private AbstractHandler newInstance(HoneyService service,
  98. HoneyListener listener, Protocol protocol, Socket client) {
  99. if (protocol.getType().equals(String.class)) {
  100. return new StringHandlerImpl(service, listener, protocol, client);
  101. } else if (protocol.getType().equals(ByteArray.class)) {
  102. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  103. } else {
  104. return null;
  105. }
  106. }
  107. }