public RouteStates( Stream<ProxyRouteConfig> proxyRouteConfigs, ApplicationState applicationState, ProxyClientFactory clientFactory) { List<ProxyRouteState> proxyRouteStates = proxyRouteConfigs .map( (ProxyRouteConfig config) -> ProxyRouteState.create( applicationState, config, new PersistentProxyHandler( clientFactory, config, new SocketAddressHelper()))) .collect(Collectors.toList()); LinkedHashMap<String, ProxyRouteState> routeMap = new LinkedHashMap<>(); proxyRouteStates.forEach((ProxyRouteState state) -> routeMap.put(state.path(), state)); this.routeMap = new AtomicReference<>(ImmutableMap.copyOf(routeMap)); }
protected String getOriginatingAddressAndPort(ChannelHandlerContext ctx, Request request) { val rawXFF = request.headers().get(X_FORWARDED_FOR); if (rawXFF == null || rawXFF.toString().trim().isEmpty()) { return addressHelper.extractRemoteAddressAndPort(ctx.channel()); } else { String stringXFF = rawXFF.toString(); if (stringXFF.contains(",")) { // extract originating address from list of addresses return stringXFF.substring(0, stringXFF.indexOf(",")); } else { // XFF only has one address return stringXFF; } } }
@Nullable public String extractRemoteAddressAndPort(Channel channel) { if (channel instanceof ServerSocketChannel) { val inetSocketAddress = ((ServerSocketChannel) channel).remoteAddress(); return computeAddressAndPort(inetSocketAddress); } else if (channel instanceof SocketChannel) { val inetSocketAddress = ((SocketChannel) channel).remoteAddress(); return computeAddressAndPort(inetSocketAddress); } else { return null; } }
private void appendXForwardedFor(ChannelHandlerContext ctx, Request request) { val remoteAddressAndPort = addressHelper.extractRemoteAddressAndPort(ctx.channel()); if (remoteAddressAndPort != null) { val rawXFF = request.headers().get(X_FORWARDED_FOR); if (rawXFF == null || rawXFF.toString().trim().isEmpty()) { request.headers().set(X_FORWARDED_FOR, remoteAddressAndPort); } else { val newXFF = rawXFF.toString().trim() + ", " + remoteAddressAndPort; request.headers().set(X_FORWARDED_FOR, newXFF); } } }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); subject = new SocketAddressHelper(); }
@Test public void testValidAddressSocketChannel() throws Exception { val testPort = 2; val testAddress = "192.168.0.1"; val inetSocketAddress = new InetSocketAddress(testAddress, testPort); val expectedResult = testAddress + ":" + String.valueOf(testPort); when(socketChannel.remoteAddress()).thenReturn(inetSocketAddress); val result = subject.extractRemoteAddressAndPort(socketChannel); assertTrue(result.equals(expectedResult)); } }
@Override public ChannelHandler getApplicationRouter() { return new PipelineRouter( ImmutableMap.of( "none", ProxyRouteState.create( appState, proxyConfig, new ProxyHandler( factory, proxyConfig, new SocketAddressHelper())))); } });
@Test public void testNonSocketOrSocketServerChannel() throws Exception { val result = subject.extractRemoteAddressAndPort(channel); assertTrue(result == null); }
EdgeProxyState(EdgeProxyConfig config) { super(config); clientFactory = new ProxyClientFactory(this); routeStates = new RouteStates<ProxyRouteState>( // create an ImmutableMap from ... ImmutableMap.copyOf( config .routeConfigs // iterate over a stream of ProxyRouteConfig .stream() // for each ProxyRouteConfig create a ProxyRouteState .map( (ProxyRouteConfig prConfig) -> ProxyRouteState.create( this, prConfig, new ProxyHandler( clientFactory, prConfig, new SocketAddressHelper()))) // collect the stream of ProxyRouteState into // LinkedHashMap<String, ProxyRouteState> where the // route path is the key and // ProxyRouteState is the value .collect(toLinkedMap(RouteState::path, state -> state)))); }
@Test public void testValidAddressSocketServerChannel() throws Exception { val testPort = 123; val testAddress = "192.168.0.1"; val inetSocketAddress = new InetSocketAddress(testAddress, testPort); val expectedResult = testAddress + ":" + String.valueOf(testPort); when(serverSocketChannel.remoteAddress()).thenReturn(inetSocketAddress); val result = subject.extractRemoteAddressAndPort(serverSocketChannel); assertTrue(result.equals(expectedResult)); }
@Test public void testWithoutInetSocketAddressSocketServerChannel() throws Exception { when(serverSocketChannel.remoteAddress()).thenReturn(null); val result = subject.extractRemoteAddressAndPort(serverSocketChannel); assertTrue(result == null); }
@Test public void testInetSocketAddressSocketChannel() throws Exception { when(socketChannel.remoteAddress()).thenReturn(null); val result = subject.extractRemoteAddressAndPort(socketChannel); assertTrue(result == null); }