HoneyService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Binder;
  10. import android.os.IBinder;
  11. import android.support.v4.app.NotificationCompat;
  12. import android.support.v4.app.TaskStackBuilder;
  13. import android.support.v4.content.LocalBroadcastManager;
  14. import de.tudarmstadt.informatik.hostage.logging.FileLogger;
  15. import de.tudarmstadt.informatik.hostage.logging.Logger;
  16. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  17. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  18. public class HoneyService extends Service {
  19. private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
  20. public List<HoneyListener> getListeners() {
  21. return listeners;
  22. }
  23. private Logger log;
  24. public Logger getLog() {
  25. return log;
  26. }
  27. private final IBinder mBinder = new LocalBinder();
  28. public class LocalBinder extends Binder {
  29. public HoneyService getService() {
  30. return HoneyService.this;
  31. }
  32. }
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. return mBinder;
  36. }
  37. @Override
  38. public void onCreate() {
  39. super.onCreate();
  40. log = new FileLogger(getApplicationContext());
  41. createNotification();
  42. for (Protocol protocol : getProtocolArray()) {
  43. listeners.add(new HoneyListener(this, protocol));
  44. }
  45. }
  46. @Override
  47. public void onDestroy() {
  48. cancelNotification();
  49. log.close();
  50. super.onDestroy();
  51. }
  52. private void createNotification() {
  53. NotificationCompat.Builder builder = new NotificationCompat.Builder(
  54. this).setSmallIcon(R.drawable.ic_launcher)
  55. .setContentTitle(getString(R.string.app_name))
  56. .setContentText("Honeypot up and running!");
  57. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  58. stackBuilder.addParentStack(MainActivity.class);
  59. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  60. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  61. PendingIntent.FLAG_UPDATE_CURRENT);
  62. builder.setContentIntent(resultPendingIntent);
  63. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  64. mNotificationManager.notify(1, builder.build());
  65. }
  66. private void cancelNotification() {
  67. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  68. mNotificationManager.cancel(1);
  69. }
  70. private ArrayList<Protocol> getProtocolArray() {
  71. String[] protocols = getResources().getStringArray(R.array.protocols);
  72. String packageName = Protocol.class.getPackage().getName();
  73. ArrayList<Protocol> protocolArray = new ArrayList<Protocol>();
  74. for (String protocol : protocols) {
  75. try {
  76. protocolArray.add((Protocol) Class.forName(
  77. String.format("%s.%s", packageName, protocol))
  78. .newInstance());
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. return protocolArray;
  84. }
  85. // IPC
  86. public void notifyUI() {
  87. Intent intent = new Intent(MainActivity.BROADCAST);
  88. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  89. }
  90. public void startListeners() {
  91. for (HoneyListener listener : listeners) {
  92. listener.start();
  93. }
  94. }
  95. public void stopListeners() {
  96. for (HoneyListener listener : listeners) {
  97. listener.stop();
  98. }
  99. }
  100. public void startListener(String protocolName) {
  101. for (HoneyListener listener : listeners) {
  102. if (listener.getProtocolName().equals(protocolName)) {
  103. listener.start();
  104. }
  105. }
  106. }
  107. public void stopListener(String protocolName) {
  108. for (HoneyListener listener : listeners) {
  109. if (listener.getProtocolName().equals(protocolName)) {
  110. listener.stop();
  111. }
  112. }
  113. }
  114. public void toggleListener(String protocolName) {
  115. for (HoneyListener listener : listeners) {
  116. if (listener.getProtocolName().equals(protocolName)) {
  117. if (listener.isRunning()) {
  118. stopListener(protocolName);
  119. } else {
  120. startListener(protocolName);
  121. }
  122. }
  123. }
  124. }
  125. }