Skip to content

Commit db36003

Browse files
improve upstream http auth
1 parent 09a817f commit db36003

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/main/java/com/testingbot/tunnel/proxy/TunnelProxyServlet.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.eclipse.jetty.client.api.Response;
1414
import org.eclipse.jetty.client.api.Result;
1515
import org.eclipse.jetty.client.util.BasicAuthentication;
16+
import org.eclipse.jetty.client.ProxyAuthenticationProtocolHandler;
17+
import org.eclipse.jetty.http.HttpHeader;
1618
import org.eclipse.jetty.proxy.AsyncProxyServlet;
1719
import org.eclipse.jetty.util.Callback;
1820
import java.net.URI;
@@ -24,6 +26,7 @@
2426
import java.util.logging.Logger;
2527

2628
public class TunnelProxyServlet extends AsyncProxyServlet {
29+
private String proxyAuthHeaderValue = null;
2730

2831
class TunnelProxyResponseListener extends ProxyResponseListener {
2932
private final HttpServletRequest request;
@@ -92,6 +95,11 @@ protected Response.Listener newProxyResponseListener(HttpServletRequest request,
9295
@Override
9396
protected void addProxyHeaders(HttpServletRequest clientRequest, Request proxyRequest) {
9497
super.addProxyHeaders(clientRequest, proxyRequest);
98+
99+
if (proxyAuthHeaderValue != null) {
100+
proxyRequest.header(HttpHeader.PROXY_AUTHORIZATION, proxyAuthHeaderValue);
101+
}
102+
95103
if (getServletContext().getAttribute("extra_headers") != null) {
96104
HashMap<String, String> headers = (HashMap<String, String>) getServletContext().getAttribute("extra_headers");
97105
for (Map.Entry<String, String> entry : headers.entrySet()) {
@@ -111,19 +119,16 @@ protected HttpClient newHttpClient() {
111119
if (proxy != null && !proxy.isEmpty()) {
112120
String[] splitted = proxy.split(":");
113121
ProxyConfiguration proxyConfig = client.getProxyConfiguration();
114-
proxyConfig.getProxies().add(new HttpProxy(splitted[0], Integer.parseInt(splitted[1])));
122+
HttpProxy httpProxy = new HttpProxy(splitted[0], Integer.parseInt(splitted[1]));
123+
proxyConfig.getProxies().add(httpProxy);
115124

116125
String proxyAuth = getServletConfig().getInitParameter("proxyAuth");
117126
if (proxyAuth != null && !proxyAuth.isEmpty()) {
118127
String[] credentials = proxyAuth.split(":");
119-
120-
auth = client.getAuthenticationStore();
121128
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.INFO, "Proxy auth {0} : {1}", new Object[]{credentials[0], credentials[1]});
122-
try {
123-
auth.addAuthentication(new BasicAuthentication(new URI("http://" + proxy), Authentication.ANY_REALM, credentials[0], credentials[1]));
124-
} catch (URISyntaxException ex) {
125-
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.SEVERE, null, ex);
126-
}
129+
130+
String userPass = credentials[0] + ":" + credentials[1];
131+
proxyAuthHeaderValue = "Basic " + java.util.Base64.getEncoder().encodeToString(userPass.getBytes());
127132
}
128133
}
129134

0 commit comments

Comments
 (0)