1313import org .eclipse .jetty .client .api .Response ;
1414import org .eclipse .jetty .client .api .Result ;
1515import org .eclipse .jetty .client .util .BasicAuthentication ;
16+ import org .eclipse .jetty .client .ProxyAuthenticationProtocolHandler ;
17+ import org .eclipse .jetty .http .HttpHeader ;
1618import org .eclipse .jetty .proxy .AsyncProxyServlet ;
1719import org .eclipse .jetty .util .Callback ;
1820import java .net .URI ;
2426import java .util .logging .Logger ;
2527
2628public 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