Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified logging for the remaining files in Bundle Client #109

Merged
merged 14 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import com.ddd.bundlesecurity.SecurityExceptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

Expand Down Expand Up @@ -80,7 +79,7 @@ private void inBackground(String... params) throws Exception {
streamObserver.onNext(metadata);

// upload file as chunk
Log.d(HelloworldActivity.TAG, "Started file transfer");
logger.log(INFO, "Started file transfer");
FileInputStream inputStream = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
inputStream = new FileInputStream(toSend.getBundle().getSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.text.method.ScrollingMovementMethod;;
import android.widget.Button;
import android.widget.TextView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import com.ddd.client.bundledeliveryagent.BundleDeliveryAgent;

import java.util.logging.Logger;

import static java.util.logging.Level.INFO;

public class BundleClientApplication {

private static final Logger logger = Logger.getLogger(BundleClientApplication.class.getName());

private static BundleDeliveryAgent bundleDeliveryAgent = new BundleDeliveryAgent("");

public static void main(String[] args) {
System.out.println("Starting Bundle Client!");
logger.log(INFO, "Starting Bundle Client!");
bundleDeliveryAgent.send();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -267,7 +266,7 @@ public List<String> getRegisteredAppIds() {
String line = "";
while ((line = bufferedReader.readLine()) != null) {
String appId = line.trim();
Log.d(HelloworldActivity.TAG, appId);
logger.log(FINE, appId);
registeredAppIds.add(appId);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import com.ddd.bundleclient.HelloworldActivity;
import com.ddd.datastore.filestore.FileStoreHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.ddd.bundlerouting.WindowUtils.WindowExceptions.BufferOverflow;
import com.ddd.bundlerouting.WindowUtils.WindowExceptions.InvalidLength;

import android.util.Log;
import com.ddd.bundlerouting.WindowUtils.CircularBuffer;
import com.ddd.bundlesecurity.BundleIDGenerator;
import com.ddd.client.bundlesecurity.ClientSecurity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import org.apache.commons.lang3.StringUtils;
import org.whispersystems.libsignal.InvalidKeyException;
import org.whispersystems.libsignal.NoSessionException;
Expand All @@ -31,10 +39,11 @@
import com.ddd.utils.JarUtils;

import android.content.res.Resources;
import android.util.Log;

public class BundleSecurity {

private static final Logger logger = Logger.getLogger(BundleSecurity.class.getName());

private ClientSecurity client = null;
private static String RootFolder;
private static String LARGEST_BUNDLE_ID_RECEIVED = "/Shared/DB/LARGEST_BUNDLE_ID_RECEIVED.txt";
Expand Down Expand Up @@ -63,7 +72,7 @@ private String getLargestBundleIdReceived() {
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("[BS] Largest bundle id received so far: " + bundleId);
logger.log(INFO, "[BS] Largest bundle id received so far: " + bundleId);
return bundleId.trim();
}

Expand Down Expand Up @@ -123,7 +132,7 @@ public BundleSecurity(String rootFolder) {
client = ClientSecurity.initializeInstance(1, bundleSecurityPath, serverKeyPath);
clientBundleGenerator = ClientBundleGenerator.initializeInstance(client, rootFolder);
clientWindow = ClientWindow.initializeInstance(5, client.getClientID(), rootFolder);
Log.d(HelloworldActivity.TAG, "Kuch Bhi");
logger.log(FINE, "Kuch Bhi");
} catch (InvalidKeyException | SecurityExceptions.IDGenerationException | SecurityExceptions.EncodingException |
WindowExceptions.InvalidLength | WindowExceptions.BufferOverflow e) {
e.printStackTrace();
Expand All @@ -133,12 +142,12 @@ public BundleSecurity(String rootFolder) {
}

public void registerLargestBundleIdReceived(String bundleId) {
Log.d(HelloworldActivity.TAG, "[BS] Inside registerLargestBundleIdReceived function " + bundleId);
logger.log(INFO, "[BS] Inside registerLargestBundleIdReceived function " + bundleId);
try {
clientWindow.processBundle(bundleId, client);
Log.d(HelloworldActivity.TAG, "Receive window is:");
logger.log(INFO, "Receive window is:");
for (String windowBundleId : clientWindow.getWindow(client)) {
Log.d(HelloworldActivity.TAG, windowBundleId);
logger.log(INFO, windowBundleId);
}
} catch (WindowExceptions.BufferOverflow e) {
e.printStackTrace();
Expand All @@ -152,7 +161,7 @@ public ClientBundleGenerator getClientBundleGenerator() {
}

public void decryptBundleContents(UncompressedPayload bundle) {
System.out.println("[BS] Decrypting contents of the bundle with id: " + bundle.getBundleId());
logger.log(INFO, "[BS] Decrypting contents of the bundle with id: " + bundle.getBundleId());
}

public String generateNewBundleId() throws SecurityExceptions.IDGenerationException,
Expand All @@ -162,8 +171,8 @@ public String generateNewBundleId() throws SecurityExceptions.IDGenerationExcept

public UncompressedBundle encryptPayload(Payload payload, String bundleGenDirPath) {
String bundleId = payload.getBundleId();
Log.d(HelloworldActivity.TAG, "encrypting payload in bundleId: " + bundleId);
System.out.println("[BS] Payload source:" + payload.getSource() + " bundle id " + bundleId);
logger.log(INFO, "Encrypting payload in bundleId: " + bundleId);
logger.log(INFO, "[BS] Payload source:" + payload.getSource() + " bundle id " + bundleId);
String[] paths;
if (!this.isEncryptionEnabled) {
return new UncompressedBundle(bundleId, payload.getSource(), null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
import java.util.Arrays;
import java.util.List;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import org.whispersystems.libsignal.DuplicateMessageException;
import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.IdentityKeyPair;
Expand Down Expand Up @@ -45,6 +53,8 @@

public class ClientSecurity {

private static final Logger logger = Logger.getLogger(ClientSecurity.class.getName());

private static ClientSecurity singleClientInstance = null;

// Used to store in local signal protocol store so can be same across devices
Expand Down Expand Up @@ -73,9 +83,9 @@ private ClientSecurity(int deviceID, String clientRootPath, String serverKeyPath

try {
loadKeysfromFiles(clientKeyPath);
System.out.println("[Sec]: Using Existing Keys");
logger.log(FINE, "[Sec]: Using Existing Keys");
} catch (IOException | EncodingException e) {
System.out.println("[Sec]: Error Loading Keys from files, generating new keys instead\n" + e);
logger.log(WARNING, "[Sec]: Error Loading Keys from files, generating new keys instead\n" + e);
// Create Client's Key pairs
ECKeyPair identityKeyPair = Curve.generateKeyPair();
ourIdentityKeyPair = new IdentityKeyPair(new IdentityKey(identityKeyPair.getPublicKey()),
Expand Down Expand Up @@ -189,8 +199,8 @@ private void createCipher() throws InvalidKeyException {
byte[] sessionStoreBytes = SecurityUtils.readFromFile(sessionStorePath);
clientSessionRecord = new SessionRecord(sessionStoreBytes);
} catch (IOException e) {
System.out.println(
"Error Reading Session record from " + sessionStorePath + "\nCreating New Session Record!");
logger.log(WARNING,
"Error Reading Session record from " + sessionStorePath + "\nCreating New Session Record!");
clientSessionRecord = new SessionRecord();
initializeRatchet(clientSessionRecord.getSessionState());
}
Expand Down Expand Up @@ -258,7 +268,7 @@ public static synchronized ClientSecurity initializeInstance(int deviceID, Strin
if (singleClientInstance == null) {
singleClientInstance = new ClientSecurity(deviceID, clientRootPath, serverKeyPath);
} else {
System.out.println("[Sec]: Client Security Instance is already initialized!");
logger.log(FINE, "[Sec]: Client Security Instance is already initialized!");
}

return singleClientInstance;
Expand Down Expand Up @@ -351,18 +361,18 @@ public void decrypt(String bundlePath, String decryptedPath) throws IOException,
try (FileOutputStream stream = new FileOutputStream(decryptedFile, true)) {
stream.write(serverDecryptedMessage);
}
System.out.printf("Decrypted Size = %d\n", serverDecryptedMessage.length);
logger.log(FINER, "Decrypted Size = %d\n", serverDecryptedMessage.length);

if (SecurityUtils.verifySignature(serverDecryptedMessage, theirIdentityKey.getPublicKey(), signatureFile)) {
System.out.println("Verified Signature!");
logger.log(FINE, "Verified Signature!");
} else {
// Failed to verify sign, delete bundle and return
System.out.println("Invalid Signature [" + payloadName + "], Aborting bundle " + bundleID);
logger.log(WARNING, "Invalid Signature [" + payloadName + "], Aborting bundle " + bundleID);

try {
new File(decryptedFile).delete();
} catch (Exception e) {
System.out.printf("Error: Failed to delete decrypted file [%s]", decryptedFile);
logger.log(WARNING, "Error: Failed to delete decrypted file [%s]", decryptedFile);
System.out.println(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
import java.util.List;
import java.util.Optional;

import java.util.logging.Logger;

import static java.util.logging.Level.FINER;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Level.SEVERE;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import com.ddd.bundleclient.HelloworldActivity;
Expand All @@ -32,10 +40,11 @@
import com.ddd.utils.AckRecordUtils;
import com.ddd.utils.BundleUtils;
import com.ddd.utils.Constants;
import android.util.Log;

public class BundleTransmission {

private static final Logger logger = Logger.getLogger(BundleTransmission.class.getName());

private BundleSecurity bundleSecurity;

private ApplicationDataManager applicationDataManager;
Expand Down Expand Up @@ -133,7 +142,7 @@ private void processReceivedBundle(String transportId, Bundle bundle) {
File.separator + RECEIVED_PROCESSING);
Payload payload = this.bundleSecurity.decryptPayload(uncompressedBundle);
try {
Log.d(HelloworldActivity.TAG, "Updating client routing metadata for transport " + transportId);
logger.log(INFO, "Updating client routing metadata for transport " + transportId);
clientRouting.updateMetaData(transportId);
} catch (RoutingExceptions.ClientMetaDataFileException e) {
e.printStackTrace();
Expand Down Expand Up @@ -167,18 +176,18 @@ private void processReceivedBundle(String transportId, Bundle bundle) {

public void processReceivedBundles(String transportId, String bundlesLocation) {
File bundleStorageDirectory = new File(bundlesLocation);
Log.d(HelloworldActivity.TAG, "inside receives" + bundlesLocation);
logger.log(FINE, "inside receives" + bundlesLocation);
if (bundleStorageDirectory.listFiles() == null || bundleStorageDirectory.listFiles().length == 0) {
Log.d(HelloworldActivity.TAG, "No Bundle received");
logger.log(INFO, "No Bundle received");
return;
}
for (final File bundleFile : bundleStorageDirectory.listFiles()) {
Bundle bundle = new Bundle(bundleFile);
Log.d(HelloworldActivity.TAG, "Processing: " + bundle.getSource().getName());
logger.log(INFO, "Processing: " + bundle.getSource().getName());
this.processReceivedBundle(transportId, bundle);
Log.d(HelloworldActivity.TAG, "Deleting Directory");
logger.log(INFO, "Deleting Directory");
FileUtils.deleteQuietly(bundle.getSource());
Log.d(HelloworldActivity.TAG, "Deleted Directory");
logger.log(INFO, "Deleted Directory");
}
String largestBundleId = getLargestBundleIdReceived();
this.bundleSecurity.registerLargestBundleIdReceived(largestBundleId);
Expand Down Expand Up @@ -218,8 +227,7 @@ private BundleDTO generateNewBundle(UncompressedPayload.Builder builder, File ta
BundleUtils.writeUncompressedPayload(toSendBundlePayload, new File(
this.ROOT_DIR + BUNDLE_GENERATION_DIRECTORY + File.separator + UNCOMPRESSED_PAYLOAD), bundleId);
try {
Log.d(HelloworldActivity.TAG,
"Placing routing.metadata in " + toSendBundlePayload.getSource().getAbsolutePath());
logger.log(INFO, "Placing routing.metadata in " + toSendBundlePayload.getSource().getAbsolutePath());
clientRouting.bundleMetaData(toSendBundlePayload.getSource().getAbsolutePath());
} catch (RoutingExceptions.ClientMetaDataFileException e) {
System.out.println("[BR]: Failed to add Routing metadata to bundle!");
Expand All @@ -239,7 +247,7 @@ private BundleDTO generateNewBundle(UncompressedPayload.Builder builder, File ta
}

public BundleDTO generateBundleForTransmission() {
Log.d(HelloworldActivity.TAG, "Started process of generating bundle");
logger.log(FINE, "Started process of generating bundle");
File toSendDir = new File(this.ROOT_DIR + BUNDLE_GENERATION_DIRECTORY + File.separator + TO_SEND_DIRECTORY);

BundleDTO toSend = null;
Expand Down Expand Up @@ -269,7 +277,7 @@ public BundleDTO generateBundleForTransmission() {
toSend = this.generateNewBundle(unprocessedPayloadBuilder, toSendDir, bundleId);
}

Log.d(HelloworldActivity.TAG, "sending bundle with id: " + toSend.getBundleId());
logger.log(INFO, "sending bundle with id: " + toSend.getBundleId());
return toSend;
}

Expand Down
Loading
Loading