|
| 1 | +package com.fox2code.mmm.androidacy; |
| 2 | + |
| 3 | +import android.content.SharedPreferences; |
| 4 | +import android.util.Log; |
| 5 | +import android.webkit.CookieManager; |
| 6 | + |
| 7 | +import com.fox2code.mmm.R; |
| 8 | +import com.fox2code.mmm.manager.ModuleInfo; |
| 9 | +import com.fox2code.mmm.repo.RepoData; |
| 10 | +import com.fox2code.mmm.repo.RepoModule; |
| 11 | +import com.fox2code.mmm.utils.Http; |
| 12 | +import com.fox2code.mmm.utils.PropUtils; |
| 13 | + |
| 14 | +import org.json.JSONArray; |
| 15 | +import org.json.JSONException; |
| 16 | +import org.json.JSONObject; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class AndroidacyRepoData extends RepoData { |
| 25 | + private static final String TAG = "AndroidacyRepoData"; |
| 26 | + private long androidacyBlockade = 0; |
| 27 | + |
| 28 | + public AndroidacyRepoData(String url, File cacheRoot, |
| 29 | + SharedPreferences cachedPreferences) { |
| 30 | + super(url, cacheRoot, cachedPreferences); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected boolean prepare() { |
| 35 | + // Implementation details discussed on telegram |
| 36 | + long time = System.currentTimeMillis(); |
| 37 | + if (this.androidacyBlockade > time) return false; |
| 38 | + this.androidacyBlockade = time + 5_000L; |
| 39 | + String cookies = CookieManager.getInstance().getCookie("https://.androidacy.com/"); |
| 40 | + int start = cookies.indexOf("USER="); |
| 41 | + String token = null; |
| 42 | + if (start != -1) { |
| 43 | + int end = cookies.indexOf(";", start); |
| 44 | + if (end != -1) |
| 45 | + token = cookies.substring(start, end); |
| 46 | + } |
| 47 | + if (token != null) { |
| 48 | + try { |
| 49 | + Http.doHttpGet("https://api.androidacy.com/auth/me",true); |
| 50 | + } catch (Exception e) { |
| 51 | + if ("Received error code: 419".equals(e.getMessage()) || |
| 52 | + "Received error code: 429".equals(e.getMessage())) { |
| 53 | + Log.e(TAG, "We are being rate limited!", e); |
| 54 | + this.androidacyBlockade = time + 3_600_000L; |
| 55 | + return false; |
| 56 | + } |
| 57 | + Log.w(TAG, "Invalid token, resetting..."); |
| 58 | + CookieManager.getInstance().setCookie("https://.androidacy.com/", |
| 59 | + "USER=; expires=Thu, 01 Jan 1970 00:00:00 GMT;" + |
| 60 | + " path=/; secure; domain=.androidacy.com"); |
| 61 | + token = null; |
| 62 | + } |
| 63 | + } |
| 64 | + if (token == null) { |
| 65 | + try { |
| 66 | + Log.i(TAG, "Refreshing token..."); |
| 67 | + token = new String(Http.doHttpPost( |
| 68 | + "https://api.androidacy.com/auth/register", |
| 69 | + "",true), StandardCharsets.UTF_8); |
| 70 | + CookieManager.getInstance().setCookie(".androidacy.com", |
| 71 | + "USER="+ token + "; expires=Fri, 31 Dec 9999 23:59:59 GMT;" + |
| 72 | + " path=/; secure; domain=.androidacy.com"); |
| 73 | + } catch (Exception e) { |
| 74 | + if ("Received error code: 419".equals(e.getMessage()) || |
| 75 | + "Received error code: 429".equals(e.getMessage())) { |
| 76 | + Log.e(TAG, "We are being rate limited!", e); |
| 77 | + this.androidacyBlockade = time + 3_600_000L; |
| 78 | + } |
| 79 | + Log.e(TAG, "Failed to get a new token", e); |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException { |
| 88 | + if (!jsonObject.getString("status").equals("success")) |
| 89 | + throw new JSONException("Response is not a success!"); |
| 90 | + String name = jsonObject.optString( |
| 91 | + "name", "Androidacy Modules Repo"); |
| 92 | + String nameForModules = name.endsWith(" (Official)") ? |
| 93 | + name.substring(0, name.length() - 11) : name; |
| 94 | + JSONArray jsonArray = jsonObject.getJSONArray("data"); |
| 95 | + for (RepoModule repoModule : this.moduleHashMap.values()) { |
| 96 | + repoModule.processed = false; |
| 97 | + } |
| 98 | + ArrayList<RepoModule> newModules = new ArrayList<>(); |
| 99 | + int len = jsonArray.length(); |
| 100 | + long lastLastUpdate = 0; |
| 101 | + for (int i = 0; i < len; i++) { |
| 102 | + jsonObject = jsonArray.getJSONObject(i); |
| 103 | + String moduleId = jsonObject.getString("codename"); |
| 104 | + long lastUpdate = jsonObject.getLong("updated_at") * 1000; |
| 105 | + lastLastUpdate = Math.max(lastLastUpdate, lastUpdate); |
| 106 | + RepoModule repoModule = this.moduleHashMap.get(moduleId); |
| 107 | + if (repoModule == null) { |
| 108 | + repoModule = new RepoModule(moduleId); |
| 109 | + repoModule.moduleInfo.flags = 0; |
| 110 | + this.moduleHashMap.put(moduleId, repoModule); |
| 111 | + newModules.add(repoModule); |
| 112 | + } else { |
| 113 | + if (repoModule.lastUpdated < lastUpdate) { |
| 114 | + newModules.add(repoModule); |
| 115 | + } |
| 116 | + } |
| 117 | + repoModule.processed = true; |
| 118 | + repoModule.lastUpdated = lastUpdate; |
| 119 | + repoModule.repoName = nameForModules; |
| 120 | + repoModule.zipUrl = filterURL( |
| 121 | + jsonObject.optString("zipUrl", "")); |
| 122 | + repoModule.notesUrl = filterURL( |
| 123 | + jsonObject.optString("notesUrl", "")); |
| 124 | + if (repoModule.zipUrl == null) |
| 125 | + repoModule.zipUrl = jsonObject.getString("url"); |
| 126 | + if (repoModule.notesUrl == null) { |
| 127 | + repoModule.notesUrl = // Fallback url in case the API doesn't return one |
| 128 | + "https://api.androidacy.com/magisk/readme/?module=" + moduleId; |
| 129 | + } |
| 130 | + repoModule.qualityText = R.string.module_downloads; |
| 131 | + repoModule.qualityValue = jsonObject.optInt("downloads", 0); |
| 132 | + String checksum = jsonObject.optString("checksum", ""); |
| 133 | + repoModule.checksum = checksum.isEmpty() ? null : checksum; |
| 134 | + ModuleInfo moduleInfo = repoModule.moduleInfo; |
| 135 | + moduleInfo.name = jsonObject.getString("name"); |
| 136 | + moduleInfo.versionCode = jsonObject.getLong("versionCode"); |
| 137 | + moduleInfo.version = jsonObject.optString( |
| 138 | + "version", "v" + moduleInfo.versionCode); |
| 139 | + moduleInfo.author = jsonObject.optString("author", "Unknown"); |
| 140 | + moduleInfo.minApi = jsonObject.getInt("minApi"); |
| 141 | + moduleInfo.maxApi = jsonObject.getInt("maxApi"); |
| 142 | + String minMagisk = jsonObject.getString("minMagisk"); |
| 143 | + try { |
| 144 | + int c = minMagisk.indexOf('.'); |
| 145 | + if (c == -1) { |
| 146 | + moduleInfo.minMagisk = Integer.parseInt(minMagisk); |
| 147 | + } else { |
| 148 | + moduleInfo.minMagisk = // Allow 24.1 to mean 24100 |
| 149 | + (Integer.parseInt(minMagisk.substring(0, c)) * 1000) + |
| 150 | + (Integer.parseInt(minMagisk.substring(c + 1)) * 100); |
| 151 | + } |
| 152 | + } catch (Exception e) { |
| 153 | + moduleInfo.minMagisk = 0; |
| 154 | + } |
| 155 | + moduleInfo.support = filterURL(jsonObject.optString("support")); |
| 156 | + moduleInfo.donate = filterURL(jsonObject.optString("donate")); |
| 157 | + String config = jsonObject.optString("config", ""); |
| 158 | + moduleInfo.config = config.isEmpty() ? null : config; |
| 159 | + PropUtils.applyFallbacks(moduleInfo); // Apply fallbacks |
| 160 | + Log.d(TAG, "Module " + moduleInfo.name + " " + moduleInfo.id + " " + |
| 161 | + moduleInfo.version + " " + moduleInfo.versionCode); |
| 162 | + } |
| 163 | + Iterator<RepoModule> moduleInfoIterator = this.moduleHashMap.values().iterator(); |
| 164 | + while (moduleInfoIterator.hasNext()) { |
| 165 | + RepoModule repoModule = moduleInfoIterator.next(); |
| 166 | + if (!repoModule.processed) { |
| 167 | + moduleInfoIterator.remove(); |
| 168 | + } |
| 169 | + } |
| 170 | + this.lastUpdate = lastLastUpdate; |
| 171 | + this.name = name; |
| 172 | + return newModules; |
| 173 | + } |
| 174 | + |
| 175 | + private static String filterURL(String url) { |
| 176 | + if (url == null || url.isEmpty() || PropUtils.isInvalidURL(url)) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + return url; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void storeMetadata(RepoModule repoModule, byte[] data) {} |
| 184 | + |
| 185 | + @Override |
| 186 | + public boolean tryLoadMetadata(RepoModule repoModule) { |
| 187 | + return true; |
| 188 | + } |
| 189 | +} |
0 commit comments