2016-01-31 16 views
6

Netty istemcisindeki çorap proxy'sini yapılandırmalıyım (socks4 veya 5 proxy ile farklı siteler istemek için).Socks4/5 Proxy İşleyicileri Netty Client'ta (4.1) nasıl kullanılır?

@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); 
} 

bağlantı takılırsa, sıfırlanır veya bazı garip durumlar almaya: (http://sockslist.net/ vb www.socks-proxy.net gibi) serbest çorap listelerinden ama hiç şansım proxy'lerinin çok çalıştı. Sorun nedir? Proxy desteği 4,1'den beri Netty'ye eklendi (şimdi bir 4.1CR var, bunu denedi ve 4.1b7-8'den önce)

cevap

5

Proxy örneği, bağlantıyı yürütmek istediğinizde, boru hattındaki ilk örnek olmalıdır. Herhangi bir http içeriği ele alınmadan önce proxy.

, bunu değiştirmek p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678))); geçmek için:

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

olarak ChannelPipeline belgelerinde açıklandığı veri akışı ilk işleyicisi başlayan ve son işleyicisi sona eriyor.

+0

İlk önce sorun olur, teşekkürler! – yetanothercoder