[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake-mobile/master] Randomly generated Sid



commit 5c2800b4804f3bbb04a2d3e4cb9ad787c15f8a12
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Fri Jun 19 16:45:43 2020 +0530

    Randomly generated Sid
---
 .../torproject/snowflake/MyPersistentService.java  | 28 ++++++++-----
 .../java/org/torproject/snowflake/SIDHelper.java   | 47 ++++++++++++++++++++++
 .../snowflake/exceptions/EmptySIDException.java    | 13 ++++++
 3 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
index b888700..b3d9649 100644
--- a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
+++ b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
@@ -17,6 +17,7 @@ import androidx.annotation.Nullable;
 import org.json.JSONException;
 import org.torproject.snowflake.constants.BrokerConstants;
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
+import org.torproject.snowflake.exceptions.EmptySIDException;
 import org.torproject.snowflake.interfaces.PeerConnectionObserverCallback;
 import org.torproject.snowflake.pojo.AnsResponse;
 import org.torproject.snowflake.pojo.AnswerBody;
@@ -58,6 +59,7 @@ public class MyPersistentService extends Service {
     private CompositeDisposable compositeDisposable;
     private NotificationManager mNotificationManager;
     private boolean isConnectionAlive;
+    private SIDHelper sidHelper;
 
     @Nullable
     @Override
@@ -92,6 +94,7 @@ public class MyPersistentService extends Service {
 
         mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         isConnectionAlive = false;
+        sidHelper = SIDHelper.getInstance();
         compositeDisposable = new CompositeDisposable();
         sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE); //Assigning the shared preferences
         Notification notification = createPersistentNotification(false, null);
@@ -340,7 +343,7 @@ public class MyPersistentService extends Service {
             Log.d(TAG, "fetchOffer: Fetching offer from broker.");
             ///Retrofit call
             final GetOfferService getOfferService = RetroServiceGenerator.createService(GetOfferService.class);
-            Observable<SDPOfferResponse> offer = getOfferService.getOffer(GlobalApplication.getHeadersMap(), new OfferRequestBody("555")); //TODO:Randomly Generate SID.
+            Observable<SDPOfferResponse> offer = getOfferService.getOffer(GlobalApplication.getHeadersMap(), new OfferRequestBody(sidHelper.generateSid()));
             compositeDisposable.add(
                     offer.subscribeOn(Schedulers.io())
                             .observeOn(AndroidSchedulers.mainThread())
@@ -391,15 +394,20 @@ public class MyPersistentService extends Service {
      */
     public void sendAnswer(SessionDescription sessionDescription) {
         Log.d(TAG, "sendAnswer: Sending SDP Answer");
-        AnswerBodySDP bodySDP = new AnswerBodySDP();
-        bodySDP.setSdp(SDPSerializer.serializeAnswer(sessionDescription));
-        AnswerBody body = new AnswerBody("555", bodySDP.toString()); //TODO:Use randomly Generate SID from sendRequest
-        SendAnswerService service = RetroServiceGenerator.createService(SendAnswerService.class);
-        Observable<AnsResponse> response = service.sendAnswer(GlobalApplication.getHeadersMap(), body);
-        compositeDisposable.add(
-                response.subscribeOn(Schedulers.io())
-                        .observeOn(AndroidSchedulers.mainThread()).subscribe(this::answerResponseSuccess, this::answerResponseFailure)
-        );
+        try {
+            AnswerBodySDP bodySDP = new AnswerBodySDP();
+            bodySDP.setSdp(SDPSerializer.serializeAnswer(sessionDescription));
+            AnswerBody body = new AnswerBody(sidHelper.getSid(), bodySDP.toString());
+            SendAnswerService service = RetroServiceGenerator.createService(SendAnswerService.class);
+            Observable<AnsResponse> response = service.sendAnswer(GlobalApplication.getHeadersMap(), body);
+            compositeDisposable.add(
+                    response.subscribeOn(Schedulers.io())
+                            .observeOn(AndroidSchedulers.mainThread()).subscribe(this::answerResponseSuccess, this::answerResponseFailure)
+            );
+        } catch (EmptySIDException e) {
+            Log.e(TAG, "sendAnswer: getSid() is called before sid generation");
+            e.printStackTrace();
+        }
     }
 
     /**
diff --git a/app/src/main/java/org/torproject/snowflake/SIDHelper.java b/app/src/main/java/org/torproject/snowflake/SIDHelper.java
new file mode 100644
index 0000000..b2040d1
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/SIDHelper.java
@@ -0,0 +1,47 @@
+package org.torproject.snowflake;
+
+import org.torproject.snowflake.exceptions.EmptySIDException;
+
+import java.util.Random;
+
+/**
+ * A helper class to handle SID operations.
+ * There should only be one SID for whole connection cycle hence singleton.
+ */
+public class SIDHelper {
+    private static SIDHelper sidHelper;
+    private String sid;
+
+    private SIDHelper() {
+    }
+
+    public static SIDHelper getInstance() {
+        if (sidHelper == null) {
+            sidHelper = new SIDHelper();
+        }
+        return sidHelper;
+    }
+
+    /**
+     * generates a new sid.
+     *
+     * @return generated sid.
+     */
+    public String generateSid() {
+        sid = Integer.toString(new Random().nextInt(10000));
+        return sid;
+    }
+
+    /**
+     * Returns the generated sid.
+     *
+     * @return Existing sid that is generated in "generateSid" method.
+     * @throws EmptySIDException sid is not generated.
+     */
+    public String getSid() throws EmptySIDException {
+        if (sid == null) {
+            throw new EmptySIDException();
+        }
+        return sid;
+    }
+}
diff --git a/app/src/main/java/org/torproject/snowflake/exceptions/EmptySIDException.java b/app/src/main/java/org/torproject/snowflake/exceptions/EmptySIDException.java
new file mode 100644
index 0000000..f69a366
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/exceptions/EmptySIDException.java
@@ -0,0 +1,13 @@
+package org.torproject.snowflake.exceptions;
+
+import androidx.annotation.NonNull;
+
+public class EmptySIDException extends Exception {
+    private final String ERROR = "SID is not initialized or generated";
+
+    @NonNull
+    @Override
+    public String toString() {
+        return ERROR;
+    }
+}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits