Example usage for io.netty.util.internal ConcurrentSet iterator

List of usage examples for io.netty.util.internal ConcurrentSet iterator

Introduction

In this page you can find the example usage for io.netty.util.internal ConcurrentSet iterator.

Prototype

@Override
    public Iterator<E> iterator() 

Source Link

Usage

From source file:com.mapple.forward.socks.SocksServerConnectHandlerEx.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception {
    ConcurrentSet<Channel> proxyList = ForwardLoginHandler.INSTANCE.proxyList();
    String id = null;// w ww .ja  v a2 s. c  o m
    Channel channel = null;
    if (message instanceof Socks4CommandRequest) {
        final Socks4CommandRequest request = (Socks4CommandRequest) message;
        id = request.userId();
    } else if (message instanceof Socks5CommandRequest) {
        AttributeKey<String> SOCKS5 = AttributeKey.valueOf("socks5");
        id = ctx.channel().attr(SOCKS5).get();
    }
    if (id != null) {
        //            System.out.println("id:" + id + " size:" + proxyList.size());
        //            id = "Mapple";
        if (id.equals("Mapple") && proxyList.size() > 0) {
            int pos = new Random().nextInt() % proxyList.size();
            int i = 0;

            Iterator<Channel> it = proxyList.iterator();
            while (it.hasNext()) {
                Channel ch = it.next();
                if (pos == i++) {
                    channel = ch;
                    break;
                }
            }
        } else {
            Iterator<Channel> it = proxyList.iterator();
            while (it.hasNext()) {
                Channel ch = it.next();
                ForwardLogin p = ch.attr(Session).get();
                if (p.getProvince2() != null && id.toUpperCase().equals(p.getProvince2().toUpperCase())) {
                    channel = ch;
                    break;
                }
                if (id.equals(p.getRemoteAddr())) {
                    channel = ch;
                    break;
                }
            }
        }
    }

    if (channel == null || !channel.isActive()) {
        if (message instanceof Socks4CommandRequest) {
            ctx.writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED));
            SocksServerUtils.closeOnFlush(ctx.channel());
        } else if (message instanceof Socks5CommandRequest) {
            final Socks5CommandRequest request = (Socks5CommandRequest) message;
            ctx.writeAndFlush(
                    new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType()));
            SocksServerUtils.closeOnFlush(ctx.channel());
        } else {
            ctx.close();
        }
        return;
    }
    if (message instanceof Socks4CommandRequest) {
        //            System.out.println("SocksServerConnectHandlerEx Socks4CommandRequest");
        final Socks4CommandRequest request = (Socks4CommandRequest) message;
        InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
        ForwardConnect fc = new ForwardConnect(addr.getAddress().getHostAddress(), addr.getPort(),
                Socks5AddressType.DOMAIN, request.dstAddr(), request.dstPort());
        fc.setSrcChannel(ctx.channel());
        ctx.channel().attr(AttributeKey.valueOf("socks4"));
        channel.writeAndFlush(fc);

        System.out.println("socks4:" + request.userId());
    } else if (message instanceof Socks5CommandRequest) {
        System.out.println("SocksServerConnectHandlerEx Socks5CommandRequest");
        final Socks5CommandRequest request = (Socks5CommandRequest) message;
        InetSocketAddress addr = (InetSocketAddress) ctx.channel().remoteAddress();
        ForwardConnect fc = new ForwardConnect(addr.getAddress().getHostAddress(), addr.getPort(),
                request.dstAddrType(), request.dstAddr(), request.dstPort());
        fc.setSrcChannel(ctx.channel());
        channel.writeAndFlush(fc);
    } else {
        ctx.close();
    }
}

From source file:com.mapple.http.HttpHelloWorldServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;

        if (HttpUtil.is100ContinueExpected(req)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }/*from w w  w. jav a2  s  .com*/

        QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
        List<String> close = decoder.parameters().get("close");
        int pos = -1;
        if (close != null && close.size() > 0) {
            pos = Integer.valueOf(close.get(0));
        }

        StringBuilder body = new StringBuilder();
        ConcurrentSet<Channel> proxyList = ForwardLoginHandler.INSTANCE.proxyList();
        int i = 0;
        AttributeKey<ForwardLogin> Session = AttributeKey.valueOf("Session");
        Iterator<Channel> it = proxyList.iterator();
        while (it.hasNext()) {
            Channel ch = it.next();
            ForwardLogin p = ch.attr(Session).get();
            body.append(i + 1);
            body.append("  ");
            body.append(p.getUserName());
            body.append("  ");
            body.append(
                    p.getProvince() + (p.getCity() == null ? "" : p.getCity()) + "[" + p.getProvince2() + "]");
            body.append("  ");
            body.append(p.getRemoteAddr() + ":" + p.getRemotePort());
            body.append("  ");
            body.append(p.getCarrier());
            body.append("  ");
            if (ch instanceof SocketChannel) {
                body.append("TCP");
            } else {
                body.append("UDT");
            }
            if (i++ == pos) {
                body.append("  [CLOSED]");
                ch.writeAndFlush(new ForwardForceClose());
            }
            body.append("\n");
        }
        String data = body.toString();
        if (data.isEmpty()) {
            data = "?";
        }

        //            boolean keepAlive = HttpUtil.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                Unpooled.wrappedBuffer(data.getBytes(CharsetUtil.UTF_8)));
        response.headers().set(CONTENT_TYPE, "text/plain; charset=utf-8");
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

        //            if (!keepAlive) {
        ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        //            } else {
        //                response.headers().set(CONNECTION, KEEP_ALIVE);
        //                ctx.write(response);
        //            }
    }
}