-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckWebsiteAlive.java
More file actions
250 lines (218 loc) · 7.41 KB
/
CheckWebsiteAlive.java
File metadata and controls
250 lines (218 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class CheckWebsiteAlive {
// this DEAD_PHRASES is obsoleted, for reference only. The program will use a dead phrases from text file
private static final String[] DEAD_PHRASES = new String[]{
"is expired and be suspended",
"hosting for this domain is not configured",
"cannot connect"
};
private static List<String> listUrl = new ArrayList<>();
private static List<String> listOfDeadPhrases = new ArrayList<>();
public static void main(String[] args) {
System.out.println("******");
System.out.println("This program will help you to check if a website from a list is dead or alive. To see the instruction, please visit: https://github.com/scorta/CheckWebsiteAlive");
System.out.println("******");
System.out.println("Start working...");
String inputFile = "input.txt";
String deadPhrasesFile = "dead_phrases.txt";
StringBuilder result = new StringBuilder();
if (args.length != 4) {
System.out.println("Must have 4 params: file name that contains list of urls, " +
"and file name that contains list of dead phrases, number of thread(s), " +
"and choice (0 == checking url alive, 1 == get page source)");
return;
}
inputFile = args[0];
listUrl = importList(inputFile);
if (listUrl.size() == 0) {
System.out.println("NO URLS, will exit now");
return;
}
deadPhrasesFile = args[1];
listOfDeadPhrases = importList(deadPhrasesFile);
if (listOfDeadPhrases.size() == 0) {
System.out.println("WARN: no dead phrases imported, are you sure?");
}
CheckUrl.listOfDeadPhrases = listOfDeadPhrases;
int numberOfThreads = Integer.parseInt(args[2]);
if (numberOfThreads < 1) {
System.out.println("WARN: 0 thread? Will use 1 now");
numberOfThreads = 1;
}
int choice = Integer.parseInt(args[3]);
Thread[] listThread = new Thread[numberOfThreads];
{
int start, end = 0, step;
step = 1 + listUrl.size() / numberOfThreads;
for (int i = 0; i < numberOfThreads; ++i) {
start = end;
end = start + step;
if (end >= listUrl.size()) {
end = listUrl.size() - 1;
}
System.out.println("Thread " + i + " covers " + start + " " + end);
if (choice == 0) {
listThread[i] = new Thread(new CheckUrlAliveThread("Thread " + i, listUrl, result, start, end));
} else {
listThread[i] = new Thread(new GetPageSourceThread("Thread " + i, listUrl, result, start, end));
}
listThread[i].start();
}
}
for (int i = 0; i < numberOfThreads; ++i) {
try {
listThread[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writeOutput(inputFile, result.toString());
}
private static void forceExit() {
System.out.println("This program has encountered an error and will now exit.");
System.exit(1);
}
private static void writeOutput(String fileName, String content) {
try {
if (content.isEmpty()) {
System.out.println("WARN: Empty content");
return;
}
PrintWriter output = new PrintWriter(fileName + "_out.txt");
output.print(content);
output.close();
System.out.println("Done. Please check the output file. It is named " + fileName + "_out.txt");
} catch (Exception e) {
System.out.println("Error while writing result: " + e.toString());
forceExit();
}
}
private static ArrayList<String> importList(String fileName) {
try {
ArrayList<String> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader(fileName));
for (String line; (line = br.readLine()) != null; ) {
list.add(line);
}
return list;
} catch (Exception e) {
System.out.println("Error while reading list: " + e.toString());
forceExit();
return null;
}
}
}
class CheckUrlAliveThread implements Runnable {
private final String threadName;
private final List<String> listUrl;
private final StringBuilder res;
private final int start;
private final int end;
@Override
public void run() {
for (int i = start; i < end; ++i) {
StringBuilder sbTemp = new StringBuilder();
sbTemp.append(listUrl.get(i));
sbTemp.append("\t");
System.out.println(threadName + " is checking: " + listUrl.get(i));
String livingStatus = CheckUrl.isDead(listUrl.get(i)) ? "DEAD" : "ALIVE";
System.out.println(threadName + " is DONE checking: " + listUrl.get(i));
sbTemp.append(livingStatus);
sbTemp.append("\n");
synchronized (res) {
res.append(sbTemp);
}
}
System.out.println(threadName + " done!");
}
public CheckUrlAliveThread(String name, List<String> listUrl, StringBuilder res, int start, int end) {
this.threadName = name;
this.listUrl = listUrl;
this.res = res;
this.start = start;
this.end = end;
}
}
class CheckUrl {
public static List<String> listOfDeadPhrases;
public static boolean isDead(String url) {
boolean isDeadHttps = isDeadHelper("https://" + url);
boolean isDeadHttp = isDeadHelper("http://" + url);
return isDeadHttps && isDeadHttp;
}
public static boolean isDeadHelper(String url) {
int htmlStatusCode = SocketConnection.htmlStatusCode(url);
if (htmlStatusCode >= 301 || htmlStatusCode < 200) {
return true;
}
String urlContent = SocketConnection.getURLSource(url);
if (urlContent.length() < 10) {
return true;
}
for (String deadPhrase : listOfDeadPhrases) {
if (urlContent.contains(deadPhrase)) {
return true;
}
}
return false;
}
}
class SocketConnection {
public static final int timeOut = 3;
public static int htmlStatusCode(String url) {
try {
URL urlObject = new URL(url);
HttpURLConnection huc = (HttpURLConnection) urlObject.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setConnectTimeout(timeOut * 1000);
huc.setReadTimeout(timeOut * 1000);
huc.setRequestMethod("HEAD");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");
huc.connect();
return huc.getResponseCode();
} catch (Exception e) {
e.printStackTrace();
return 999;
}
}
public static String getURLSource(String url) {
try {
URL urlObject = new URL(url);
HttpURLConnection huc = (HttpURLConnection) urlObject.openConnection();
HttpURLConnection.setFollowRedirects(false);
huc.setConnectTimeout(timeOut * 1000);
huc.setRequestMethod("GET");
huc.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");
huc.connect();
return toString(huc.getInputStream());
} catch (Exception e) {
return "cannot connect. Error: " + e.toString();
}
}
private static String toString(InputStream inputStream) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
String inputLine;
StringBuilder stringBuilder = new StringBuilder();
while ((inputLine = bufferedReader.readLine()) != null) {
stringBuilder.append(inputLine);
stringBuilder.append("\n");
}
replaceAll(stringBuilder, "\n", " ");
return stringBuilder.toString().toLowerCase();
} catch (Exception e) {
return "Error: " + e.toString();
}
}
private static void replaceAll(StringBuilder builder, String from, String to) {
int index = builder.indexOf(from);
while (index != -1) {
builder.replace(index, index + from.length(), to);
index += to.length(); // Move to the end of the replacement
index = builder.indexOf(from, index);
}
}
}