Skip to content

Commit

Permalink
Fix local proxy handling in REST Client module
Browse files Browse the repository at this point in the history
With the previous implementation, Cloudfare was
blocking requests coming from the local proxy
  • Loading branch information
geoand committed Dec 17, 2024
1 parent 65ab924 commit 64c5045
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,12 @@ public void start(List<RestClientHttpProxyBuildItem> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
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;
import io.vertx.core.http.HttpClient;
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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ProxyResponse> handleProxyRequest(ProxyContext context) {
ProxyRequest request = context.request();
MultiMap headers = request.headers();
headers.set("Host", host);
request.setAuthority(authority);

return context.sendRequest();
}
Expand Down

0 comments on commit 64c5045

Please sign in to comment.