Search in sources :

Example 1 with Socks4ProxyHandler

use of io.netty.handler.proxy.Socks4ProxyHandler in project vert.x by eclipse.

the class ProxyChannelProvider method connect.

@Override
public void connect(VertxInternal vertx, Bootstrap bootstrap, ProxyOptions options, String host, int port, Handler<Channel> channelInitializer, Handler<AsyncResult<Channel>> channelHandler) {
    final String proxyHost = options.getHost();
    final int proxyPort = options.getPort();
    final String proxyUsername = options.getUsername();
    final String proxyPassword = options.getPassword();
    final ProxyType proxyType = options.getType();
    vertx.resolveAddress(proxyHost, dnsRes -> {
        if (dnsRes.succeeded()) {
            InetAddress address = dnsRes.result();
            InetSocketAddress proxyAddr = new InetSocketAddress(address, proxyPort);
            ProxyHandler proxy;
            switch(proxyType) {
                default:
                case HTTP:
                    proxy = proxyUsername != null && proxyPassword != null ? new HttpProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new HttpProxyHandler(proxyAddr);
                    break;
                case SOCKS5:
                    proxy = proxyUsername != null && proxyPassword != null ? new Socks5ProxyHandler(proxyAddr, proxyUsername, proxyPassword) : new Socks5ProxyHandler(proxyAddr);
                    break;
                case SOCKS4:
                    proxy = proxyUsername != null ? new Socks4ProxyHandler(proxyAddr, proxyUsername) : new Socks4ProxyHandler(proxyAddr);
                    break;
            }
            bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
            InetSocketAddress targetAddress = InetSocketAddress.createUnresolved(host, port);
            bootstrap.handler(new ChannelInitializer<Channel>() {

                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    pipeline.addFirst("proxy", proxy);
                    pipeline.addLast(new ChannelInboundHandlerAdapter() {

                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            if (evt instanceof ProxyConnectionEvent) {
                                pipeline.remove(proxy);
                                pipeline.remove(this);
                                channelInitializer.handle(ch);
                                channelHandler.handle(Future.succeededFuture(ch));
                            }
                            ctx.fireUserEventTriggered(evt);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                            channelHandler.handle(Future.failedFuture(cause));
                        }
                    });
                }
            });
            ChannelFuture future = bootstrap.connect(targetAddress);
            future.addListener(res -> {
                if (!res.isSuccess()) {
                    channelHandler.handle(Future.failedFuture(res.cause()));
                }
            });
        } else {
            channelHandler.handle(Future.failedFuture(dnsRes.cause()));
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ProxyHandler(io.netty.handler.proxy.ProxyHandler) HttpProxyHandler(io.netty.handler.proxy.HttpProxyHandler) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ProxyConnectionEvent(io.netty.handler.proxy.ProxyConnectionEvent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) HttpProxyHandler(io.netty.handler.proxy.HttpProxyHandler) ProxyType(io.vertx.core.net.ProxyType) InetAddress(java.net.InetAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 2 with Socks4ProxyHandler

use of io.netty.handler.proxy.Socks4ProxyHandler in project reactor-netty by reactor.

the class ClientProxyOptions method newProxyHandler.

/**
 * Return a new eventual {@link ProxyHandler}
 *
 * @return a new eventual {@link ProxyHandler}
 */
public final ProxyHandler newProxyHandler() {
    InetSocketAddress proxyAddr = this.address.get();
    String username = this.username;
    String password = Objects.nonNull(username) && Objects.nonNull(this.password) ? this.password.apply(username) : null;
    switch(this.type) {
        case HTTP:
            return Objects.nonNull(username) && Objects.nonNull(password) ? new HttpProxyHandler(proxyAddr, username, password) : new HttpProxyHandler(proxyAddr);
        case SOCKS4:
            return Objects.nonNull(username) ? new Socks4ProxyHandler(proxyAddr, username) : new Socks4ProxyHandler(proxyAddr);
        case SOCKS5:
            return Objects.nonNull(username) && Objects.nonNull(password) ? new Socks5ProxyHandler(proxyAddr, username, password) : new Socks5ProxyHandler(proxyAddr);
    }
    throw new IllegalArgumentException("Proxy type unsupported : " + this.type);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpProxyHandler(io.netty.handler.proxy.HttpProxyHandler) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler)

Example 3 with Socks4ProxyHandler

use of io.netty.handler.proxy.Socks4ProxyHandler in project async-http-client by AsyncHttpClient.

the class ChannelManager method getBootstrap.

public Future<Bootstrap> getBootstrap(Uri uri, NameResolver<InetAddress> nameResolver, ProxyServer proxy) {
    final Promise<Bootstrap> promise = ImmediateEventExecutor.INSTANCE.newPromise();
    if (uri.isWebSocket() && proxy == null) {
        return promise.setSuccess(wsBootstrap);
    } else if (proxy != null && proxy.getProxyType().isSocks()) {
        Bootstrap socksBootstrap = httpBootstrap.clone();
        ChannelHandler httpBootstrapHandler = socksBootstrap.config().handler();
        nameResolver.resolve(proxy.getHost()).addListener((Future<InetAddress> whenProxyAddress) -> {
            if (whenProxyAddress.isSuccess()) {
                socksBootstrap.handler(new ChannelInitializer<Channel>() {

                    @Override
                    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                        httpBootstrapHandler.handlerAdded(ctx);
                        super.handlerAdded(ctx);
                    }

                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        InetSocketAddress proxyAddress = new InetSocketAddress(whenProxyAddress.get(), proxy.getPort());
                        Realm realm = proxy.getRealm();
                        String username = realm != null ? realm.getPrincipal() : null;
                        String password = realm != null ? realm.getPassword() : null;
                        ProxyHandler socksProxyHandler;
                        switch(proxy.getProxyType()) {
                            case SOCKS_V4:
                                socksProxyHandler = new Socks4ProxyHandler(proxyAddress, username);
                                break;
                            case SOCKS_V5:
                                socksProxyHandler = new Socks5ProxyHandler(proxyAddress, username, password);
                                break;
                            default:
                                throw new IllegalArgumentException("Only SOCKS4 and SOCKS5 supported at the moment.");
                        }
                        channel.pipeline().addFirst(SOCKS_HANDLER, socksProxyHandler);
                    }
                });
                promise.setSuccess(socksBootstrap);
            } else {
                promise.setFailure(whenProxyAddress.cause());
            }
        });
    } else {
        promise.setSuccess(httpBootstrap);
    }
    return promise;
}
Also used : ProxyHandler(io.netty.handler.proxy.ProxyHandler) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) Socks4ProxyHandler(io.netty.handler.proxy.Socks4ProxyHandler) Socks5ProxyHandler(io.netty.handler.proxy.Socks5ProxyHandler) Bootstrap(io.netty.bootstrap.Bootstrap) InetAddress(java.net.InetAddress)

Aggregations

Socks4ProxyHandler (io.netty.handler.proxy.Socks4ProxyHandler)3 Socks5ProxyHandler (io.netty.handler.proxy.Socks5ProxyHandler)3 InetSocketAddress (java.net.InetSocketAddress)3 HttpProxyHandler (io.netty.handler.proxy.HttpProxyHandler)2 ProxyHandler (io.netty.handler.proxy.ProxyHandler)2 InetAddress (java.net.InetAddress)2 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 ProxyConnectionEvent (io.netty.handler.proxy.ProxyConnectionEvent)1 ProxyType (io.vertx.core.net.ProxyType)1