Copy package com.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.subgraph.orchid.TorClient;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MultiThreadedTorProxy {
private static final String TARGET_URL = "http://127.0.0.1:8080";
private static final int NUM_THREADS = 1;
public static void main(String[] args) {
// Initialize Tor
TorClient torClient = new TorClient();
System.out.println("Starting Tor client...");
String torProxyHost = "127.0.0.1";
int torProxyPort = 9050;
try {
torClient.start();
torClient.enableSocksListener(torProxyPort); // Default SOCKS port
} catch (Exception e) {
e.printStackTrace();
}
// Ensure Tor is fully started
while (!isSocksPortOpen(torProxyHost, torProxyPort)) {
try {
System.out.println("Waiting for Tor to start...");
Thread.sleep(1000); // Check every 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.println("Tor proxy is running on port 9050");
// Set up ExecutorService for multithreading
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(torProxyHost, torProxyPort));
Random random = new Random();
for (int i = 0; i < NUM_THREADS; i++) {
// Introduce a random delay between 5 to 15 seconds
try {
int delay = 5000 + random.nextInt(10000);
System.out.println("Thread " + i + " sleeping for " + delay + " ms");
Thread.sleep(delay);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
// Submit tasks to the executor
executor.submit(() -> {
OkHttpClient client = new OkHttpClient.Builder()
.proxy(proxy)
.build();
Request request = new Request.Builder()
.url(TARGET_URL)
.get()
// .post(RequestBody.create(
// MediaType.parse("application/json"),
// "{\"loginid\":\"yourLoginId\", \"password\":\"yourPassword\",
// \"queryId\":\"yourQueryId\"}"))
.build();
System.out.println(request);
try (Response response = client.newCall(request).execute()) {
if (response.body() != null) {
System.out.println(response.headers());
String content = response.body().string();
saveResponseToFile(content);
} else {
System.out.println("fail empty respond");
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
// Shutdown the executor
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
// Stop Tor client
torClient.stop();
}
private static void saveResponseToFile(String content) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("responses.txt", true))) {
writer.write(content);
writer.newLine();
writer.newLine(); // Separate each response with a new line
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean isSocksPortOpen(String host, int port) {
try (Socket socket = new Socket(host, port)) {
return true;
} catch (IOException e) {
return false;
}
}
}
```