HoneyService.java 4.7 KB

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