【问题标题】:how to use Socks4/5 Proxy Handlers in Netty Client (4.1)如何在 Netty 客户端 (4.1) 中使用 Socks4/5 代理处理程序
【发布时间】:2016-05-09 05:24:31
【问题描述】:

我需要在 Netty 客户端中配置 socks 代理(通过 socks4 或 5 个代理请求不同的站点)。 从免费袜子列表中尝试了很多代理(如 www.socks-proxy.net、http://sockslist.net/ 等),但没有成功:

@Test
public void testProxy() throws Exception {
    final String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
    final String host = "www.main.de";
    final int port = 80;

    Bootstrap b = new Bootstrap();
    b.group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();

                    p.addLast(new HttpClientCodec());
                    p.addLast(new HttpContentDecompressor());
                    p.addLast(new HttpObjectAggregator(10_485_760));
                    p.addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                            HttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
                            request.headers().set(HOST, host + ":" + port);
                            request.headers().set(USER_AGENT, ua);
                            request.headers().set(CONNECTION, CLOSE);

                            ctx.writeAndFlush(request);

                            System.out.println("!sent");
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            System.out.println("!answer");
                            if (msg instanceof FullHttpResponse) {
                                FullHttpResponse httpResp = (FullHttpResponse) msg;


                                ByteBuf content = httpResp.content();
                                String strContent = content.toString(UTF_8);
                                System.out.println("body: " + strContent);

                                finish.countDown();
                                return;
                            }

                            super.channelRead(ctx, msg);
                        }

                        @Override
                        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                            cause.printStackTrace(System.err);
                            ctx.close();
                            finish.countDown();
                        }
                    });

                    p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678)));
                }
            });

    b.connect(host, port).awaitUninterruptibly();
    System.out.println("!connected");

    finish.await(1, MINUTES);
}

连接挂起、重置或出现一些奇怪的异常。 怎么了? Netty 从 4.1 开始添加代理支持(现在有一个 4.1CR,试过了,之前是 4.1b7-8)

【问题讨论】:

    标签: java httpclient netty http-proxy socks


    【解决方案1】:

    代理实例应该是管道中的第一个,因为您希望它首先处理与代理的连接,然后再处理任何 http 内容。

    要更改此设置,请将p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678))); 更改为:

    p.addFirst(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678)));
    

    正如ChannelPipeline 的文档中所述,数据流从第一个处理程序开始,到最后一个处理程序结束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-19
      • 2014-10-07
      相关资源
      最近更新 更多