Ali
Ali

Reputation: 1879

Spring boot Resttemplate dynamic proxy setup connection through ssh tunnel does not recognize remote DNS

I am trying to reach a remote server via SSH tunnel to get some Rest requests. I have setup a dynamic proxy via putty to proxy traffic to the remote server. It works as expected with using browsers to access the remote services.

I have created a Resttemplate proxy to use my local dynamic proxy to connect to a remote service via SSH tunnelling. The issue I have been facing is my application cannot resolve the remote DNS to find the remote server. Hence, I get java.net.UnkonwnHostException error. I know with browser access, normally there is a property that should be set to enable proxy for DNS requests as well. I am not sure if there is such a thing for Spring Boot Resttemplate proxy setup. Please find my proxy setup code as follows.

@Configuration
public class ProxyCustomizer implements RestTemplateCustomizer {
    @Autowired
    private ProxyConfig proxyConfig;


    @Override
    public void customize(RestTemplate restTemplate) {
        if (proxyConfig == null) return;
        HttpHost proxy = new HttpHost(proxyConfig.getHostname(), proxyConfig.getPort());
        HttpClient httpClient = HttpClientBuilder.create()
                .setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {

                    @Override
                    public HttpHost determineProxy(HttpHost target,
                                                   HttpRequest request, HttpContext context)
                            throws HttpException {
                        return super.determineProxy(target, request, context);
                    }

                }).build();
        restTemplate.setRequestFactory(
                new HttpComponentsClientHttpRequestFactory(httpClient));
    }
}

I have also tried to use Socks and HTTP proxy system properties, but it didn't work:

Socks:

    System.getProperties().put( "proxySet", "true" );
    System.getProperties().put( "socksProxyHost", "127.0.0.1" );
    System.getProperties().put( "socksProxyPort", "9001" );

HTTP proxy:

    props.put("http.proxyHost", "127.0.0.1");
    props.put("http.proxyPort", "9001");
    props.put("https.proxyHost", "127.0.0.1");
    props.put("https.proxyPort", "9001");

Upvotes: 1

Views: 1599

Answers (1)

Ali
Ali

Reputation: 1879

I could not find any solution to this problem. However, adding a record to /etc/hosts file can be used as a workaround.

Upvotes: 0

Related Questions