123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- package de.tudarmstadt.informatik.hostage;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.support.v4.app.NotificationCompat;
- import android.support.v4.app.TaskStackBuilder;
- import android.support.v4.content.LocalBroadcastManager;
- import android.widget.Toast;
- import de.tudarmstadt.informatik.hostage.logging.FileLogger;
- import de.tudarmstadt.informatik.hostage.logging.Logger;
- import de.tudarmstadt.informatik.hostage.protocol.Protocol;
- import de.tudarmstadt.informatik.hostage.ui.MainActivity;
- public class HoneyService extends Service {
- private ArrayList<HoneyListener> listeners = new ArrayList<HoneyListener>();
- public List<HoneyListener> getListeners() {
- return listeners;
- }
- private Logger log;
- public Logger getLog() {
- return log;
- }
- private final IBinder mBinder = new LocalBinder();
- public class LocalBinder extends Binder {
- public HoneyService getService() {
- return HoneyService.this;
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- log = new FileLogger(getApplicationContext());
- createNotification();
- for (Protocol protocol : getProtocolArray()) {
- listeners.add(new HoneyListener(this, protocol));
- }
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // We want this service to continue running until it is explicitly
- // stopped, so return sticky.
- return START_STICKY;
- }
-
- @Override
- public void onDestroy() {
- cancelNotification();
- log.close();
- super.onDestroy();
- }
- private void createNotification() {
- NotificationCompat.Builder builder = new NotificationCompat.Builder(
- this).setSmallIcon(R.drawable.ic_launcher)
- .setContentTitle(getString(R.string.app_name))
- .setContentText("Honeypot up and running!");
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
- stackBuilder.addParentStack(MainActivity.class);
- stackBuilder.addNextIntent(new Intent(this, MainActivity.class));
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
- PendingIntent.FLAG_UPDATE_CURRENT);
- builder.setContentIntent(resultPendingIntent);
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.notify(1, builder.build());
- }
- private void cancelNotification() {
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.cancel(1);
- }
- private ArrayList<Protocol> getProtocolArray() {
- String[] protocols = getResources().getStringArray(R.array.protocols);
- String packageName = Protocol.class.getPackage().getName();
- ArrayList<Protocol> protocolArray = new ArrayList<Protocol>();
- for (String protocol : protocols) {
- try {
- protocolArray.add((Protocol) Class.forName(
- String.format("%s.%s", packageName, protocol))
- .newInstance());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return protocolArray;
- }
-
- public boolean hasRunningListeners(){
- for (HoneyListener listener : listeners) {
- if (listener.isRunning())
- return true;
- }
- return false;
- }
- // IPC
- public void notifyUI() {
- Intent intent = new Intent(MainActivity.BROADCAST);
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- }
- public void startListeners() {
- for (HoneyListener listener : listeners) {
- listener.start();
- }
- Toast.makeText(getApplicationContext(), "SERVICES STARTED!", Toast.LENGTH_LONG).show();
- }
- public void stopListeners() {
- for (HoneyListener listener : listeners) {
- listener.stop();
- }
- notifyUI();
- Toast.makeText(getApplicationContext(), "SERVICES STOPPED!", Toast.LENGTH_LONG).show();
- }
- public void startListener(String protocolName) {
- for (HoneyListener listener : listeners) {
- if (listener.getProtocolName().equals(protocolName)) {
- listener.start();
- }
- }
- Toast.makeText(getApplicationContext(), protocolName + " SERVICE STARTED!", Toast.LENGTH_LONG).show();
- }
- public void stopListener(String protocolName) {
- for (HoneyListener listener : listeners) {
- if (listener.getProtocolName().equals(protocolName)) {
- listener.stop();
- }
- }
- Toast.makeText(getApplicationContext(), protocolName + " SERVICE STOPPED!", Toast.LENGTH_LONG).show();
- }
- public void toggleListener(String protocolName) {
- for (HoneyListener listener : listeners) {
- if (listener.getProtocolName().equals(protocolName)) {
- if (listener.isRunning()) {
- stopListener(protocolName);
- } else {
- startListener(protocolName);
- }
- }
- }
- }
- }
|