@@ -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}
0 commit comments