package de.tudarmstadt.informatik.hostage.logging; import android.app.IntentService; import android.content.Context; import android.content.Intent; import android.os.Parcelable; import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE; import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper; public class Logger extends IntentService { private static final String ACTION_LOG_MESSAGE = "de.tudarmstadt.informatik.hostage.action.LOG_MESSAGE"; private static final String ACTION_LOG_ATTACK = "de.tudarmstadt.informatik.hostage.action.LOG_ATTACK"; private static final String ACTION_LOG_NETWORK = "de.tudarmstadt.informatik.hostage.action.LOG_NETWORK"; private static final String ACTION_LOG_PORTSCAN = "de.tudarmstadt.informatik.hostage.action.LOG_PORTSCAN"; private static final String EXTRA_RECORD = "de.tudarmstadt.informatik.hostage.extra.RECORD"; private static final String EXTRA_TIMESTAMP = "de.tudarmstadt.informatik.hostage.extra.TIMESTAMP"; public static void log(Context context, MessageRecord record) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_LOG_MESSAGE); intent.putExtra(EXTRA_RECORD, (Parcelable)record); context.startService(intent); } public static void log(Context context, AttackRecord record) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_LOG_ATTACK); intent.putExtra(EXTRA_RECORD, (Parcelable)record); context.startService(intent); } public static void log(Context context, NetworkRecord record) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_LOG_NETWORK); intent.putExtra(EXTRA_RECORD, (Parcelable)record); context.startService(intent); } public static void logPortscan(Context context, AttackRecord record, long timestamp){ Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_LOG_PORTSCAN); intent.putExtra(EXTRA_RECORD, (Parcelable)record); intent.putExtra(EXTRA_TIMESTAMP, timestamp); context.startService(intent); } private HostageDBOpenHelper mDbHelper; public Logger() { super("Logger"); } @Override public void onCreate() { super.onCreate(); mDbHelper = new HostageDBOpenHelper(getApplicationContext()); } private void handleActionLog(MessageRecord record) { mDbHelper.addMessageRecord(record); } private void handleActionLog(AttackRecord record) { mDbHelper.addAttackRecord(record); mDbHelper.updateSyncAttackCounter(record); } private void handleActionLog(NetworkRecord record) { mDbHelper.updateNetworkInformation(record); } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (ACTION_LOG_MESSAGE.equals(action)) { final MessageRecord record = intent.getParcelableExtra(EXTRA_RECORD); handleActionLog(record); }else if(ACTION_LOG_ATTACK.equals(action)){ final AttackRecord record = intent.getParcelableExtra(EXTRA_RECORD); handleActionLog(record); }else if(ACTION_LOG_NETWORK.equals(action)){ final NetworkRecord record = intent.getParcelableExtra(EXTRA_RECORD); handleActionLog(record); }else if(ACTION_LOG_PORTSCAN.equals(action)){ final AttackRecord record = intent.getParcelableExtra(EXTRA_RECORD); handleActionLog(record); MessageRecord mRecord = new MessageRecord(); mRecord.setAttack_id(record.getAttack_id()); mRecord.setId(0); mRecord.setPacket(""); mRecord.setTimestamp(intent.getLongExtra(EXTRA_TIMESTAMP, 0)); mRecord.setType(TYPE.RECEIVE); } } } }