Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New module] Skip redirect #10

Open
stephenhawk8054 opened this issue Feb 16, 2022 · 2 comments
Open

[New module] Skip redirect #10

stephenhawk8054 opened this issue Feb 16, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@stephenhawk8054
Copy link

Hi, I don't know if it would be too difficult to implement or not but it would be nice if there's another module to skip redirection links, similar to how Skip Redirect extension works:

https://github.com/sblask/webextension-skip-redirect
https://addons.mozilla.org/en-US/firefox/addon/skip-redirect/

For example this link

https://www.google.com/chrome/?or-maybe-rather-firefox=https%3A%2F%2Fwww.mozilla.org/

can be rewritten to

https://www.mozilla.org/

I really like this project, one of my must-install app on android now.

@TrianguloY
Copy link
Owner

As with #7 the main issue is to obtain the database with all the redirections to use.

Clear Urls have a public database with all the rules, that's the main reason it was implemented. In any case ClearUrl does some of the static redirections too (though probably not all).

@TrianguloY TrianguloY added the enhancement New feature or request label Feb 16, 2022
@stephenhawk8054
Copy link
Author

stephenhawk8054 commented Feb 17, 2022

As far as I see from Skip Redirect's issues, their approach is just using static redirections, and no need for a database.

I think most of the redirections are in this link:
https://github.com/sblask/webextension-skip-redirect/blob/main/url.js

I really don't know how to code in Java, so it might look really bad but this is the best I could do to replicate the codes:

Java code
import java.util.*;
import java.util.regex.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

class Bundle {
    public LinkedList<String> prefix;
    public String prefixString;
}

public class main {
    public static String decodeURIComponent(String s, String charset) {
        if (s == null) {
            return null;
        }
        String result = null;
        try {
            result = URLDecoder.decode(s, charset);
        }
        // This exception should never occur.
        catch (UnsupportedEncodingException e) {
            result = s;
        }
        return result;
    }
    
    public static Bundle comprehend(LinkedList<String> pList, String s) {
        LinkedList<String> result = new LinkedList<String>();
        result.push("www\\.");
        for (String p : pList) {
            result.push(p + s);
        }

        String resultString = "(?:";
        for (int i = 0; i < result.size(); i++) {
            resultString = resultString + result.get(i) + "|";
        }
        resultString = resultString.substring(0, resultString.length() - 1) + ")";

        Bundle resultBundle = new Bundle();
        resultBundle.prefix = result;
        resultBundle.prefixString = resultString;

        return resultBundle;
    }

    public static Pattern getQueryRegexpPlainProtocol(String plainString) {
        return Pattern.compile("https?://.*" + "=" + "(" + plainString + "(?:[^?&;#]*[?][^?]*|[^?&;#]*)" + ")", Pattern.CASE_INSENSITIVE);
    }

    public static String maybeDecode(String urlMatch, Pattern percentReg, Pattern symbolReg) {
        Matcher percentMatcher = percentReg.matcher(urlMatch);
        Matcher symbolMatcher = symbolReg.matcher(urlMatch);

        while (percentMatcher.find()) {
            urlMatch = decodeURIComponent(urlMatch, "UTF-8");
            break;
        }
        while (symbolMatcher.find()) {
            urlMatch = decodeURIComponent(urlMatch, "UTF-8");
            break;
        }
        if (urlMatch.indexOf("http") != 0) {
            urlMatch = "http://" + urlMatch;
        }
        return urlMatch;
    }

    public static String getPlainMatches(String url, String plainString, Pattern plainReg) {
        List<Pattern> list = new ArrayList<Pattern>();
        list.add(plainReg);
        list.add(getQueryRegexpPlainProtocol(plainString));
        for (Pattern p: list) {
            Matcher matcher = p.matcher(url);
            if (matcher.find()) {
                return matcher.group(1);
            }
        }
        return null;
    }

    public static String getRedirectTarget(String url, String plainString, Pattern plainReg, Pattern percentReg, Pattern symbolReg) {
        String extractedUrl = getPlainMatches(url, plainString, plainReg);
        if (extractedUrl != null) {
            return getRedirectTarget(maybeDecode(extractedUrl, percentReg, symbolReg), plainString, plainReg, percentReg, symbolReg);
        }
        return url;
    }

