Skip to content

Commit

Permalink
issue-2 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunbee Kim committed Sep 30, 2020
1 parent 01d330e commit a94c9da
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.metadata/
/bin/
Binary file modified bin/Kernel32.class
Binary file not shown.
Binary file modified bin/UrlCheck.class
Binary file not shown.
Binary file modified bin/window/UrlCheck.jar
Binary file not shown.
150 changes: 25 additions & 125 deletions src/UrlCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,19 @@
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
final static String regex = "(https?):\\/\\/[-a-zA-Z0-9+&@#%?=~_|!:,.;]*[-a-zA-Z0-9+&@#%=~_|\\/]*";

// 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()
{
Expand All @@ -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("| |");
Expand All @@ -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 <fileName> |");
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 <fileName> |");
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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"))
{
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions src/UrlCheckForMac.java
Original file line number Diff line number Diff line change
@@ -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);

}
}
}
82 changes: 82 additions & 0 deletions src/UrlCheckForWindow.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit a94c9da

Please sign in to comment.