Code:
package cf.huzpsb.proxycheck;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ProxyMatcher {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new BufferedInputStream(new FileInputStream("input.txt")));
if (!isSocks4Valid(null, -1)) {
System.out.println("AssertionError: can not retrieve valid response from example.com!");
return;
}
int total = 0, recognized = 0;
AtomicInteger portOpenButInvalid = new AtomicInteger(0);
AtomicInteger invalid = new AtomicInteger(0);
ExecutorService executor = new ThreadPoolExecutor(5, 50, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5), new ThreadPoolExecutor.CallerRunsPolicy());
while (sc.hasNextLine()) {
String line = sc.nextLine();
if (!line.contains(".")) {
continue;
}
int lastSlash = line.lastIndexOf('/');
if (lastSlash != -1) {
line = line.substring(lastSlash + 1);
}
int lastColon = line.lastIndexOf(':');
if (lastColon == -1) {
continue;
}
int port;
try {
port = Integer.parseInt(line.substring(lastColon + 1));
} catch (Exception ex) {
continue;
}
line = line.substring(0, lastColon);
total++;
if (total % 100 == 0) {
System.out.println("Total: " + total + ", Recognized: " + recognized);
}
CheckResult result = ProxyChecker.checkProxy(line);
if (result.isProxy()) {
recognized++;
} else {
String addr = line;
executor.submit(() -> {
if (isPortOpen(addr, port)) {
if (!isSocks4Valid(addr, port)) {
portOpenButInvalid.incrementAndGet();
}
} else {
invalid.incrementAndGet();
}
});
}
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
int invalidCount = invalid.get();
int portOpenButInvalidCount = portOpenButInvalid.get();
int validCount = total - recognized - invalidCount - portOpenButInvalidCount;
System.out.println("Total: " + total);
int percent = recognized * 100 / total;
System.out.println("Recognized: " + recognized + " (" + percent + "%)");
percent = invalidCount * 100 / total;
System.out.println("Port Closed: " + invalidCount + " (" + percent + "%)");
percent = portOpenButInvalidCount * 100 / total;
System.out.println("Not a Proxy: " + portOpenButInvalidCount + " (" + percent + "%)");
percent = validCount * 100 / total;
System.out.println("Bypassed: " + validCount + " (" + percent + "%)");
}
private static boolean isPortOpen(String host, int port) {
try (Socket socket = new Socket()) {
SocketAddress address = new InetSocketAddress(host, port);
socket.connect(address, 5000);
return true;
} catch (IOException e) {
return false;
}
}
private static final String GET = "GET / HTTP/1.1\n" +
"Host: example.com\n" +
"User-Agent: Mozilla/5.0 (HTTPDebugger4Java/1.2)\n\n";
private static boolean isSocks4Valid(String host, int port) {
try {
Proxy s4;
if (port == -1) {
s4 = Proxy.NO_PROXY;
} else {
s4 = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(host, port));
}
try (Socket toExampleDotCom = new Socket(s4)) {
toExampleDotCom.connect(new InetSocketAddress("example.com", 80), 5000);
OutputStream os = toExampleDotCom.getOutputStream();
os.write(GET.getBytes());
os.flush();
InputStream is = toExampleDotCom.getInputStream();
byte[] buffer = new byte[1024];
int read = is.read(buffer);
String response = new String(buffer, 0, read);
if (response.contains("HTTP/1.1 200 OK")) {
return true;
}
System.out.println(response);
}
} catch (Exception ignored) {
}
return false;
}
}
Result:
Total: 100, Recognized: 60
Total: 200, Recognized: 125
Total: 300, Recognized: 189
Total: 398
Recognized: 261 (65%)
Port Closed: 104 (26%)
Not a Proxy: 23 (5%)
Bypassed: 10 (2%)
Proxies are from:
https://api.proxyscrape.com/v3/free-proxy-list/get?request=displayproxies&protocol=socks4&proxy_format=protocolipport&format=text&timeout=20000
Valid proxy raito (tested with other tools):
27%
This indicates PaimonProxy can mitigate DDoS by cutting the availability of the proxies to merely 2%.
Test date:
2024/5/27