    public static void main(String[] args) {
        String[] protocols = {
            "rtsp",
            "mms",
            "https",
            "http",
            "ftp",
        };
        LinkedList<String> protocolList = new LinkedList<String>();
        Collections.addAll(protocolList, protocols);

        // possibleColonPrefix
        Bundle colonBundle = comprehend(protocolList, "(?::)");
        LinkedList<String> possibleColonPrefixes = colonBundle.prefix;
        String possibleColonPrefixesString = colonBundle.prefixString;
        
        // possiblePlainPrefixes
        Bundle plainBundle = comprehend(protocolList, "(?::|%3A|%253A)");
        LinkedList<String> possiblePlainPrefixes = plainBundle.prefix;
        String possiblePlainPrefixesString = plainBundle.prefixString;

        // pathRegexpPlainProtocol
        Pattern pathRegexpPlainProtocol = Pattern.compile("https?://.*?" + "(?:[^/][/]|\\?)" + "(" + possibleColonPrefixesString + ".*$" + ")", Pattern.CASE_INSENSITIVE);
        // %
        Pattern percentRegExp = Pattern.compile("%25", Pattern.CASE_INSENSITIVE);
        // #$&+,/:;=?@
        Pattern symbolRegExp = Pattern.compile("(%23|%24|%26|%2B|%2C|%2F|%3A|%3B|%3D|%3F|%40)", Pattern.CASE_INSENSITIVE);

        // Test
        String[] testUrls = {
            "https://www.google.com/chrome/?or-maybe-rather-firefox=https%3A%2F%2Fwww.mozilla.org/", // -> https://www.mozilla.org/
            "https://www.google.com/?url=www.example.com", // -> http://www.example.com
            "https://www.youtube.com/redirect?event=backstage_event&redir_token=QUFFLUhqbnUyRDFFeWFuWm5LRy1JZnZnSEV4dVF5QWFWUXxBQ3Jtc0trQ3FpMkotdDJpQkhzTjVBWGhUWExEYjRsUzNrSEFtX2lFQ2R5QTFHT2RRQV9iZjNMcENLTVo1LVhjaDRFQmp4Z2tHbHMxNVhaN1kwTG9CdVZCMXNtSDhfY1FIbTdwY3N5SkZQbTRIOF9uZS1DME5nOA&q=https%3A%2F%2Ftwitter.com%2FKW7MD8FEWT7lMXx%2Fstatus%2F1493586411479392261%3Ft%3D5UmHquFVB6A_R-CbFgUM2g%26s%3D19&html_redirect=1", // -> https://twitter.com/KW7MD8FEWT7lMXx/status/1493586411479392261?t=5UmHquFVB6A_R-CbFgUM2g&s=19
        };
        for (String url: testUrls) {
            String redirectUrl = getRedirectTarget(url, possiblePlainPrefixesString, pathRegexpPlainProtocol, percentRegExp, symbolRegExp);
            System.out.println("---------------------------------------------------------");
            System.out.println(url);
            System.out.println("-> " + redirectUrl);
        }
    }
}

I just did the plain text URL first, and haven't included Encoded, Base64 or Exceptions situations. The results of 3 example links look like this

image

I think the exceptions are in this link:

https://github.com/sblask/webextension-skip-redirect/blob/89b42e5a5e477ca151c33e82c72a4e686c8ccccc/background.js#L37

https://github.com/sblask/webextension-skip-redirect/blob/89b42e5a5e477ca151c33e82c72a4e686c8ccccc/background.js#L46

TrianguloY pushed a commit that referenced this issue Oct 5, 2022
This action updates assets files, currently there is only one asset
file, the ClearURLs catalog, but the action can be easily expanded if
more are added ( maybe these issues can benefit from it: #82 #61 #10 #7
), just copy and paste the ClearURLS catalog step and change the
enviroment variables.

Co-authored-by: Ilithy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants