]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Handbook improvements.
authorNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sat, 2 Mar 2019 02:25:24 +0000 (02:25 +0000)
committerNathaniel Wesley Filardo <nwfilardo@gmail.com>
Sat, 2 Mar 2019 02:25:24 +0000 (02:25 +0000)
Add policy permitting cleartext connection to cmukgb.org and its
subdomains.

Rate-limit resubscriptions to handbook stamp topic, to prevent
busy-looping around errors such as seen prior to the existence of the
above.

mobile/src/main/AndroidManifest.xml
mobile/src/main/java/com/acmetensortoys/ctfwstimer/HandbookDownloader.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/MainService.java
mobile/src/main/res/xml/network_security_config.xml [new file with mode: 0644]

index 0a919811918cfc999607c217699a0ded8d51519f..b6bf9f54222a7348cbeed72d285df225d14014de 100644 (file)
@@ -15,6 +15,7 @@
     <application
         android:allowBackup="true"
         android:fullBackupContent="@xml/backup_rules"
+        android:networkSecurityConfig="@xml/network_security_config"
         android:icon="@mipmap/ic_shield_1"
         android:label="@string/app_name"
         android:supportsRtl="true"
index bb2260f581c50c5b935f2b0b7c62837f5b50fc87..dc9001d815a0e244e32e10aaa1c951ca33aca778 100644 (file)
@@ -1,6 +1,7 @@
 package com.acmetensortoys.ctfwstimer;
 
 import android.content.Context;
+import android.os.Handler;
 import android.util.Log;
 
 import com.acmetensortoys.ctfwstimer.CheckedAsyncDownloader;
@@ -25,9 +26,12 @@ public class HandbookDownloader implements IMqttMessageListener {
     private final Context mCtx;
     private final Runnable mDLFiniCB;
     private IMqttAsyncClient mMqc;
+    private Handler mHdl;
+    private Runnable nextSubRunnable;
 
-    public HandbookDownloader(Context mCtx, Runnable dlfinicb) {
-        this.mCtx = mCtx;
+    public HandbookDownloader(Context ctx, Handler hdl, Runnable dlfinicb) {
+        this.mCtx = ctx;
+        this.mHdl = hdl;
         this.mDLFiniCB = dlfinicb;
     }
 
@@ -69,17 +73,37 @@ public class HandbookDownloader implements IMqttMessageListener {
         }
 
         private void fini() {
-            if (mqc == mMqc) {
-                try {
-                    subscribe(mqc);
-                } catch (MqttException mqe) {
-                    /*
-                     * Well this stinks.  Presumably it is because something has gone
-                     * wrong somewhere else and we will notice shortly.
-                     */
-                    ;
+            /*
+             * Try to resubscribe in a while.
+             * This is a very crude kind of rate limiting.
+             */
+            synchronized (this) {
+                if (nextSubRunnable != null) {
+                    mHdl.removeCallbacks(nextSubRunnable);
                 }
+                nextSubRunnable = new Runnable() {
+                    @Override
+                    public void run() {
+                        Log.d(TAG, "Resubscribing to handbook topic");
+                        synchronized (HandbookDownloader.this) {
+                            if (mqc == mMqc) {
+                                try {
+                                    subscribe(mqc);
+                                    nextSubRunnable = null;
+                                } catch (MqttException mqe) {
+                                    /*
+                                     * Well this stinks.  Presumably it is because
+                                     * something has gone wrong somewhere else and
+                                     * we will notice shortly.
+                                     */
+                                }
+                            }
+                        }
+                    }
+                };
+                mHdl.postDelayed(nextSubRunnable, 60000);
             }
+
             HandbookDownloader.this.downloader = null;
             HandbookDownloader.this.download = null;
         }
@@ -173,6 +197,9 @@ public class HandbookDownloader implements IMqttMessageListener {
 
     public void subscribeOn(IMqttAsyncClient mqc) throws MqttException {
         synchronized (this) {
+            if (nextSubRunnable != null) {
+                mHdl.removeCallbacks(nextSubRunnable);
+            }
             mMqc = mqc;
             subscribe(mqc);
         }
@@ -184,6 +211,9 @@ public class HandbookDownloader implements IMqttMessageListener {
 
     public void unsubscribeOn(IMqttAsyncClient mqc) throws MqttException {
         synchronized (this) {
+            if (nextSubRunnable != null) {
+                mHdl.removeCallbacks(nextSubRunnable);
+            }
             unsubscribe(mqc);
             mMqc = null;
         }
index 5ad5b465121f23a92f9a857e4354badc1637fad6..1d16d9527860696d01cc139b2a2e61b31fdbb21b 100644 (file)
@@ -6,6 +6,7 @@ import android.content.SharedPreferences;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.Looper;
 import android.preference.PreferenceManager;
 import android.support.annotation.Nullable;
 import android.util.Log;
@@ -60,6 +61,7 @@ public class MainService extends Service {
 
     // Handbook fetch logic; this is a singleton for the service, even as connections come and go.
     private HandbookDownloader mHandDL = new HandbookDownloader(this,
+            new Handler(Looper.getMainLooper()),
             new Runnable() {
                 @Override
                 public void run() {
diff --git a/mobile/src/main/res/xml/network_security_config.xml b/mobile/src/main/res/xml/network_security_config.xml
new file mode 100644 (file)
index 0000000..7f9d952
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<network-security-config>
+    <domain-config cleartextHttpTrafficPermitted="true">
+        <domain includeSubdomains="true">cmukgb.org</domain>
+    </domain-config>
+</network-security-config>
\ No newline at end of file