diff --git a/.gitignore b/.gitignore index e10e727..2715f36 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /.metadata/ +/bin/ diff --git a/bin/Kernel32.class b/bin/Kernel32.class index 64707a2..56f86e4 100644 Binary files a/bin/Kernel32.class and b/bin/Kernel32.class differ diff --git a/bin/UrlCheck.class b/bin/UrlCheck.class index 736eb86..87651aa 100644 Binary files a/bin/UrlCheck.class and b/bin/UrlCheck.class differ diff --git a/bin/window/UrlCheck.jar b/bin/window/UrlCheck.jar index bc68ded..5297e5b 100644 Binary files a/bin/window/UrlCheck.jar and b/bin/window/UrlCheck.jar differ diff --git a/src/UrlCheck.java b/src/UrlCheck.java index 2ad7252..f570777 100644 --- a/src/UrlCheck.java +++ b/src/UrlCheck.java @@ -4,22 +4,11 @@ import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.sun.jna.Library; -import com.sun.jna.Native; - -interface Kernel32 extends Library { - - boolean SetConsoleTextAttribute(int h_ConsoleOutput, int u16_Attributes); - - int GetStdHandle(int u32_Device); - -} public class UrlCheck { // url set regex @@ -27,112 +16,7 @@ public class UrlCheck { // delimiter to get url from input file final static String delimiter = "[\\[\\]\"<>'\n\b\r]"; - - //text for mac -// public static final String RED = "\033[0;31m"; -// public static final String GREEN = "\033[0;32m"; -// public static final String WHITE = "\033[0;37m"; -// public static final String BLUE ="\033[0;34m"; -// public static final String RESET = "\033[0m"; - - // text for window - static final int GRAY = 0x7; - static final int GREEN = 0xA; - static final int RED = 0xC; - static final int WHITE = 0xF; - static final int BLUE = 0x9; - static final int STD_OUTPUT_HANDLE = -11; - - - // request url and check the response - public static void availableURL(String host) - { - Kernel32 lib = Native.load("kernel32", Kernel32.class); - - try { - // request url - URL url = new URL(host); - URLConnection con; - con = url.openConnection(); - - // response - HttpURLConnection exitCode = (HttpURLConnection) con; - exitCode.setInstanceFollowRedirects(true); - HttpURLConnection.setFollowRedirects(true); - exitCode.setConnectTimeout(1000); - - - // response code result - if(exitCode.getResponseCode() >= 200 && exitCode.getResponseCode() < 300) - { - - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), GREEN); - System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Good"); - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); - } - else if(exitCode.getResponseCode() >= 400 && exitCode.getResponseCode() < 500) - { - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), RED); - System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Bad"); - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); - } - else if(exitCode.getResponseCode() == 301 || exitCode.getResponseCode() == 307 || exitCode.getResponseCode() == 308 ) - { - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), BLUE); - System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Redirect"); - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); - - // redirect to new location by Recursion itself when it is 301,307,308 - String newUrl = exitCode.getHeaderField("Location"); - availableURL(newUrl); - - } - else - { - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), GRAY); - System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Unknown"); - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); - } - - }catch (Exception e) { - // response fail, server is not existed - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), RED); - System.out.println("[599] "+ host +" - Fail" ); - lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); - } -// // for Mac -// if(exitCode.getResponseCode() >= 200 && exitCode.getResponseCode() < 300) -// { -// -// System.out.println(GREEN+"["+exitCode.getResponseCode()+"] "+ host +" - Good"+RESET); -// -// } -// else if(exitCode.getResponseCode() >= 400 && exitCode.getResponseCode() < 500) -// { -// System.out.println(RED+"["+exitCode.getResponseCode()+"] "+ host +" - Bad"+RESET); -// } -// else if(exitCode.getResponseCode() == 301 || exitCode.getResponseCode() == 307 || exitCode.getResponseCode() == 308 ) -// { -// System.out.println(BLUE + "["+exitCode.getResponseCode()+"] "+ host +" - Redirect"+ RESET); -// -// // redirect to new location by Recursion itself when it is 301,307,308 -// String newUrl = exitCode.getHeaderField("Location"); -// availableURL(newUrl); -// -// } -// else -// { -// System.out.println(RED+"["+exitCode.getResponseCode()+"] "+ host +" - Unknown"+RESET); -// } -// -// }catch (Exception e) { -// // response fail, server is not existed -// System.out.println(RED+"[599] "+ host +" - Fail" +RESET); -// -// } - - - } + public static void helpMessage() { @@ -147,7 +31,7 @@ public static void helpMessage() System.out.println("| |"); System.out.println("| 2) UrlCheck help |"); System.out.println("| |"); - System.out.println("| 3) Option a, s, v |"); + System.out.println("| 3) Option a, s, v, m |"); System.out.println("| |"); System.out.println("| For Window : |"); System.out.println("| |"); @@ -169,6 +53,11 @@ public static void helpMessage() System.out.println("| |"); System.out.println("| For Mac : |"); System.out.println("| |"); + System.out.println("| Option m : To run the tool in Mac |"); + System.out.println("| |"); + System.out.println("| UrlCheck /m |"); + System.out.println("| ex) UrlCheck /m index2.html |"); + System.out.println("| |"); System.out.println("| Option a : To check for archived versions of URLs |"); System.out.println("| |"); System.out.println("| UrlCheck /a |"); @@ -203,13 +92,13 @@ public static void endMessage() // list up url in input file - public static void fileUrlListUp(String fName, boolean archived, boolean secured) + public static void fileUrlListUp(String fName, boolean archived, boolean secured, boolean runMac) { String regSecure = "^(http)://"; Pattern pat = Pattern.compile(regex, Pattern.MULTILINE); BufferedReader br; - + try { br = new BufferedReader(new FileReader(fName)); String line = null; @@ -223,8 +112,15 @@ public static void fileUrlListUp(String fName, boolean archived, boolean secured { // change http to https if(secured) str = str.replaceFirst(regSecure, "https://"); - - availableURL(str); + + if(runMac) + { + UrlCheckForMac.availableURL(str); + } + else + { + UrlCheckForWindow.availableURL(str); + } // request archived if(archived) archiveUrl(str); @@ -277,6 +173,7 @@ public static void main(String[] args) { boolean archived = false; boolean secured = false; + boolean runMac = false; if(args.length == 0 || args[0].toLowerCase().equals("help")) { @@ -301,12 +198,15 @@ public static void main(String[] args) { // secure request flag handle if(args[0].contains("s")) secured = true; - if(archived || secured) + // secure request flag handle + if(args[0].contains("m")) runMac = true; + + if(archived || secured || runMac) { for(int i = 1; i < args.length; i++) { System.out.println("File : " + args[i]); - fileUrlListUp(args[i],archived,secured); + fileUrlListUp(args[i],archived,secured,runMac); } } @@ -316,7 +216,7 @@ public static void main(String[] args) { for(int i = 0; i < args.length; i++) { System.out.println("File : " + args[i]); - fileUrlListUp(args[i],archived,secured); + fileUrlListUp(args[i],archived,secured,runMac); } } } diff --git a/src/UrlCheckForMac.java b/src/UrlCheckForMac.java new file mode 100644 index 0000000..878ee5f --- /dev/null +++ b/src/UrlCheckForMac.java @@ -0,0 +1,62 @@ +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; + +public class UrlCheckForMac { + + //text for mac + public static final String RED = "\033[0;31m"; + public static final String GREEN = "\033[0;32m"; + public static final String WHITE = "\033[0;37m"; + public static final String BLUE ="\033[0;34m"; + public static final String RESET = "\033[0m"; + + // request url and check the response + public static void availableURL(String host) + { + + try { + // request url + URL url = new URL(host); + URLConnection con; + con = url.openConnection(); + + // response + HttpURLConnection exitCode = (HttpURLConnection) con; + exitCode.setInstanceFollowRedirects(true); + HttpURLConnection.setFollowRedirects(true); + exitCode.setConnectTimeout(1000); + + +// // for Mac + if(exitCode.getResponseCode() >= 200 && exitCode.getResponseCode() < 300) + { + + System.out.println(GREEN+"["+exitCode.getResponseCode()+"] "+ host +" - Good"+RESET); + + } + else if(exitCode.getResponseCode() >= 400 && exitCode.getResponseCode() < 500) + { + System.out.println(RED+"["+exitCode.getResponseCode()+"] "+ host +" - Bad"+RESET); + } + else if(exitCode.getResponseCode() == 301 || exitCode.getResponseCode() == 307 || exitCode.getResponseCode() == 308 ) + { + System.out.println(BLUE + "["+exitCode.getResponseCode()+"] "+ host +" - Redirect"+ RESET); + + // redirect to new location by Recursion itself when it is 301,307,308 + String newUrl = exitCode.getHeaderField("Location"); + availableURL(newUrl); + + } + else + { + System.out.println(RED+"["+exitCode.getResponseCode()+"] "+ host +" - Unknown"+RESET); + } + + }catch (Exception e) { + // response fail, server is not existed + System.out.println(RED+"[599] "+ host +" - Fail" +RESET); + + } + } +} diff --git a/src/UrlCheckForWindow.java b/src/UrlCheckForWindow.java new file mode 100644 index 0000000..c654e37 --- /dev/null +++ b/src/UrlCheckForWindow.java @@ -0,0 +1,82 @@ +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import com.sun.jna.Library; +import com.sun.jna.Native; + +interface Kernel32 extends Library { + + boolean SetConsoleTextAttribute(int h_ConsoleOutput, int u16_Attributes); + + int GetStdHandle(int u32_Device); + +} + +public class UrlCheckForWindow { + + // text for window + static final int GRAY = 0x7; + static final int GREEN = 0xA; + static final int RED = 0xC; + static final int WHITE = 0xF; + static final int BLUE = 0x9; + static final int STD_OUTPUT_HANDLE = -11; + + // request url and check the response + public static void availableURL(String host) + { + Kernel32 lib = Native.load("kernel32", Kernel32.class); + + try { + // request url + URL url = new URL(host); + URLConnection con; + con = url.openConnection(); + + // response + HttpURLConnection exitCode = (HttpURLConnection) con; + exitCode.setInstanceFollowRedirects(true); + HttpURLConnection.setFollowRedirects(true); + exitCode.setConnectTimeout(1000); + + + // response code result + if(exitCode.getResponseCode() >= 200 && exitCode.getResponseCode() < 300) + { + + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), GREEN); + System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Good"); + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); + } + else if(exitCode.getResponseCode() >= 400 && exitCode.getResponseCode() < 500) + { + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), RED); + System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Bad"); + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); + } + else if(exitCode.getResponseCode() == 301 || exitCode.getResponseCode() == 307 || exitCode.getResponseCode() == 308 ) + { + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), BLUE); + System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Redirect"); + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); + + // redirect to new location by Recursion itself when it is 301,307,308 + String newUrl = exitCode.getHeaderField("Location"); + availableURL(newUrl); + + } + else + { + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), GRAY); + System.out.println("["+exitCode.getResponseCode()+"] "+ host +" - Unknown"); + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); + } + + }catch (Exception e) { + // response fail, server is not existed + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), RED); + System.out.println("[599] "+ host +" - Fail" ); + lib.SetConsoleTextAttribute(lib.GetStdHandle(STD_OUTPUT_HANDLE), WHITE); + } + } +}