Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[Java] RedirectScanner - show real URL filter_list
Author
Message
[Java] RedirectScanner - show real URL #1
Hello HC,

this code snippet checks a given URL for redirects. I.e. if there is someone providing a shortened link with services like TinyURL can check the real URL here without visiting the site.

Code:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class RedirectLocationScanner {

    public static void main(final String[] args) throws IOException {
        
        String urlStr = null;

        if (!(args.length == 1)) {
            Scanner scan = new Scanner(System.in);
            System.out.println("URL?");
            urlStr = scan.nextLine();
            scan.close();
        } else {
            urlStr = args[0];
        }
        
        if(!urlStr.startsWith("http://")){
            urlStr = "http://" + urlStr;
        }
        
        URL url = new URL(urlStr);
        HttpURLConnection.setFollowRedirects(false);
        String redirectLoc = url.openConnection().getHeaderField("Location");
        
        if(redirectLoc == null){
            System.out.println("No redirect.");
        } else {
            System.out.println("real URL: " + redirectLoc);
        }
    }
}

__________________________________________________________________

This code is an updated version, that also scans for target links in ad link services like adfly, adfoc and linkbucks.

Code:
package scanner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class LinkScanner {

    private static final String[] adServiceStrings = { "adf.ly", "adfoc.us",
            "any.gs", "tinylinks.co", "linkbucks.com", "yyv.co", "miniurls.co",
            "qqc.co", "whackyvidz.com", "ultrafiles.net", "dyo.gs",
            "megaline.co", "uberpicz.com", "linkgalleries.net", "qvvo.com",
            "urlbeat.net", "seriousfiles.com", "zxxo.net", "ugalleries.net",
            "picturesetc.net" };

    private static final String USAGE = "java -jar scanner.jar <URL>";

    public static void main(String[] args) {
        try {
            if (args.length == 1) {
                System.out.println("Target URL: ");
                scanAndPrint(args[0]);
            } else if (args.length == 0) {
                Scanner scan = new Scanner(System.in);
                System.out.println("URL?");
                String url = scan.nextLine();
                scanAndPrint(url);
            } else {
                System.out.println(USAGE);
            }
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
    }

    private static void scanAndPrint(String string) throws IOException {
        System.out.println("Scanning ...");
        String target = new LinkScanner().scan(string);
        if (target != null) {
            System.out.println("Target URL: ");
            System.out.println(target);
        } else {
            System.err.println("No target URL found");
        }
    }

    public String scan(String urlStr) throws IOException {
        if (!urlStr.startsWith("http://")) {
            urlStr = "http://" + urlStr;
        }
        URL url = new URL(urlStr);
        HttpURLConnection.setFollowRedirects(false);

        if (isAdServiceLink(urlStr)) {
            return handleAdLink(url);
        } else {
            return handleRedirect(url);
        }

    }

    private boolean isAdServiceLink(String urlStr) {
        for (String str : adServiceStrings) {
            if (urlStr.contains(str)) {
                return true;
            }
        }
        return false;
    }

    private String handleRedirect(URL url) throws IOException {
        return url.openConnection().getHeaderField("Location");
    }

    private String handleAdLink(URL url) throws IOException {
        URLConnection urlc = url.openConnection();
        urlc.addRequestProperty("user-agent", "Firefox");
        BufferedReader in = null;
        try {
            in = new BufferedReader(
                    new InputStreamReader(urlc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                if (line.contains("var zzz =")) {
                    return extractADFlyURL(line);
                }
                if (line.contains("var click_url =")
                        && !line.contains("//var click_url =")) {
                    return extractADFocURL(line);
                }
                if (line.contains("Lbjs.TargetUrl =")) {
                    return extractLinkBucksURL(line);
                }
            }
            throw new IOException("Unable to find target URL in link");
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    private String extractLinkBucksURL(String line) {
        String go = line.split("'")[1];
        return go;
    }

    private String extractADFocURL(String line) throws IOException {
        String go = line.split("\"")[1];
        return go;
    }

    private String extractADFlyURL(String line) throws IOException {
        String go = line.split("'")[1];
        String redirect = handleRedirect(new URL("http://adf.ly" + go));
        if (redirect == null) {
            return go;
        }
        return redirect;
    }
}

Reply





Messages In This Thread
[Java] RedirectScanner - show real URL - by Deque - 10-24-2012, 01:20 PM



Users browsing this thread: 1 Guest(s)