-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChecker.java
More file actions
126 lines (118 loc) · 4.92 KB
/
Copy pathChecker.java
File metadata and controls
126 lines (118 loc) · 4.92 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
package proxies;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Checker implements Runnable {
String IP_CHECK_SITE = "http://api.ipify.org/";
int TIMEOUT = 4000;
int threadCount = 10;
Proxies p;
public Checker(int threadCount, Proxies p){
this.threadCount = threadCount;
this.p = p;
}
public Checker(int threadCount, int timeout, Proxies p){
this.TIMEOUT = timeout;
this.threadCount = threadCount;
this.p = p;
}
@Override
public void run() {
Thread[] threads = new Thread[threadCount];
for (int c = 0; c < threadCount; c++) {
Thread t = new Thread(new Check(this));
t.start();
threads[c] = t;
}
try {
for (Thread t : threads) {
if (t != null) {
t.join();
}
}
} catch (InterruptedException ex) {
Logger.getLogger(Checker.class.getName()).log(Level.SEVERE, null, ex);
}
// while(true){
// Collection<ProxyClass> s = p.proxies.values();
// synchronized(p.proxies){
// Iterator i = s.iterator();
// Thread[] threads = new Thread[threadCount];
// int count = 0;
// while(i.hasNext()){
// ProxyClass p = (ProxyClass) i.next();
// System.out.println("Checking " + p);
// if(System.currentTimeMillis() - p.last_checked >= 300000 && !p.dead){ //can delete dead
// Check c = new Check(p);
// Thread t = new Thread(c);
// t.start();
// threads[count] = t;
// count++;
// }
// if(count == threadCount-1){
// for(Thread t : threads){
// try {
// if(t != null)
// t.join();
// } catch (InterruptedException ex) {
// Logger.getLogger(Checker.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// count = 0;
// }
// }
// }
// }
}
class Check implements Runnable {
// public Check(ProxyClass p){
// this.p = p;
// proxy = new Proxy(p.type, new InetSocketAddress(p.proxy.split(":")[0], Integer.parseInt(p.proxy.split(":")[1])));
// //System.out.println("Check " + p);
// }
Checker c;
public Check(Checker c){
this.c = c;
}
public void run() {
//System.out.println("Run Check " + p);
while(true){
ProxyClass checkProxy = c.p.getProxyCheck();
//System.out.println(Thread.currentThread() + " " + checkProxy);
checkProxy.last_checked = System.currentTimeMillis();
Proxy proxy = proxy = new Proxy(checkProxy.type, new InetSocketAddress(checkProxy.proxy.split(":")[0], Integer.parseInt(checkProxy.proxy.split(":")[1])));
try {
URL url = new URL(IP_CHECK_SITE);
long start = System.currentTimeMillis();
HttpURLConnection connect = (HttpURLConnection) url.openConnection(proxy);
connect.setReadTimeout(TIMEOUT);
connect.setConnectTimeout(TIMEOUT);
connect.setUseCaches(false);
BufferedReader br = new BufferedReader(new InputStreamReader(connect.getInputStream()));
long finish = System.currentTimeMillis();
checkProxy.last_checked = System.currentTimeMillis();
checkProxy.ping = (short) (finish - start);
String res = br.readLine();
checkProxy.exitAddress = res;
System.out.println("Worked: " + checkProxy);
return;
} catch (MalformedURLException ex) {
Logger.getLogger(Checker.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
//System.out.println(Thread.currentThread() + " " + ex);
//System.out.println("connection timeout");
}
checkProxy.dead = true;
}
}
}
}