Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 08e0edb

Browse files
committed
Give up on jsdelivr (Fix #46)
1 parent b97eaf1 commit 08e0edb

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

app/src/main/java/com/fox2code/mmm/repo/RepoData.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,6 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
138138
moduleNotesUrl = Http.updateLink(moduleNotesUrl);
139139
modulePropsUrl = Http.updateLink(modulePropsUrl);
140140
moduleZipUrl = Http.updateLink(moduleZipUrl);
141-
} else {
142-
moduleNotesUrl = Http.fixUpLink(moduleNotesUrl);
143-
modulePropsUrl = Http.fixUpLink(modulePropsUrl);
144-
moduleZipUrl = Http.fixUpLink(moduleZipUrl);
145141
}
146142
RepoModule repoModule = this.moduleHashMap.get(moduleId);
147143
if (repoModule == null) {

app/src/main/java/com/fox2code/mmm/repo/RepoManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ private RepoManager(MainApplication mainApplication) {
6868
this.repoData = new LinkedHashMap<>();
6969
this.modules = new HashMap<>();
7070
// We do not have repo list config yet.
71-
this.addRepoData(MAGISK_REPO_JSDELIVR);
72-
this.addRepoData(MAGISK_ALT_REPO_JSDELIVR);
71+
this.addRepoData(MAGISK_REPO);
72+
this.addRepoData(MAGISK_ALT_REPO);
7373
this.addAndroidacyRepoData();
7474
// Populate default cache
7575
for (RepoData repoData:this.repoData.values()) {
@@ -93,12 +93,12 @@ private RepoManager(MainApplication mainApplication) {
9393
public RepoData get(String url) {
9494
if (url == null) return null;
9595
switch (url) {
96+
case MAGISK_REPO_JSDELIVR:
9697
case MAGISK_REPO_MANAGER:
97-
case MAGISK_REPO:
98-
url = MAGISK_REPO_JSDELIVR;
98+
url = MAGISK_REPO;
9999
break;
100-
case MAGISK_ALT_REPO:
101-
url = MAGISK_ALT_REPO_JSDELIVR;
100+
case MAGISK_ALT_REPO_JSDELIVR:
101+
url = MAGISK_ALT_REPO;
102102
}
103103
return this.repoData.get(url);
104104
}

app/src/main/java/com/fox2code/mmm/utils/Http.java

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,16 @@ public class Http {
113113
+ " FoxMmm/" + BuildConfig.VERSION_CODE;
114114
httpclientBuilder.addInterceptor(chain -> {
115115
Request.Builder request = chain.request().newBuilder();
116-
if (chain.request().url().host().endsWith(".androidacy.com")) {
116+
request.header("Upgrade-Insecure-Requests", "1");
117+
String host = chain.request().url().host();
118+
if (host.endsWith(".androidacy.com")) {
117119
request.header("User-Agent", androidacyUA);
118-
} else if (InstallerInitializer.peekMagiskPath() != null) {
119-
request.header("User-Agent", // Declare Magisk version to the server
120-
"Magisk/" + InstallerInitializer.peekMagiskVersion());
120+
} else if (!(host.equals("github.com") || host.endsWith(".github.com") ||
121+
host.endsWith(".jsdelivr.net") || host.endsWith(".githubusercontent.com"))) {
122+
if (InstallerInitializer.peekMagiskPath() != null) {
123+
request.header("User-Agent", // Declare Magisk version to the server
124+
"Magisk/" + InstallerInitializer.peekMagiskVersion());
125+
}
121126
}
122127
if (chain.request().header("Accept-Language") == null) {
123128
request.header("Accept-Language", // Send system language to the server
@@ -148,13 +153,9 @@ public static OkHttpClient getHttpClientWithCache() {
148153
return httpClientWithCache;
149154
}
150155

151-
private static Request.Builder makeRequestBuilder() {
152-
return new Request.Builder().header("Upgrade-Insecure-Requests", "1");
153-
}
154-
155156
public static byte[] doHttpGet(String url,boolean allowCache) throws IOException {
156-
Response response = (allowCache ? httpClientWithCache : httpClient).newCall(
157-
makeRequestBuilder().url(url).get().build()
157+
Response response = (allowCache ? getHttpClientWithCache() : getHttpClient()).newCall(
158+
new Request.Builder().url(url).get().build()
158159
).execute();
159160
// 200/204 == success, 304 == cache valid
160161
if (response.code() != 200 && response.code() != 204 &&
@@ -172,8 +173,8 @@ public static byte[] doHttpGet(String url,boolean allowCache) throws IOException
172173
}
173174

174175
public static byte[] doHttpPost(String url,String data,boolean allowCache) throws IOException {
175-
Response response = (allowCache ? httpClientWithCache : httpClient).newCall(
176-
makeRequestBuilder().url(url).post(JsonRequestBody.from(data))
176+
Response response = (allowCache ? getHttpClientWithCache() : getHttpClient()).newCall(
177+
new Request.Builder().url(url).post(JsonRequestBody.from(data))
177178
.header("Content-Type", "application/json").build()
178179
).execute();
179180
// 200/204 == success, 304 == cache valid
@@ -193,7 +194,8 @@ public static byte[] doHttpPost(String url,String data,boolean allowCache) throw
193194

194195
public static byte[] doHttpGet(String url,ProgressListener progressListener) throws IOException {
195196
Log.d("Http", "Progress URL: " + url);
196-
Response response = httpClient.newCall(makeRequestBuilder().url(url).get().build()).execute();
197+
Response response = getHttpClient().newCall(
198+
new Request.Builder().url(url).get().build()).execute();
197199
if (response.code() != 200 && response.code() != 204) {
198200
throw new IOException("Received error code: "+ response.code());
199201
}
@@ -442,28 +444,40 @@ public void writeTo(@NonNull BufferedSink bufferedSink) throws IOException {
442444
*/
443445
public static String updateLink(String string) {
444446
if (string.startsWith("https://cdn.jsdelivr.net/gh/Magisk-Modules-Repo/")) {
445-
int start = string.lastIndexOf('@'),
446-
end = string.lastIndexOf('/');
447+
String tmp = string.substring(48);
448+
int start = tmp.lastIndexOf('@'),
449+
end = tmp.lastIndexOf('/');
447450
if ((end - 8) <= start) return string; // Skip if not a commit id
448-
return string.substring(0, start + 1) + "master" + string.substring(end);
451+
return "https://raw.githubusercontent.com/" +
452+
tmp.substring(0, start) + "/master" + string.substring(end);
449453
}
450454
if (string.startsWith("https://github.com/Magisk-Modules-Repo/")) {
451455
int i = string.lastIndexOf("/archive/");
452-
if (i != -1) return string.substring(0, i + 9) + "master.zip";
456+
if (i != -1 && string.indexOf('/', i + 9) == -1)
457+
return string.substring(0, i + 9) + "master.zip";
453458
}
454-
return fixUpLink(string);
459+
return string;
455460
}
456461

457462
/**
458-
* Change URL to appropriate url
463+
* Change GitHub user-content url to jsdelivr url
464+
* (Unused but kept as a documentation)
459465
*/
460-
public static String fixUpLink(String string) {
466+
public static String cdnIfyLink(String string) {
461467
if (string.startsWith("https://raw.githubusercontent.com/")) {
462468
String[] tokens = string.substring(34).split("/", 4);
463469
if (tokens.length != 4) return string;
464470
return "https://cdn.jsdelivr.net/gh/" +
465471
tokens[0] + "/" + tokens[1] + "@" + tokens[2] + "/" + tokens[3];
466472
}
473+
if (string.startsWith("https://github.com/")) {
474+
int i = string.lastIndexOf("/archive/");
475+
if (i == -1 || string.indexOf('/', i + 9) != -1)
476+
return string; // Not an archive link
477+
String[] tokens = string.substring(19).split("/", 4);
478+
return "https://cdn.jsdelivr.net/gh/" +
479+
tokens[0] + "/" + tokens[1] + "@" + tokens[2] + "/" + tokens[3];
480+
}
467481
return string;
468482
}
469483
}

app/src/main/java/com/fox2code/mmm/utils/PropUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public static void readProperties(ModuleInfo moduleInfo, InputStream inputStream
160160
break;
161161
case "updateJson":
162162
if (isInvalidURL(value)) break;
163-
moduleInfo.updateJson = Http.fixUpLink(value);
163+
moduleInfo.updateJson = value;
164164
readUpdateJson = true;
165165
break;
166166
case "support":

0 commit comments

Comments
 (0)