在Netty websocket客户端的Http代理连接到互联网

时间:2017-12-04 20:39:06

标签: websocket netty http-proxy netty-socketio

我的应用程序在企业防火墙后运行,我需要使用http代理(http://theclientproxy.net:8080)连接到互联网

我使用过如下的Netty客户端, https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example/http/websocketx/client

代码:

    public final class WebSocketClient {

        static final String URL = System.getProperty("url", "wss://127.0.0.1:8080/websocket");

        public static void main(String[] args) throws Exception {
            URI uri = new URI(URL);
            String scheme = uri.getScheme() == null? "ws" : uri.getScheme();
            final String host = uri.getHost() == null? "127.0.0.1" : uri.getHost();
            final int port;
    final boolean ssl = "wss".equalsIgnoreCase(scheme);
            final SslContext sslCtx;
            if (ssl) {
                sslCtx = SslContextBuilder.forClient()
                    .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
            } else {
                sslCtx = null;
            }


            EventLoopGroup group = new NioEventLoopGroup();
            try {
        final WebSocketClientHandler handler =
                            new WebSocketClientHandler(
                                    WebSocketClientHandshakerFactory.newHandshaker(
                                            uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()));

                    Bootstrap b = new Bootstrap();
                    b.group(group)
                     .channel(NioSocketChannel.class)
                     .handler(new ChannelInitializer<SocketChannel>() {
                         @Override
                         protected void initChannel(SocketChannel ch) {
                             ChannelPipeline p = ch.pipeline();
                             if (sslCtx != null) {
                                 p.addFirst(new HttpProxyHandler(new InetSocketAddress("theclientproxy.net", 8080) ) );
                                 p.addLast(sslCtx.newHandler(ch.alloc(), host, port));
                             }
                             p.addLast(
                                     new HttpClientCodec(),
                                     new HttpObjectAggregator(8192),
                                     WebSocketClientCompressionHandler.INSTANCE,
                                     handler);
                         }
                     });

                    Channel ch = b.connect(uri.getHost(), port).sync().channel();
                    handler.handshakeFuture().sync();

BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String msg = console.readLine(); //THIS IS NULL IN DATA CENTER LOGS
                if (msg == null) {
                    break;
                } else if ("bye".equals(msg.toLowerCase())) {
                    ch.writeAndFlush(new CloseWebSocketFrame());
                    ch.closeFuture().sync();
                    break;
                } else if ("ping".equals(msg.toLowerCase())) {
                    WebSocketFrame frame = new PingWebSocketFrame(Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
                    ch.writeAndFlush(frame);
                } else {
                    WebSocketFrame frame = new TextWebSocketFrame(msg);
                    ch.writeAndFlush(frame);
                }
            }
        } finally {
            group.shutdownGracefully();
        }

处理程序:

public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {

    private final WebSocketClientHandshaker handshaker;
    private ChannelPromise handshakeFuture;

    public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
        this.handshaker = handshaker;
    }

    public ChannelFuture handshakeFuture() {
        return handshakeFuture;
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        handshakeFuture = ctx.newPromise();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        handshaker.handshake(ctx.channel());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        System.out.println("WebSocket Client disconnected!");
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        Channel ch = ctx.channel();
        if (!handshaker.isHandshakeComplete()) {
            try {
                handshaker.finishHandshake(ch, (FullHttpResponse) msg);
                System.out.println("WebSocket Client connected!");
                handshakeFuture.setSuccess();
            } catch (WebSocketHandshakeException e) {
                System.out.println("WebSocket Client failed to connect");
                handshakeFuture.setFailure(e);
            }
            return;
        }

应用程序能够成功连接到本地计算机上的websocket服务器端点。

但是在我的应用程序部署的公司数据中心,我看到msg值为null并且websocket客户端已断开连接

这是否意味着我的连接在防火墙被阻止了?如果是这种情况那么为什么声明“WebSocket Client连接!”是打印的吗?

由于

1 个答案:

答案 0 :(得分:0)

您使用的httpproxyhandler是正确的

在linux,docker等中部署时,只需删除下面提到的BufferredReader代码: Netty WebSocket Client Channel always gets inactive on Linux Server