HoneyService.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 android.widget.Toast;
  15. import de.tudarmstadt.informatik.hostage.logging.FileLogger;
  16. import de.tudarmstadt.informatik.hostage.logging.Logger;
  17. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  18. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  19. public class HoneyService extends Service {
  20. private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
  21. public List<HoneyListener> getListeners() {
  22. return listeners;
  23. }
  24. private Logger log;
  25. public Logger getLog() {
  26. return log;
  27. }
  28. private final IBinder mBinder = new LocalBinder();
  29. public class LocalBinder extends Binder {
  30. public HoneyService getService() {
  31. return HoneyService.this;
  32. }
  33. }
  34. @Override
  35. public IBinder onBind(Intent intent) {
  36. return mBinder;
  37. }
  38. @Override
  39. public void onCreate() {
  40. super.onCreate();
  41. log = new FileLogger(getApplicationContext());
  42. createNotification();
  43. for (Protocol protocol : getProtocolArray()) {
  44. listeners.add(new HoneyListener(this, protocol));
  45. }
  46. }
  47. @Override
  48. public int onStartCommand(Intent intent, int flags, int startId) {
  49. // We want this service to continue running until it is explicitly
  50. // stopped, so return sticky.
  51. return START_STICKY;
  52. }
  53. @Override
  54. public void onDestroy() {
  55. cancelNotification();
  56. log.close();
  57. super.onDestroy();
  58. }
  59. private void createNotification() {
  60. NotificationCompat.Builder builder = new NotificationCompat.Builder(
  61. this).setSmallIcon(R.drawable.ic_launcher)
  62. .setContentTitle(getString(R.string.app_name))
  63. .setContentText("Honeypot up and running!");
  64. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
  65. stackBuilder.addParentStack(MainActivity.class);
  66. stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
  67. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
  68. PendingIntent.FLAG_UPDATE_CURRENT);
  69. builder.setContentIntent(resultPendingIntent);
  70. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  71. mNotificationManager.notify(1, builder.build());
  72. }
  73. private void cancelNotification() {
  74. NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  75. mNotificationManager.cancel(1);
  76. }
  77. private ArrayList<Protocol> getProtocolArray() {
  78. String[] protocols = getResources().getStringArray(R.array.protocols);
  79. String packageName = Protocol.class.getPackage().getName();
  80. ArrayList<Protocol> protocolArray = new ArrayList<Protocol>();
  81. for (String protocol : protocols) {
  82. try {
  83. protocolArray.add((Protocol) Class.forName(
  84. String.format("%s.%s", packageName, protocol))
  85. .newInstance());
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. return protocolArray;
  91. }
  92. public boolean hasRunningListeners(){
  93. for (HoneyListener listener : listeners) {
  94. if (listener.isRunning())
  95. return true;
  96. }
  97. return false;
  98. }
  99. // IPC
  100. public void notifyUI() {
  101. Intent intent = new Intent(MainActivity.BROADCAST);
  102. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  103. }
  104. public void startListeners() {
  105. for (HoneyListener listener : listeners) {
  106. listener.start();
  107. }
  108. Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_LONG).show();
  109. }
  110. public void stopListeners() {
  111. for (HoneyListener listener : listeners) {
  112. listener.stop();
  113. }
  114. notifyUI();
  115. Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_LONG).show();
  116. }
  117. public void startListener(String protocolName) {
  118. for (HoneyListener listener : listeners) {
  119. if (listener.getProtocolName().equals(protocolName)) {
  120. listener.start();
  121. }
  122. }
  123. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_LONG).show();
  124. }
  125. public void stopListener(String protocolName) {
  126. for (HoneyListener listener : listeners) {
  127. if (listener.getProtocolName().equals(protocolName)) {
  128. listener.stop();
  129. }
  130. }
  131. Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_LONG).show();
  132. }
  133. public void toggleListener(String protocolName) {
  134. for (HoneyListener listener : listeners) {
  135. if (listener.getProtocolName().equals(protocolName)) {
  136. if (listener.isRunning()) {
  137. stopListener(protocolName);
  138. } else {
  139. startListener(protocolName);
  140. }
  141. }
  142. }
  143. }
  144. }