@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { try { switch (state()) { case INIT: { final byte version = in.readByte(); if (version != SocksVersion.SOCKS5.byteValue()) { throw new DecoderException( "unsupported version: " + version + " (expected: " + SocksVersion.SOCKS5.byteValue() + ')'); } final Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(in.readByte()); out.add(new DefaultSocks5InitialResponse(authMethod)); checkpoint(State.SUCCESS); } case SUCCESS: { int readableBytes = actualReadableBytes(); if (readableBytes > 0) { out.add(in.readRetainedSlice(readableBytes)); } break; } case FAILURE: { in.skipBytes(actualReadableBytes()); break; } } } catch (Exception e) { fail(out, e); } }
public static Socks5AuthMethod valueOf(byte b) { switch (b) { case 0x00: return NO_AUTH; case 0x01: return GSSAPI; case 0x02: return PASSWORD; case (byte) 0xFF: return UNACCEPTED; } return new Socks5AuthMethod(b); }
private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); final List<Socks5AuthMethod> authMethods = msg.authMethods(); final int numAuthMethods = authMethods.size(); out.writeByte(numAuthMethods); if (authMethods instanceof RandomAccess) { for (int i = 0; i < numAuthMethods; i ++) { out.writeByte(authMethods.get(i).byteValue()); } } else { for (Socks5AuthMethod a: authMethods) { out.writeByte(a.byteValue()); } } }
public static boolean isSocks5(ByteBuf msg, int actualReadableBytes) { // first byte has to be 5 if (SocksVersion.valueOf(msg.getByte(msg.readerIndex())) != SocksVersion.SOCKS5) { return false; } // then the amount of authentication methods byte numberOfAuthenticationMethods = msg.getByte(msg.readerIndex() + 1); // now the authentication methods for (int i = 0; i < numberOfAuthenticationMethods; i++) { Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(msg.getByte(msg.readerIndex() + 2 + i)); if (!(NO_AUTH.equals(authMethod) || PASSWORD.equals(authMethod) || GSSAPI.equals(authMethod))) { return false; } } // more methods than advertised, either broken request or not actually SOCKS5 // do this check last so that any waiting for data is already done return actualReadableBytes == (2 + numberOfAuthenticationMethods); } }
private void answerInitialRequest(ChannelHandlerContext ctx, Socks5InitialRequest initialRequest, Socks5AuthMethod requiredAuthMethod, ChannelHandler nextRequestDecoder) { ctx.pipeline().remove(Socks5InitialRequestDecoder.class); for (Socks5AuthMethod authMethod : initialRequest.authMethods()) { if (requiredAuthMethod.equals(authMethod)) { ctx.pipeline().addFirst(nextRequestDecoder); ctx.write(new DefaultSocks5InitialResponse(requiredAuthMethod)); return; } } ctx.write(new DefaultSocks5InitialResponse(Socks5AuthMethod.UNACCEPTED)); }
public static boolean isSocks5(ByteBuf msg, int actualReadableBytes) { // first byte has to be 5 if (SocksVersion.valueOf(msg.getByte(msg.readerIndex())) != SocksVersion.SOCKS5) { return false; } // then the amount of authentication methods byte numberOfAuthenticationMethods = msg.getByte(msg.readerIndex() + 1); // now the authentication methods for (int i = 0; i < numberOfAuthenticationMethods; i++) { Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(msg.getByte(msg.readerIndex() + 2 + i)); if (!(NO_AUTH.equals(authMethod) || PASSWORD.equals(authMethod) || GSSAPI.equals(authMethod))) { return false; } } // more methods than advertised, either broken request or not actually SOCKS5 // do this check last so that any waiting for data is already done return actualReadableBytes == (2 + numberOfAuthenticationMethods); } }
private void answerInitialRequest(ChannelHandlerContext ctx, Socks5InitialRequest initialRequest, Socks5AuthMethod requiredAuthMethod, ChannelHandler nextRequestDecoder) { ctx.pipeline().remove(Socks5InitialRequestDecoder.class); for (Socks5AuthMethod authMethod : initialRequest.authMethods()) { if (requiredAuthMethod.equals(authMethod)) { ctx.pipeline().addFirst(nextRequestDecoder); ctx.write(new DefaultSocks5InitialResponse(requiredAuthMethod)); return; } } ctx.write(new DefaultSocks5InitialResponse(Socks5AuthMethod.UNACCEPTED)); }
authMethods[i] = Socks5AuthMethod.valueOf(in.readByte());
private static void encodeAuthMethodResponse(Socks5InitialResponse msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); out.writeByte(msg.authMethod().byteValue()); }
public static Socks5AuthMethod valueOf(byte b) { switch (b) { case 0x00: return NO_AUTH; case 0x01: return GSSAPI; case 0x02: return PASSWORD; case (byte) 0xFF: return UNACCEPTED; } return new Socks5AuthMethod(b); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { try { switch (state()) { case INIT: { final byte version = in.readByte(); if (version != SocksVersion.SOCKS5.byteValue()) { throw new DecoderException( "unsupported version: " + version + " (expected: " + SocksVersion.SOCKS5.byteValue() + ')'); } final Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(in.readByte()); out.add(new DefaultSocks5InitialResponse(authMethod)); checkpoint(State.SUCCESS); } case SUCCESS: { int readableBytes = actualReadableBytes(); if (readableBytes > 0) { out.add(in.readRetainedSlice(readableBytes)); } break; } case FAILURE: { in.skipBytes(actualReadableBytes()); break; } } } catch (Exception e) { fail(out, e); } }
private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); final List<Socks5AuthMethod> authMethods = msg.authMethods(); final int numAuthMethods = authMethods.size(); out.writeByte(numAuthMethods); if (authMethods instanceof RandomAccess) { for (int i = 0; i < numAuthMethods; i ++) { out.writeByte(authMethods.get(i).byteValue()); } } else { for (Socks5AuthMethod a: authMethods) { out.writeByte(a.byteValue()); } } }
public static Socks5AuthMethod valueOf(byte b) { switch (b) { case 0x00: return NO_AUTH; case 0x01: return GSSAPI; case 0x02: return PASSWORD; case (byte) 0xFF: return UNACCEPTED; } return new Socks5AuthMethod(b); }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { try { switch (state()) { case INIT: { final byte version = in.readByte(); if (version != SocksVersion.SOCKS5.byteValue()) { throw new DecoderException( "unsupported version: " + version + " (expected: " + SocksVersion.SOCKS5.byteValue() + ')'); } final Socks5AuthMethod authMethod = Socks5AuthMethod.valueOf(in.readByte()); out.add(new DefaultSocks5InitialResponse(authMethod)); checkpoint(State.SUCCESS); } case SUCCESS: { int readableBytes = actualReadableBytes(); if (readableBytes > 0) { out.add(in.readRetainedSlice(readableBytes)); } break; } case FAILURE: { in.skipBytes(actualReadableBytes()); break; } } } catch (Exception e) { fail(out, e); } }
private static void encodeAuthMethodRequest(Socks5InitialRequest msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); final List<Socks5AuthMethod> authMethods = msg.authMethods(); final int numAuthMethods = authMethods.size(); out.writeByte(numAuthMethods); if (authMethods instanceof RandomAccess) { for (int i = 0; i < numAuthMethods; i ++) { out.writeByte(authMethods.get(i).byteValue()); } } else { for (Socks5AuthMethod a: authMethods) { out.writeByte(a.byteValue()); } } }
authMethods[i] = Socks5AuthMethod.valueOf(in.readByte());
private static void encodeAuthMethodResponse(Socks5InitialResponse msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); out.writeByte(msg.authMethod().byteValue()); }
authMethods[i] = Socks5AuthMethod.valueOf(in.readByte());
private static void encodeAuthMethodResponse(Socks5InitialResponse msg, ByteBuf out) { out.writeByte(msg.version().byteValue()); out.writeByte(msg.authMethod().byteValue()); }