Skip to content

Commit

Permalink
DeviceInfo and Beacon logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Aug 20, 2020
1 parent 2b417b5 commit 6dc5e47
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 12 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Expand Up @@ -108,6 +108,8 @@ dependencies {
// https://mvnrepository.com/artifact/commons-io/commons-io
implementation group: 'commons-io', name: 'commons-io', version: '2.6'

//device-Names
implementation 'com.jaredrummler:android-device-names:2.0.0'

//materialDesign
implementation 'com.google.android.material:material:1.3.0-alpha02'
Expand Down
Expand Up @@ -15,31 +15,23 @@


public class JSONHelper {
private static final String PERSIST_FILENAME = "publish.json";
File file = new File("/data/data/" + MainActivity.getContext().getPackageName() + "/" + PERSIST_FILENAME);


public void persistData(ArrayList<RecordAll> records){
public void jsonWriter(JSONArray arr,File file){
try {
int BUFFER_SIZE = 8192;
String UTF8 = "utf8";
FileOutputStream fout = new FileOutputStream(file);
BufferedWriter fnw = new BufferedWriter(new OutputStreamWriter(fout, UTF8), BUFFER_SIZE);

JSONArray arr = new JSONArray();
for(RecordAll record: records) {
arr.put(record.toJSON());
}
fnw.write(arr.toString());
fnw.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}

}

public String getFilePath(){
public String getFilePath(File file){
return file.getAbsolutePath();
}
}
@@ -0,0 +1,40 @@
package de.tudarmstadt.informatik.hostage.event;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import de.tudarmstadt.informatik.hostage.commons.JSONHelper;
import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;

public class BeaconEvent implements Event {
private static final String PERSIST_FILENAME = "beaconInfo.json";
File file = new File("/data/data/" + MainActivity.getContext().getPackageName() + "/" + PERSIST_FILENAME);

@Override
public JSONObject toJSON() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("deviceid",deviceId);
jsonObject.put("open","working");

} catch (JSONException e) {
e.printStackTrace();
}

return jsonObject;
}
private JSONArray persistData() {
JSONArray array = new JSONArray();
array.put(this.toJSON());

return array;
}
@Override
public void writeData() {
JSONHelper jsonHelper = new JSONHelper();
jsonHelper.jsonWriter(persistData(),file);
}
}
@@ -0,0 +1,64 @@
package de.tudarmstadt.informatik.hostage.event;

import android.content.Context;

import com.jaredrummler.android.device.DeviceName;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import de.tudarmstadt.informatik.hostage.commons.JSONHelper;
import de.tudarmstadt.informatik.hostage.system.Device;
import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;

public class DeviceEvent implements Event{
Context context;
String manufacturer;
String model;
String deviceName;
private static final String PERSIST_FILENAME = "deviceInfo.json";
File file = new File("/data/data/" + MainActivity.getContext().getPackageName() + "/" + PERSIST_FILENAME);

public DeviceEvent(Context context){
this.context = context;
}

public void getDeviceInfo() {
DeviceName.with(context).request((info, error) -> {
manufacturer = info.manufacturer; // "Samsung"
model = info.model; // "SM-G955W"
deviceName = info.getName(); // "Galaxy S8+"
});
}


private JSONArray persistData() {
JSONArray array = new JSONArray();
array.put(this.toJSON());

return array;
}

@Override
public JSONObject toJSON() {
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("deviceid",deviceId);
jsonObject.put("manufacturer",manufacturer);
jsonObject.put("model",model);
jsonObject.put("deciveName",deviceName);
jsonObject.put("rooted",Device.isRooted());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}

@Override
public void writeData() {
JSONHelper jsonHelper = new JSONHelper();
jsonHelper.jsonWriter(persistData(),file);
}
}
14 changes: 14 additions & 0 deletions src/main/java/de/tudarmstadt/informatik/hostage/event/Event.java
@@ -0,0 +1,14 @@
package de.tudarmstadt.informatik.hostage.event;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.UUID;

public interface Event {
String deviceId = UUID.randomUUID().toString();

JSONObject toJSON() throws JSONException;
void writeData();
}
Expand Up @@ -3,6 +3,9 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.json.JSONArray;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

Expand All @@ -16,6 +19,8 @@
import de.tudarmstadt.informatik.hostage.ui.model.LogFilter;

public class PublishHelper {
private static final String PERSIST_FILENAME = "publish.json";
File file = new File("/data/data/" + MainActivity.getContext().getPackageName() + "/" + PERSIST_FILENAME);
private DaoSession dbSession;
private DAOHelper daoHelper;
private int offset=0;
Expand Down Expand Up @@ -62,7 +67,7 @@ public void uploadRecordHpfeeds(){
* Persists the record in a JSON file.
*/
private void persistRecord(){
jsonHelper.persistData(getLastInsertedRecords());
jsonHelper.jsonWriter(this.persistData(getLastInsertedRecords()),file);
}

/**
Expand All @@ -75,7 +80,7 @@ private void persistRecord(){
*/
private void publisher() throws Hpfeeds.ReadTimeOutException, Hpfeeds.EOSException, Hpfeeds.InvalidStateException, Hpfeeds.LargeMessageException, IOException {
Publisher publisher = new Publisher();
String initialConfigurationUrl = jsonHelper.getFilePath();
String initialConfigurationUrl = jsonHelper.getFilePath(file);
publisher.setCommand(host,port,ident,secret,channel,initialConfigurationUrl);
publisher.publishFile();
}
Expand All @@ -87,4 +92,18 @@ private void publisher() throws Hpfeeds.ReadTimeOutException, Hpfeeds.EOSExcepti
private ArrayList<RecordAll> getLastInsertedRecords(){
return daoHelper.getAttackRecordDAO().getRecordsForFilter(filter,offset,limit,attackRecordOffset,attackRecordLimit);
}

/**
* Creates a JSON array.
* @param records of the attacks
* @return JSON array.
*/
public JSONArray persistData(ArrayList<RecordAll> records){
JSONArray arr = new JSONArray();
for(RecordAll record: records) {
arr.put(record.toJSON());
}
return arr;
}

}

0 comments on commit 6dc5e47

Please sign in to comment.