Remove token, use ETag to prevent multiple queries
This commit is contained in:
parent
5363b0f810
commit
61e2c3444a
@ -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);
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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<String, Repo> 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<String, Repo> cached = null;
|
||||
|
||||
if (jsonString != null) {
|
||||
cached = gson.fromJson(jsonString, new TypeToken< ValueSortedMap<String, Repo> >(){}.getType());
|
||||
cached = gson.fromJson(jsonString, new TypeToken<ValueSortedMap<String, Repo>>(){}.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<String, String> 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");
|
||||
|
@ -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;
|
||||
|
@ -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<String, List<String>> 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<String, String> params, boolean addNewLines) {
|
||||
Logger.dev("WebRequest: Service call " + urladdress);
|
||||
public static String request(String urlAddress, int method,
|
||||
Map<String, String> params, Map<String, String> 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<String, String> 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<String, List<String>> getLastResponseHeader() {
|
||||
return responseHeader;
|
||||
}
|
||||
|
||||
}
|
@ -111,7 +111,7 @@
|
||||
<string name="check_release_notes">Check release notes</string>
|
||||
|
||||
<!--URL Templates-->
|
||||
<string name="url_main" translatable="false">https://api.github.com/orgs/Magisk-Modules-Repo/repos?access_token=8f3c379fbeb80754b45b02486482584893af142a</string>
|
||||
<string name="url_main" translatable="false">https://api.github.com/orgs/Magisk-Modules-Repo/repos</string>
|
||||
<string name="file_url" translatable="false">https://raw.githubusercontent.com/Magisk-Modules-Repo/%1$s/master/%2$s</string>
|
||||
<string name="zip_url" translatable="false">https://github.com/Magisk-Modules-Repo/%1$s/archive/master.zip</string>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user