From 61e2c3444a68eeda7a56e7eb992c7fa1beac16d7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Wed, 11 Jan 2017 17:37:35 +0800 Subject: [PATCH] Remove token, use ETag to prevent multiple queries --- .../com/topjohnwu/magisk/module/Repo.java | 4 +- .../com/topjohnwu/magisk/utils/Async.java | 2 +- .../topjohnwu/magisk/utils/ModuleHelper.java | 31 +++++++--- .../com/topjohnwu/magisk/utils/Utils.java | 13 ---- .../{WebRequest.java => WebService.java} | 59 +++++++++++-------- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 60 insertions(+), 51 deletions(-) rename app/src/main/java/com/topjohnwu/magisk/utils/{WebRequest.java => WebService.java} (62%) diff --git a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java index 2a64a17aa..4a5ea52d6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/Repo.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/Repo.java @@ -4,7 +4,7 @@ import android.content.Context; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Logger; -import com.topjohnwu.magisk.utils.WebRequest; +import com.topjohnwu.magisk.utils.WebService; import java.util.Date; @@ -22,7 +22,7 @@ public class Repo extends BaseModule { public void update() throws CacheModException { Logger.dev("Repo: Re-fetch prop"); - String props = WebRequest.makeWebServiceCall(mManifestUrl, WebRequest.GET, true); + String props = WebService.request(mManifestUrl, WebService.GET, true); String lines[] = props.split("\\n"); parseProps(lines); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 5d7041e15..11ee12971 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -56,7 +56,7 @@ public class Async { @Override protected Void doInBackground(Void... voids) { - String jsonStr = WebRequest.makeWebServiceCall(UPDATE_JSON, WebRequest.GET); + String jsonStr = WebService.request(UPDATE_JSON, WebService.GET); try { JSONObject json = new JSONObject(jsonStr); JSONObject magisk = json.getJSONObject("magisk"); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/ModuleHelper.java b/app/src/main/java/com/topjohnwu/magisk/utils/ModuleHelper.java index 06f81a5e4..719c9d5a3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/ModuleHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ModuleHelper.java @@ -31,6 +31,7 @@ public class ModuleHelper { private static final String FILE_KEY = "RepoMap"; private static final String REPO_KEY = "repomap"; private static final String VERSION_KEY = "version"; + private static final String ETAG_KEY = "ETag"; private static final int DATABASE_VER = 1; private static ValueSortedMap repoMap = new ValueSortedMap<>(); @@ -57,10 +58,11 @@ public class ModuleHelper { public static void createRepoMap(Context context) { Logger.dev("ModuleHelper: Loading repos"); + SharedPreferences prefs = context.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE); + repoMap.clear(); Gson gson = new Gson(); - SharedPreferences prefs = context.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE); String jsonString; int cachedVersion = prefs.getInt(VERSION_KEY, 0); @@ -74,20 +76,31 @@ public class ModuleHelper { ValueSortedMap cached = null; if (jsonString != null) { - cached = gson.fromJson(jsonString, new TypeToken< ValueSortedMap >(){}.getType()); + cached = gson.fromJson(jsonString, new TypeToken>(){}.getType()); } if (cached == null) { cached = new ValueSortedMap<>(); } - // Making a request to url and getting response - jsonString = WebRequest.makeWebServiceCall(context.getString(R.string.url_main), WebRequest.GET); + // Get cached ETag to add in the request header + String etag = prefs.getString(ETAG_KEY, ""); + HashMap header = new HashMap<>(); + header.put("If-None-Match", etag); - if (jsonString != null && !jsonString.isEmpty()) { - // Have internet access + // Making a request to main URL for repo info + jsonString = WebService.request( + context.getString(R.string.url_main), WebService.GET, null, header, false); + + if (!jsonString.isEmpty()) { try { JSONArray jsonArray = new JSONArray(jsonString); + // If it gets to this point, the response is valid, update ETag + etag = WebService.getLastResponseHeader().get(ETAG_KEY).get(0); + // Maybe bug in Android build tools, sometimes the ETag has crap in it... + etag = etag.substring(etag.indexOf('\"'), etag.lastIndexOf('\"') + 1); + + // Update repo info for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonobject = jsonArray.getJSONObject(i); String id = jsonobject.getString("description"); @@ -106,7 +119,7 @@ public class ModuleHelper { Logger.dev("ModuleHelper: Create new repo " + id); repo = new Repo(context, name, updatedDate); } else { - Logger.dev("ModuleHelper: Cached repo " + id); + Logger.dev("ModuleHelper: Update cached repo " + id); repo.update(updatedDate); } if (repo.getId() != null) { @@ -118,13 +131,15 @@ public class ModuleHelper { e.printStackTrace(); } } else { - // Use cached if no internet + // Use cached if no internet or no updates + Logger.dev("ModuleHelper: No updates, use cached"); repoMap.putAll(cached); } prefs.edit() .putInt(VERSION_KEY, DATABASE_VER) .putString(REPO_KEY, gson.toJson(repoMap)) + .putString(ETAG_KEY, etag) .apply(); Logger.dev("ModuleHelper: Repo load done"); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index eafe1df4a..dce0ad31c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -10,7 +10,6 @@ import android.net.Uri; import android.os.Environment; import android.support.v4.app.ActivityCompat; import android.text.TextUtils; -import android.util.Base64; import android.widget.Toast; import com.topjohnwu.magisk.R; @@ -19,20 +18,8 @@ import com.topjohnwu.magisk.receivers.DownloadReceiver; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.UnsupportedEncodingException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; import java.util.List; -import javax.crypto.BadPaddingException; -import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.DESKeySpec; - public class Utils { public static boolean isDownloading = false; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java similarity index 62% rename from app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java rename to app/src/main/java/com/topjohnwu/magisk/utils/WebService.java index 0836a5613..a33639d44 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebRequest.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebService.java @@ -8,21 +8,17 @@ import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; -import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.net.ssl.HttpsURLConnection; -public class WebRequest { +public class WebService { - static String response = null; public final static int GET = 1; public final static int POST = 2; - //Constructor with no parameter - public WebRequest() { - - } + private static Map> responseHeader; /** * Making web service call @@ -30,15 +26,12 @@ public class WebRequest { * @url - url to make request * @requestmethod - http request method */ - public static String makeWebServiceCall(String url, int requestmethod) { - return makeWebServiceCall(url, requestmethod, null, false); - - + public static String request(String url, int method) { + return request(url, method, null, null, false); } - public static String makeWebServiceCall(String url, int requestmethod, boolean addNewLines) { - return makeWebServiceCall(url, requestmethod, null, addNewLines); - + public static String request(String url, int method, boolean newline) { + return request(url, method, null, null, newline); } /** @@ -47,26 +40,35 @@ public class WebRequest { * @url - url to make request * @requestmethod - http request method * @params - http request params + * @header - http request header + * @newline - true to append a newline each line */ - public static String makeWebServiceCall(String urladdress, int requestmethod, - HashMap params, boolean addNewLines) { - Logger.dev("WebRequest: Service call " + urladdress); + public static String request(String urlAddress, int method, + Map params, Map header, + boolean newline) { + Logger.dev("WebService: Service call " + urlAddress); URL url; - String response = ""; + StringBuilder response = new StringBuilder(); try { - url = new URL(urladdress); + url = new URL(urlAddress); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(15000); conn.setConnectTimeout(15000); conn.setDoInput(true); - if (requestmethod == POST) { + if (method == POST) { conn.setRequestMethod("POST"); - } else if (requestmethod == GET) { + } else if (method == GET) { conn.setRequestMethod("GET"); } + if (header != null) { + for (Map.Entry entry : header.entrySet()) { + conn.setRequestProperty(entry.getKey(), entry.getValue()); + } + } + if (params != null) { OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( @@ -98,20 +100,25 @@ public class WebRequest { String line; BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = br.readLine()) != null) { - if (addNewLines) { - response += line + "\n"; + if (newline) { + response.append(line).append("\n"); } else { - response += line; + response.append(line); } } + responseHeader = conn.getHeaderFields(); } else { - response = ""; + responseHeader = null; } } catch (Exception e) { e.printStackTrace(); } - return response; + return response.toString(); + } + + public static Map> getLastResponseHeader() { + return responseHeader; } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 06dddaad1..4635b2199 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -111,7 +111,7 @@ Check release notes - https://api.github.com/orgs/Magisk-Modules-Repo/repos?access_token=8f3c379fbeb80754b45b02486482584893af142a + https://api.github.com/orgs/Magisk-Modules-Repo/repos https://raw.githubusercontent.com/Magisk-Modules-Repo/%1$s/master/%2$s https://github.com/Magisk-Modules-Repo/%1$s/archive/master.zip