2025-09-05 10:42:58 -04:00
|
|
|
package nu.marginalia.proxy;
|
|
|
|
|
|
|
|
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
2025-09-07 21:49:21 +00:00
|
|
|
import org.apache.hc.core5.http.io.SocketConfig;
|
|
|
|
import org.apache.hc.core5.util.Timeout;
|
2025-09-05 10:42:58 -04:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class for configuring HTTP clients with SOCKS proxy support.
|
|
|
|
*/
|
|
|
|
public class SocksProxyHttpClientFactory {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SocksProxyHttpClientFactory.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures a connection manager builder with SOCKS proxy support.
|
2025-09-07 21:49:21 +00:00
|
|
|
* If no proxy is provided, uses default socket configuration.
|
2025-09-05 10:42:58 -04:00
|
|
|
*/
|
|
|
|
public static void configureConnectionManager(PoolingHttpClientConnectionManagerBuilder builder,
|
2025-09-06 21:39:20 +00:00
|
|
|
SocksProxyConfiguration.SocksProxy proxy) {
|
2025-09-05 10:42:58 -04:00
|
|
|
if (proxy != null) {
|
|
|
|
logger.debug("Configuring HTTP client with SOCKS proxy: {}", proxy);
|
|
|
|
|
2025-09-07 21:49:21 +00:00
|
|
|
// Create SOCKS proxy address
|
|
|
|
InetSocketAddress socksProxyAddress = new InetSocketAddress(proxy.getHost(), proxy.getPort());
|
|
|
|
|
|
|
|
// Configure socket config with SOCKS proxy
|
|
|
|
SocketConfig socketConfig = SocketConfig.custom()
|
|
|
|
.setSocksProxyAddress(socksProxyAddress)
|
|
|
|
.setSoTimeout(Timeout.ofSeconds(30))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Apply the socket configuration to the connection manager
|
|
|
|
builder.setDefaultSocketConfig(socketConfig);
|
|
|
|
|
|
|
|
logger.info("SOCKS proxy configured: {}:{}", proxy.getHost(), proxy.getPort());
|
2025-09-05 10:42:58 -04:00
|
|
|
} else {
|
|
|
|
logger.debug("Configuring HTTP client without proxy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|