HttpHeaderUtil { static ProxiedAddresses determineProxiedAddresses(HttpHeaders headers, List<ClientAddressSource> clientAddressSources, @Nullable ProxiedAddresses proxiedAddresses, InetSocketAddress remoteAddress, Predicate<? super InetAddress> filter) { for (final ClientAddressSource source : clientAddressSources) { if (source.isProxyProtocol()) { if (proxiedAddresses != null) { if (filter.test(proxiedAddresses.sourceAddress().getAddress()) && filter.test(proxiedAddresses.destinationAddresses().get(0).getAddress())) { return proxiedAddresses; } } } else { final ImmutableList.Builder<InetSocketAddress> builder = ImmutableList.builder(); final AsciiString name = source.header(); if (name.equals(HttpHeaderNames.FORWARDED)) { headers.getAll(HttpHeaderNames.FORWARDED).forEach(forwarded -> { getAllValidAddress(forwarded, FORWARDED_CONVERTER, filter, builder); }); } else { headers.getAll(name).forEach(header -> { getAllValidAddress(header, Function.identity(), filter, builder); }); } final ImmutableList<InetSocketAddress> addresses = builder.build(); if (!addresses.isEmpty()) { return ProxiedAddresses.of(addresses.get(0), addresses.subList(1, addresses.size())); } } } return ProxiedAddresses.of(remoteAddress); } private  HttpHeaderUtil();   }
@Test void proxiedAddresses_Forwarded() throws UnknownHostException { final ProxiedAddresses proxiedAddresses = ProxiedAddresses.of( new InetSocketAddress("10.0.0.1", 0), new InetSocketAddress("10.0.0.2", 0)); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.FORWARDED, "for=10.0.0.1,for=10.0.0.2", HttpHeaderNames.X_FORWARDED_FOR, "10.1.0.2,10.1.0.3"), ClientAddressSource.DEFAULT_SOURCES, null, remoteAddr, ACCEPT_ANY)) .isEqualTo(proxiedAddresses); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.FORWARDED, "for=10.0.0.1,for=10.0.0.2"), ClientAddressSource.DEFAULT_SOURCES, null, remoteAddr, ACCEPT_ANY)) .isEqualTo(proxiedAddresses); }
@Test void proxiedAddresses_IPv6() throws UnknownHostException { final InetSocketAddress remoteAddr = new InetSocketAddress("11.0.0.1", 0); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.FORWARDED, "for=\"[2001:db8:cafe::17]:4711\",for=[2001:db8:cafe::18]"), ClientAddressSource.DEFAULT_SOURCES, null, remoteAddr, ACCEPT_ANY)) .isEqualTo(ProxiedAddresses.of(new InetSocketAddress("2001:db8:cafe::17", 4711), new InetSocketAddress("2001:db8:cafe::18", 0))); }
@Test void proxiedAddresses_custom_header() { assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.FORWARDED, "for=10.0.0.1,for=10.0.0.2", HttpHeaderNames.X_FORWARDED_FOR, "10.1.0.1,10.1.0.2", HttpHeaderNames.of("x-real-ip"), "10.2.0.1,10.2.0.2:443"), ImmutableList.of( ofHeader("x-real-ip"), ofHeader(HttpHeaderNames.FORWARDED), ofHeader(HttpHeaderNames.X_FORWARDED_FOR)), null, remoteAddr, ACCEPT_ANY)) .isEqualTo(ProxiedAddresses.of(new InetSocketAddress("10.2.0.1", 0), new InetSocketAddress("10.2.0.2", 443))); }
@Test void proxiedAddresses_X_Forwarded_For() { final ProxiedAddresses proxiedAddresses = ProxiedAddresses.of( new InetSocketAddress("10.1.0.1", 80), new InetSocketAddress("10.1.0.2", 443)); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.X_FORWARDED_FOR, "10.1.0.1:80,10.1.0.2:443"), ClientAddressSource.DEFAULT_SOURCES, null, remoteAddr, ACCEPT_ANY)) .isEqualTo(proxiedAddresses); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(HttpHeaderNames.FORWARDED, "for=10.0.0.1,for=10.0.0.2", HttpHeaderNames.X_FORWARDED_FOR, "10.1.0.1:80,10.1.0.2:443"), ImmutableList.of(ofHeader(HttpHeaderNames.X_FORWARDED_FOR)), null, remoteAddr, ACCEPT_ANY)) .isEqualTo(proxiedAddresses); }
@Test void proxiedAddress_Proxy_Protocol() { final ProxiedAddresses proxiedAddresses = ProxiedAddresses.of( new InetSocketAddress("10.2.0.1", 50001), new InetSocketAddress("10.2.0.2", 50002)); assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(), ClientAddressSource.DEFAULT_SOURCES, proxiedAddresses, remoteAddr, ACCEPT_ANY)) .isEqualTo(proxiedAddresses); }
@Test void proxiedAddress_no_proxy() { assertThat(HttpHeaderUtil.determineProxiedAddresses( HttpHeaders.of(), ClientAddressSource.DEFAULT_SOURCES, null, remoteAddr, ACCEPT_ANY)) .isEqualTo(ProxiedAddresses.of(remoteAddr)); }