io.netty.handler.codec.socksx.SocksVersion Java Examples

The following examples show how to use io.netty.handler.codec.socksx.SocksVersion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Socks5CommandResponseDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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 Socks5CommandStatus status = Socks5CommandStatus.valueOf(in.readByte());
            in.skipBytes(1); // Reserved
            final Socks5AddressType addrType = Socks5AddressType.valueOf(in.readByte());
            final String addr = addressDecoder.decodeAddress(addrType, in);
            final int port = in.readUnsignedShort();

            out.add(new DefaultSocks5CommandResponse(status, addrType, addr, port));
            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);
    }
}
 
Example #2
Source File: Socks5InitialRequestDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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 int authMethodCnt = in.readUnsignedByte();
            if (actualReadableBytes() < authMethodCnt) {
                break;
            }

            final Socks5AuthMethod[] authMethods = new Socks5AuthMethod[authMethodCnt];
            for (int i = 0; i < authMethodCnt; i++) {
                authMethods[i] = Socks5AuthMethod.valueOf(in.readByte());
            }

            out.add(new DefaultSocks5InitialRequest(authMethods));
            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);
    }
}
 
Example #3
Source File: Socks5CommandRequestDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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 Socks5CommandType type = Socks5CommandType.valueOf(in.readByte());
            in.skipBytes(1); // RSV
            final Socks5AddressType dstAddrType = Socks5AddressType.valueOf(in.readByte());
            final String dstAddr = addressDecoder.decodeAddress(dstAddrType, in);
            final int dstPort = in.readUnsignedShort();

            out.add(new DefaultSocks5CommandRequest(type, dstAddrType, dstAddr, dstPort));
            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);
    }
}
 
Example #4
Source File: Socks5InitialResponseDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: AbstractSocks4Message.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public final SocksVersion version() {
    return SocksVersion.SOCKS4a;
}
 
Example #6
Source File: Socks4ServerDecoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    try {
        switch (state()) {
        case START: {
            final int version = in.readUnsignedByte();
            if (version != SocksVersion.SOCKS4a.byteValue()) {
                throw new DecoderException("unsupported protocol version: " + version);
            }

            type = Socks4CommandType.valueOf(in.readByte());
            dstPort = in.readUnsignedShort();
            dstAddr = NetUtil.intToIpAddress(in.readInt());
            checkpoint(State.READ_USERID);
        }
        case READ_USERID: {
            userId = readString("userid", in);
            checkpoint(State.READ_DOMAIN);
        }
        case READ_DOMAIN: {
            // Check for Socks4a protocol marker 0.0.0.x
            if (!"0.0.0.0".equals(dstAddr) && dstAddr.startsWith("0.0.0.")) {
                dstAddr = readString("dstAddr", in);
            }
            out.add(new DefaultSocks4CommandRequest(type, dstAddr, dstPort, userId));
            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);
    }
}
 
Example #7
Source File: AbstractSocks5Message.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public final SocksVersion version() {
    return SocksVersion.SOCKS5;
}