private ByteBuf getEncodedTargetAddress(ByteBufAllocator allocator, boolean resolve) throws ProxyConnectException { InetSocketAddress remoteAddress = destinationAddress(); SocksAddressType remoteAddressType; String remoteHost; if (!resolve || remoteAddress.isUnresolved()) { remoteAddressType = SocksAddressType.DOMAIN; remoteHost = remoteAddress.getHostString(); } else { remoteHost = remoteAddress.getAddress().getHostAddress(); if (NetUtil.isValidIpV4Address(remoteHost)) { remoteAddressType = SocksAddressType.IPv4; } else if (NetUtil.isValidIpV6Address(remoteHost)) { remoteAddressType = SocksAddressType.IPv6; } else { throw new ProxyConnectException("unknown address type: " + StringUtil.simpleClassName(remoteHost)); } } int remotePort = remoteAddress.getPort(); SocksCmdRequest request = new SocksCmdRequest(SocksCmdType.UNKNOWN, remoteAddressType, remoteHost, remotePort); return SSocksAddressEncoder.INSTANCE.encode(allocator, request); }
public ByteBuf encode(ByteBufAllocator allocator, SocksCmdRequest msg) { if (LOG.isTraceEnabled()) { LOG.trace("encode target address"); } ByteBuf buf = allocator.directBuffer(); msg.encodeAsByteBuf(buf); buf.skipBytes(3); if (LOG.isTraceEnabled()) { byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); } return buf; }
/** * 获取远程ip地址 * @param request * @return */ private String getIpAddr(SocksCmdRequest request) { if(isProxy) { return config.get_ipAddr(); } else { return request.host(); } }
/** * 获取远程端口 * @param request * @return */ private int getPort(SocksCmdRequest request) { if(isProxy) { return config.get_port(); } else { return request.port(); } }
/** * localserver和remoteserver进行connect发送的数据 * * @param request * @param outboundChannel */ private void sendConnectRemoteMessage(SocksCmdRequest request, Channel outboundChannel) { ByteBuf buff = Unpooled.buffer(); request.encodeAsByteBuf(buff); if (buff.hasArray()) { int len = buff.readableBytes(); byte[] arr = new byte[len]; buff.getBytes(0, arr); byte[] data = remoteByte(arr); sendRemote(data, data.length, outboundChannel); } }
@Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { logger.info("localserver init"); ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { logger.info("localserver connect"); ctx.pipeline().addLast(new SocksServerConnectHandler(config)); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
@Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { ctx.pipeline().addLast(new ShadowsocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
@Override protected void encode(ChannelHandlerContext ctx, SocksCmdRequest msg, ByteBuf out) throws Exception { msg.encodeAsByteBuf(out); out.skipBytes(3); // Simply skip first 3 bytes ctx.pipeline().remove(this); }
@Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { // auth support example //ctx.pipeline().addFirst(new SocksAuthRequestDecoder()); //ctx.write(new SocksInitResponse(SocksAuthScheme.AUTH_PASSWORD)); ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: ctx.pipeline().addFirst(new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { ctx.pipeline().addLast(new SocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
@Override public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { // auth support example // ctx.pipeline().addFirst("socksAuthRequestDecoder",new SocksAuthRequestDecoder()); // ctx.write(new SocksInitResponse(SocksMessage.SocksAuthScheme.AUTH_PASSWORD)); ctx.pipeline().addFirst(SocksCmdRequestDecoder.getName(), new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: ctx.pipeline().addFirst(SocksCmdRequestDecoder.getName(), new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { ctx.pipeline().addLast(SocksServerConnectHandler.getName(), new SocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
@Override public void messageReceived(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception { switch (socksRequest.requestType()) { case INIT: { // auth support example // ctx.pipeline().addFirst("socksAuthRequestDecoder",new SocksAuthRequestDecoder()); // ctx.write(new SocksInitResponse(SocksMessage.SocksAuthScheme.AUTH_PASSWORD)); ctx.pipeline().addFirst(SocksCmdRequestDecoder.getName(), new SocksCmdRequestDecoder()); ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH)); break; } case AUTH: ctx.pipeline().addFirst(SocksCmdRequestDecoder.getName(), new SocksCmdRequestDecoder()); ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS)); break; case CMD: SocksCmdRequest req = (SocksCmdRequest) socksRequest; if (req.cmdType() == SocksCmdType.CONNECT) { ctx.pipeline().addLast(SocksServerConnectHandler.getName(), new SocksServerConnectHandler()); ctx.pipeline().remove(this); ctx.fireChannelRead(socksRequest); } else { ctx.close(); } break; case UNKNOWN: ctx.close(); break; } }
private ChannelHandlerContext prepare(final ChannelHandlerContext ctx, Object msg, ChannelFuture future) throws IOException { if (msg.getClass() == SocksCmdRequest.class) { SocksCmdRequest cmd = SocksCmdRequest.class.cast(msg); return Socks5ProxyServerHandlerBuilder.this.exchangerContext.exists(cmd.host()) ? this.activate(Socks5ProxyServerHandlerBuilder.this.exchangerContext.activate(cmd.host()), future, ctx) : this.wait(cmd, ctx); } return ctx; }
private SocksCmdResponse getSuccessResponse(SocksCmdRequest request) { return new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType()); }
private SocksCmdResponse getFailureResponse(SocksCmdRequest request) { return new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType()); }
/** * 禁止关闭接收方,cascade = false * * @param cmd * @param ctx * @return * @throws IOException */ private ChannelHandlerContext wait(SocksCmdRequest cmd, ChannelHandlerContext ctx) throws IOException { ctx.attr(Socks5ProxyServerHandlerBuilder.this.exchanger).set(Socks5ProxyServerHandlerBuilder.this.exchangerContext.wait(cmd.host(), false, new NetworkTransfer(ctx))); return ctx; }