Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/main/java/com/glean/proxy/ChainedProxyConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.glean.proxy;

import java.net.InetSocketAddress;
import java.util.Queue;
import java.util.Set;
import org.littleshoot.proxy.ChainedProxy;
import org.littleshoot.proxy.ChainedProxyAdapter;
import org.littleshoot.proxy.impl.ClientDetails;
import java.util.logging.Logger;
import org.littleshoot.proxy.ChainedProxyManager;
import io.netty.handler.codec.http.HttpRequest;


public class ChainedProxyConfiguration {
private static final Logger logger = Logger.getLogger(ChainedProxyConfiguration.class.getName());

public static ChainedProxyManager fromEnvironment() {
if (System.getenv("FORWARD_PROXY_HOST") == null || System.getenv("FORWARD_PROXY_PORT") == null || System.getenv("FORWARD_PROXY_DATA_SOURCE_HOSTS") == null) {
return (HttpRequest httpRequest,
Queue<ChainedProxy> chainedProxies,
ClientDetails clientDetails) -> {
chainedProxies.add(ChainedProxyAdapter.FALLBACK_TO_DIRECT_CONNECTION);
};
}
InetSocketAddress forwardProxy = new InetSocketAddress(System.getenv("FORWARD_PROXY_HOST"), Integer.parseInt(System.getenv("FORWARD_PROXY_PORT")));
Set<String> dataSourceHosts = Set.of(System.getenv("FORWARD_PROXY_DATA_SOURCE_HOSTS").split(","));

ChainedProxyManager chainedProxyManager = (HttpRequest httpRequest,
Queue<ChainedProxy> chainedProxies,
ClientDetails clientDetails) -> {
String hostHeader = httpRequest.headers().get("Host");
if (hostHeader == null) {
chainedProxies.add(ChainedProxyAdapter.FALLBACK_TO_DIRECT_CONNECTION);
logger.severe("No host header found, falling back to direct connection");
return;
}
String host = hostHeader.split(":")[0];

if (dataSourceHosts.contains(host)) {
chainedProxies.add(new ChainedProxyAdapter() {
@Override
public InetSocketAddress getChainedProxyAddress() {
return forwardProxy;
}

@Override
public String getUsername() {
return System.getenv("FORWARD_PROXY_USERNAME");
}

@Override
public String getPassword() {
return System.getenv("FORWARD_PROXY_PASSWORD");
}
});
} else {
chainedProxies.add(ChainedProxyAdapter.FALLBACK_TO_DIRECT_CONNECTION);
}
};
return chainedProxyManager;
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/glean/proxy/ProxyNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.InetSocketAddress;
import java.util.logging.Logger;
import org.littleshoot.proxy.ChainedProxyManager;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;
import org.littleshoot.proxy.impl.ThreadPoolConfiguration;
Expand All @@ -11,6 +12,7 @@ public class ProxyNetworking {

protected final ThreadPoolConfiguration threadPoolConfiguration;
protected final FilterConfiguration filterConfiguration;
protected final ChainedProxyManager chainedProxyManager;

public void run(int port) {
DynamicHttpFiltersSourceAdapter filtersSource =
Expand All @@ -23,6 +25,7 @@ public void run(int port) {
.withIdleConnectionTimeout(1200) // seconds
.withThreadPoolConfiguration(threadPoolConfiguration)
.withFiltersSource(filtersSource)
.withChainProxyManager(chainedProxyManager)
.start();
try {
Thread.sleep(Long.MAX_VALUE);
Expand All @@ -35,6 +38,7 @@ public void run(int port) {
private ProxyNetworking(Builder builder) {
threadPoolConfiguration = builder.threadPoolConfiguration;
filterConfiguration = builder.filterConfiguration;
chainedProxyManager = builder.chainedProxyManager;
}

public static Builder builder() {
Expand All @@ -44,7 +48,8 @@ public static Builder builder() {
public static class Builder {
private ThreadPoolConfiguration threadPoolConfiguration;
private FilterConfiguration filterConfiguration;

private ChainedProxyManager chainedProxyManager;

public Builder withThreadPoolConfiguration(ThreadPoolConfiguration threadPoolConfiguration) {
this.threadPoolConfiguration = threadPoolConfiguration;
return this;
Expand All @@ -55,13 +60,21 @@ public Builder withFilterConfiguration(FilterConfiguration filterConfiguration)
return this;
}

public Builder withChainedProxyManager(ChainedProxyManager chainedProxyManager) {
this.chainedProxyManager = chainedProxyManager;
return this;
}

public ProxyNetworking build() {
if (threadPoolConfiguration == null) {
threadPoolConfiguration = createThreadPoolConfigurationFromEnvironment();
}
if (filterConfiguration == null) {
filterConfiguration = FilterConfiguration.fromEnvironment();
}
if (chainedProxyManager == null) {
chainedProxyManager = ChainedProxyConfiguration.fromEnvironment();
}
return new ProxyNetworking(this);
}
}
Expand Down