From 64c5045117044538c88d0c2e235d272dd18d7bfd Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 17 Dec 2024 10:54:08 +0200 Subject: [PATCH] Fix local proxy handling in REST Client module With the previous implementation, Cloudfare was blocking requests coming from the local proxy --- ...vServicesRestClientHttpProxyProcessor.java | 8 +++--- ...oxyDevServicesRestClientProxyProvider.java | 27 ++++++++++--------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/DevServicesRestClientHttpProxyProcessor.java b/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/DevServicesRestClientHttpProxyProcessor.java index 6e2eb61e3f12c..8672ee205a985 100644 --- a/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/DevServicesRestClientHttpProxyProcessor.java +++ b/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/DevServicesRestClientHttpProxyProcessor.java @@ -211,10 +211,12 @@ public void start(List restClientHttpProxyBuildIte var urlKeyName = String.format("quarkus.rest-client.\"%s\".override-uri", bi.getClassName()); var urlKeyValue = String.format("http://%s:%d", createResult.host(), createResult.port()); - if (baseUri.getPath() != null) { - if (!"/".equals(baseUri.getPath()) && !baseUri.getPath().isEmpty()) { - urlKeyValue = urlKeyValue + "/" + baseUri.getPath(); + String basePath = baseUri.getPath(); + if ((basePath != null) && !basePath.isEmpty()) { + if (basePath.startsWith("/")) { + basePath = basePath.substring(1); } + urlKeyValue = urlKeyValue + "/" + basePath; } devServicePropertiesProducer.produce( diff --git a/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/VertxHttpProxyDevServicesRestClientProxyProvider.java b/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/VertxHttpProxyDevServicesRestClientProxyProvider.java index 4965a4d35c457..73d457ce4a3ca 100644 --- a/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/VertxHttpProxyDevServicesRestClientProxyProvider.java +++ b/extensions/resteasy-reactive/rest-client/deployment/src/main/java/io/quarkus/rest/client/reactive/deployment/devservices/VertxHttpProxyDevServicesRestClientProxyProvider.java @@ -15,7 +15,6 @@ import io.quarkus.rest.client.reactive.spi.RestClientHttpProxyBuildItem; import io.quarkus.runtime.ResettableSystemProperties; import io.vertx.core.Future; -import io.vertx.core.MultiMap; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.file.FileSystemOptions; @@ -23,6 +22,7 @@ import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpServer; import io.vertx.core.metrics.MetricsOptions; +import io.vertx.core.net.HostAndPort; import io.vertx.httpproxy.HttpProxy; import io.vertx.httpproxy.ProxyContext; import io.vertx.httpproxy.ProxyInterceptor; @@ -71,16 +71,18 @@ public CreateResult create(RestClientHttpProxyBuildItem buildItem) { } HttpClient proxyClient = vertx.get().createHttpClient(clientOptions); HttpProxy proxy = HttpProxy.reverseProxy(proxyClient); - proxy.origin(determineOriginPort(baseUri), baseUri.getHost()) - .addInterceptor(new HostSettingInterceptor(baseUri.getHost())); + int targetPort = determineOriginPort(baseUri); + String targetHost = baseUri.getHost(); + proxy.origin(targetPort, targetHost) + .addInterceptor(new AuthoritySettingInterceptor(targetPort, targetHost)); HttpServer proxyServer = vertx.get().createHttpServer(); - Integer port = findRandomPort(); - proxyServer.requestHandler(proxy).listen(port); + Integer proxyPort = findRandomPort(); + proxyServer.requestHandler(proxy).listen(proxyPort); - logStartup(buildItem.getClassName(), port); + logStartup(buildItem.getClassName(), proxyPort); - return new CreateResult("localhost", port, new HttpServerClosable(proxyServer)); + return new CreateResult("localhost", proxyPort, new HttpServerClosable(proxyServer)); } protected void logStartup(String className, Integer port) { @@ -124,19 +126,18 @@ private Integer findRandomPort() { * This class sets the Host HTTP Header in order to avoid having services being blocked * for presenting a wrong value */ - private static class HostSettingInterceptor implements ProxyInterceptor { + private static class AuthoritySettingInterceptor implements ProxyInterceptor { - private final String host; + private final HostAndPort authority; - private HostSettingInterceptor(String host) { - this.host = host; + private AuthoritySettingInterceptor(int targetPort, String host) { + this.authority = HostAndPort.authority(host, targetPort); } @Override public Future handleProxyRequest(ProxyContext context) { ProxyRequest request = context.request(); - MultiMap headers = request.headers(); - headers.set("Host", host); + request.setAuthority(authority); return context.sendRequest(); }