Example usage for io.netty.handler.codec.socksx.v5 DefaultSocks5CommandRequest type

List of usage examples for io.netty.handler.codec.socksx.v5 DefaultSocks5CommandRequest type

Introduction

In this page you can find the example usage for io.netty.handler.codec.socksx.v5 DefaultSocks5CommandRequest type.

Prototype

Socks5CommandType type

To view the source code for io.netty.handler.codec.socksx.v5 DefaultSocks5CommandRequest type.

Click Source Link

Usage

From source file:com.slyak.services.proxy.handler.Socks5CommandRequestHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext requestChannelContext,
        final DefaultSocks5CommandRequest msg) throws Exception {
    if (Socks5CommandType.CONNECT.equals(msg.type())) {
        log.debug("Start to connect remote server : {}:{}", msg.dstAddr(), msg.dstPort());
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(remoteEventLoopGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override//from w ww. j av  a2  s.  co  m
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new IdleStateHandler(0, 0, 30));
                        pipeline.addLast(new IdleEventHandler());
                        pipeline.addLast(new Remote2RequestHandler(requestChannelContext.channel()));
                        pipeline.addLast(ExceptionHandler.INSTANCE);
                    }
                });
        final ChannelFuture future = bootstrap.connect(msg.dstAddr(), msg.dstPort());
        this.remoteChannel = future.channel();
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(final ChannelFuture connectFuture) throws Exception {
                if (connectFuture.isSuccess()) {
                    log.debug("Connected to remote server");
                    requestChannelContext.pipeline().addLast(new Request2RemoteHandler(remoteChannel));
                    Socks5CommandResponse response = new DefaultSocks5CommandResponse(
                            Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4);
                    //add client to dest handler to receive response
                    requestChannelContext.writeAndFlush(response);
                } else {
                    log.debug("Failed to connect to remote server");
                    Socks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(
                            Socks5CommandStatus.FAILURE, Socks5AddressType.IPv4);
                    requestChannelContext.writeAndFlush(commandResponse);
                }
            }
        });
    } else {
        log.debug("Fire channel read");
        requestChannelContext.fireChannelRead(msg);
    }
}