Search in sources :

Example 1 with ProxyServer

use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.

the class ProxyTunnellingTest method runTest.

private void runTest(boolean secure) throws Exception {
    setUpServers(secure);
    String targetUrl = String.format("%s://localhost:%d/", secure ? "wss" : "ws", port2);
    // CONNECT happens over HTTP, not HTTPS
    ProxyServer ps = proxyServer("localhost", port1).build();
    try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setProxyServer(ps).setUseInsecureTrustManager(true))) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<String> text = new AtomicReference<>("");
        WebSocket websocket = asyncHttpClient.prepareGet(targetUrl).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

            @Override
            public void onMessage(String message) {
                text.set(message);
                latch.countDown();
            }

            @Override
            public void onOpen(WebSocket websocket) {
            }

            @Override
            public void onClose(WebSocket websocket) {
                latch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                t.printStackTrace();
                latch.countDown();
            }
        }).build()).get();
        websocket.sendMessage("ECHO");
        latch.await();
        assertEquals(text.get(), "ECHO");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ProxyServer(org.asynchttpclient.proxy.ProxyServer) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Example 2 with ProxyServer

use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.

the class Interceptors method exitAfterIntercept.

public //
boolean exitAfterIntercept(//
Channel channel, //
NettyResponseFuture<?> future, //
AsyncHandler<?> handler, //
HttpResponse response, //
HttpResponseStatus status, HttpResponseHeaders responseHeaders) throws Exception {
    HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
    ProxyServer proxyServer = future.getProxyServer();
    int statusCode = response.status().code();
    Request request = future.getCurrentRequest();
    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    if (hasResponseFilters && responseFiltersInterceptor.exitAfterProcessingFilters(channel, future, handler, status, responseHeaders)) {
        return true;
    }
    if (statusCode == UNAUTHORIZED_401) {
        return unauthorized401Interceptor.exitAfterHandling401(channel, future, response, request, statusCode, realm, proxyServer, httpRequest);
    } else if (statusCode == PROXY_AUTHENTICATION_REQUIRED_407) {
        return proxyUnauthorized407Interceptor.exitAfterHandling407(channel, future, response, request, statusCode, proxyServer, httpRequest);
    } else if (statusCode == CONTINUE_100) {
        return continue100Interceptor.exitAfterHandling100(channel, future, statusCode);
    } else if (Redirect30xInterceptor.REDIRECT_STATUSES.contains(statusCode)) {
        return redirect30xInterceptor.exitAfterHandlingRedirect(channel, future, response, request, statusCode, realm);
    } else if (httpRequest.method() == HttpMethod.CONNECT && statusCode == OK_200) {
        return connectSuccessInterceptor.exitAfterHandlingConnect(channel, future, request, proxyServer, statusCode, httpRequest);
    }
    return false;
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) Request(org.asynchttpclient.Request) Realm(org.asynchttpclient.Realm) ProxyServer(org.asynchttpclient.proxy.ProxyServer)

Example 3 with ProxyServer

use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.

the class ProxyUtils method createProxyServerSelector.

/**
     * Creates a proxy server instance from the given properties.
     * Currently the default http.* proxy properties are supported as well as properties specific for AHC.
     *
     * @param properties the properties to evaluate. Must not be null.
     * @return a ProxyServer instance or null, if no valid properties were set.
     * @see <a href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">Networking Properties</a>
     * @see #PROXY_HOST
     * @see #PROXY_PORT
     * @see #PROXY_NONPROXYHOSTS
     */
public static ProxyServerSelector createProxyServerSelector(Properties properties) {
    String host = properties.getProperty(PROXY_HOST);
    if (host != null) {
        int port = Integer.valueOf(properties.getProperty(PROXY_PORT, "80"));
        String principal = properties.getProperty(PROXY_USER);
        String password = properties.getProperty(PROXY_PASSWORD);
        Realm realm = null;
        if (principal != null) {
            realm = basicAuthRealm(principal, password).build();
        }
        ProxyServer.Builder proxyServer = proxyServer(host, port).setRealm(realm);
        String nonProxyHosts = properties.getProperty(PROXY_NONPROXYHOSTS);
        if (nonProxyHosts != null) {
            proxyServer.setNonProxyHosts(new ArrayList<>(Arrays.asList(nonProxyHosts.split("\\|"))));
        }
        ProxyServer proxy = proxyServer.build();
        return uri -> proxy;
    }
    return ProxyServerSelector.NO_PROXY_SELECTOR;
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) Logger(org.slf4j.Logger) Realm(org.asynchttpclient.Realm) ProxyServerSelector(org.asynchttpclient.proxy.ProxyServerSelector) Request(org.asynchttpclient.Request) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) InetSocketAddress(java.net.InetSocketAddress) ProxyServer(org.asynchttpclient.proxy.ProxyServer) ArrayList(java.util.ArrayList) ProxySelector(java.net.ProxySelector) List(java.util.List) Dsl(org.asynchttpclient.Dsl) AsyncHttpClientConfig(org.asynchttpclient.AsyncHttpClientConfig) Proxy(java.net.Proxy) Uri(org.asynchttpclient.uri.Uri) URI(java.net.URI) Realm(org.asynchttpclient.Realm) ProxyServer(org.asynchttpclient.proxy.ProxyServer)

Example 4 with ProxyServer

use of org.asynchttpclient.proxy.ProxyServer in project async-http-client by AsyncHttpClient.

the class NettyRequestSender method sendRequest.

public <//
T> //
ListenableFuture<T> sendRequest(//
final Request request, //
final AsyncHandler<T> asyncHandler, //
NettyResponseFuture<T> future, boolean performingNextRequest) {
    if (isClosed())
        throw new IllegalStateException("Closed");
    validateWebSocketRequest(request, asyncHandler);
    ProxyServer proxyServer = getProxyServer(config, request);
    // websockets use connect tunnelling to work with proxies
    if (proxyServer != null && (request.getUri().isSecured() || request.getUri().isWebSocket()) && !isConnectDone(request, future))
        if (future != null && future.isConnectAllowed())
            // SSL proxy or websocket: CONNECT for sure
            return sendRequestWithCertainForceConnect(request, asyncHandler, future, performingNextRequest, proxyServer, true);
        else
            // CONNECT will depend if we can pool or connection or if we have to open a new one
            return sendRequestThroughSslProxy(request, asyncHandler, future, performingNextRequest, proxyServer);
    else
        // no CONNECT for sure
        return sendRequestWithCertainForceConnect(request, asyncHandler, future, performingNextRequest, proxyServer, false);
}
Also used : ProxyServer(org.asynchttpclient.proxy.ProxyServer) ProxyUtils.getProxyServer(org.asynchttpclient.util.ProxyUtils.getProxyServer)

Aggregations

ProxyServer (org.asynchttpclient.proxy.ProxyServer)4 Realm (org.asynchttpclient.Realm)2 Request (org.asynchttpclient.Request)2 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 InetSocketAddress (java.net.InetSocketAddress)1 Proxy (java.net.Proxy)1 ProxySelector (java.net.ProxySelector)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)1 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)1 Dsl (org.asynchttpclient.Dsl)1 ProxyServerSelector (org.asynchttpclient.proxy.ProxyServerSelector)1 Uri (org.asynchttpclient.uri.Uri)1