Login Register






[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

RE: [Java] RedirectScanner - show real URL #2
will this work on every type of short links?

Reply

RE: [Java] RedirectScanner - show real URL #3
Edit: They have to use redirects. Something like adf.ly doesn't work. Maybe I will provide a solution for this too.

Reply

RE: [Java] RedirectScanner - show real URL #4
nice code worked for me!

will wait for your solution for adf.ly links.

Reply

RE: [Java] RedirectScanner - show real URL #5
Here it is:

Code:
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 RedirectLocationScanner { public static void main(final String[] args) throws IOException { String urlStr = getURLStr(args); if (!urlStr.startsWith("http://")) { urlStr = "http://" + urlStr; } URL url = new URL(urlStr); HttpURLConnection.setFollowRedirects(false); if (urlStr.contains("adf.ly")) { handleAdfLyLink(url); } else { handleRedirect(url); } } private static String getURLStr(final String[] args) { String urlStr; if (!(args.length == 1)) { Scanner scan = new Scanner(System.in); System.out.println("URL?"); urlStr = scan.nextLine(); scan.close(); } else { urlStr = args[0]; } return urlStr; } private static void handleRedirect(URL url) throws IOException { String redirectLoc = url.openConnection().getHeaderField("Location"); if (redirectLoc == null) { System.out.println("No redirect."); } else { System.out.println("real URL: " + redirectLoc); } } private static void handleAdfLyLink(URL url) throws IOException { URLConnection urlc = url.openConnection(); urlc.addRequestProperty("user-agent", "Firefox"); try (BufferedReader in = new BufferedReader(new InputStreamReader( urlc.getInputStream()))) { String line; boolean foundURL = false; while ((line = in.readLine()) != null) { if (line.contains("var url =")) { extractAndPrintURL(line); foundURL = true; break; } } if (!foundURL) { System.err.println("Unable to find URL in adf.ly link"); } } } private static void extractAndPrintURL(String line) throws IOException { String go = line.split("'")[1]; handleRedirect(new URL("http://adf.ly" + go)); } }

Example:
Quote:URL?
http://adf.ly/5XR
real URL: http://www.google.com

Reply

RE: [Java] RedirectScanner - show real URL #6
(10-24-2012, 02:54 PM)Deque Wrote: Here it is:

Code:
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 RedirectLocationScanner { public static void main(final String[] args) throws IOException { String urlStr = getURLStr(args); if (!urlStr.startsWith("http://")) { urlStr = "http://" + urlStr; } URL url = new URL(urlStr); HttpURLConnection.setFollowRedirects(false); if (urlStr.contains("adf.ly")) { handleAdfLyLink(url); } else { handleRedirect(url); } } private static String getURLStr(final String[] args) { String urlStr; if (!(args.length == 1)) { Scanner scan = new Scanner(System.in); System.out.println("URL?"); urlStr = scan.nextLine(); scan.close(); } else { urlStr = args[0]; } return urlStr; } private static void handleRedirect(URL url) throws IOException { String redirectLoc = url.openConnection().getHeaderField("Location"); if (redirectLoc == null) { System.out.println("No redirect."); } else { System.out.println("real URL: " + redirectLoc); } } private static void handleAdfLyLink(URL url) throws IOException { URLConnection urlc = url.openConnection(); urlc.addRequestProperty("user-agent", "Firefox"); try (BufferedReader in = new BufferedReader(new InputStreamReader( urlc.getInputStream()))) { String line; boolean foundURL = false; while ((line = in.readLine()) != null) { if (line.contains("var url =")) { extractAndPrintURL(line); foundURL = true; break; } } if (!foundURL) { System.err.println("Unable to find URL in adf.ly link"); } } } private static void extractAndPrintURL(String line) throws IOException { String go = line.split("'")[1]; handleRedirect(new URL("http://adf.ly" + go)); } }

Example:
Quote:URL?
http://adf.ly/5XR
real URL: http://www.google.com

this time got some errors!

Code:
RedirectLocationScanner.java:54: '{' expected try (BufferedReader in = new BufferedReader(new InputStreamReader( ^ RedirectLocationScanner.java:54: ')' expected try (BufferedReader in = new BufferedReader(new InputStreamReader( ^ RedirectLocationScanner.java:55: ';' expected urlc.getInputStream()))) { ^ RedirectLocationScanner.java:54: 'try' without 'catch' or 'finally' try (BufferedReader in = new BufferedReader(new InputStreamReader( ^ RedirectLocationScanner.java:71: illegal start of expression private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:71: illegal start of expression private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:71: ';' expected private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:71: ')' expected private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:71: illegal start of expression private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:71: ';' expected private static void extractAndPrintURL(String line) ^ RedirectLocationScanner.java:72: not a statement throws IOException { ^ RedirectLocationScanner.java:72: ';' expected throws IOException { ^ RedirectLocationScanner.java:76: reached end of file while parsing } ^ 13 errors

Reply

RE: [Java] RedirectScanner - show real URL #7
You have a Java version < 1.7.
1.7 introduced try-with-resources statements which I used here.

This code should work in 1.6:
Code:
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 RedirectLocationScanner { public static void main(final String[] args) throws IOException { String urlStr = getURLStr(args); if (!urlStr.startsWith("http://")) { urlStr = "http://" + urlStr; } URL url = new URL(urlStr); HttpURLConnection.setFollowRedirects(false); if (urlStr.contains("adf.ly")) { handleAdfLyLink(url); } else { handleRedirect(url); } } private static String getURLStr(final String[] args) { String urlStr; if (!(args.length == 1)) { Scanner scan = new Scanner(System.in); System.out.println("URL?"); urlStr = scan.nextLine(); scan.close(); } else { urlStr = args[0]; } return urlStr; } private static void handleRedirect(URL url) throws IOException { String redirectLoc = url.openConnection().getHeaderField("Location"); if (redirectLoc == null) { System.out.println("No redirect."); } else { System.out.println("real URL: " + redirectLoc); } } private static void handleAdfLyLink(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; boolean foundURL = false; while ((line = in.readLine()) != null) { if (line.contains("var url =")) { extractAndPrintURL(line); foundURL = true; break; } } if (!foundURL) { System.err.println("Unable to find URL in adf.ly link"); } } finally { if (in != null) { in.close(); } } } private static void extractAndPrintURL(String line) throws IOException { String go = line.split("'")[1]; handleRedirect(new URL("http://adf.ly" + go)); } }

Reply

RE: [Java] RedirectScanner - show real URL #8
yup my java version is 1.6.now its working!

Reply

RE: [Java] RedirectScanner - show real URL #9
How to use this codes? D::

Reply

RE: [Java] RedirectScanner - show real URL #10
(01-07-2013, 07:30 PM)Zakne Wrote: How to use this codes? D::

Use this: http://www.multiupload.nl/CRC1KW6Y0R
It is the same, but a runnable jar with a GUI.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply