Skip to content

Commit

Permalink
feat(Sync for Reddit): Add Fix /s/ links patch
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Feb 9, 2024
1 parent 85504f6 commit a8c82ad
Showing 1 changed file with 42 additions and 0 deletions.
@@ -0,0 +1,42 @@
package app.revanced.integrations.syncforreddit;

import android.os.StrictMode;
import app.revanced.integrations.shared.Logger;

import java.net.HttpURLConnection;
import java.net.URL;

public final class FixSLinksPatch {
public static String resolveSLink(String link) {
if (link.matches(".*reddit\\.com/r/[^/]+/s/[^/]+")) {
Logger.printInfo(() -> "Resolving " + link);
try {
URL url = new URL(link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("HEAD");

// Disable strict mode in order to allow network access on the main thread.
// This is not ideal, but it's the easiest solution for now.
final var currentPolicy = StrictMode.getThreadPolicy();
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

connection.connect();
String location = connection.getHeaderField("location");
connection.disconnect();

// Restore the original strict mode policy.
StrictMode.setThreadPolicy(currentPolicy);

Logger.printInfo(() -> "Resolved " + link + " -> " + location);

return location;
} catch (Exception e) {
Logger.printException(() -> "Failed to resolve " + link, e);
}
}

return link;
}
}

0 comments on commit a8c82ad

Please sign in to comment.