List of usage examples for io.netty.handler.codec.socksx.v4 Socks4CommandRequest userId
String userId();
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;/*from w w w . j a v a 2 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(); } }