protected void onMessage()

in sshd-core/src/main/java/org/apache/sshd/common/forward/SocksProxy.java [217:290]


        protected void onMessage(Buffer buffer) throws IOException {
            boolean debugEnabled = log.isDebugEnabled();
            if (authMethods == null) {
                int nbAuthMethods = getUByte(buffer);
                authMethods = new byte[nbAuthMethods];
                buffer.getRawBytes(authMethods);
                boolean foundNoAuth = false;
                for (int i = 0; i < nbAuthMethods; i++) {
                    foundNoAuth |= authMethods[i] == 0;
                }
                buffer = new ByteArrayBuffer(Byte.SIZE, false);
                buffer.putByte((byte) 0x05);
                buffer.putByte((byte) (foundNoAuth ? 0x00 : 0xFF));
                session.writeBuffer(buffer);
                if (!foundNoAuth) {
                    throw new IllegalStateException("Received socks5 greeting without NoAuth method");
                } else if (debugEnabled) {
                    log.debug("Received socks5 greeting");
                }
            } else if (channel == null) {
                response = buffer;
                int version = getUByte(buffer);
                if (version != 0x05) {
                    throw new IllegalStateException("Unexpected version: " + version);
                }
                int cmd = buffer.getUByte();
                if (cmd != 1) { // establish a TCP/IP stream connection
                    throw new IllegalStateException("Unsupported socks command: " + cmd);
                }
                int res = buffer.getUByte();
                if (res != 0) {
                    if (debugEnabled) {
                        log.debug("No zero reserved value: {}", res);
                    }
                }

                int type = buffer.getUByte();
                String host;
                if (type == 0x01) {
                    host = Integer.toString(getUByte(buffer)) + "."
                           + Integer.toString(getUByte(buffer)) + "."
                           + Integer.toString(getUByte(buffer)) + "."
                           + Integer.toString(getUByte(buffer));
                } else if (type == 0x03) {
                    host = getBLString(buffer);
                } else if (type == 0x04) {
                    host = Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer)) + ":"
                           + Integer.toHexString(getUShort(buffer));
                } else {
                    throw new IllegalStateException("Unsupported address type: " + type);
                }
                int port = getUShort(buffer);
                if (debugEnabled) {
                    log.debug("Received socks5 connection request to {}:{}", host, port);
                }
                SshdSocketAddress remote = new SshdSocketAddress(host, port);
                channel = new TcpipClientChannel(TcpipClientChannel.Type.Direct, session, remote);
                channel.setStreaming(Streaming.Async);
                session.suspendRead();
                service.registerChannel(channel);
                channel.open().addListener(this::onChannelOpened);
            } else {
                if (debugEnabled) {
                    log.debug("Received socks5 connection message");
                }
                super.onMessage(buffer);
            }
        }