Search in sources :

Example 11 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generateFetchPayload.

public static byte[] generateFetchPayload(long cursorStatementId, long numOfRows) {
    try (MySQLPayloadWriter writer = new MySQLPayloadWriter(9)) {
        writer.writeByte(0x1c);
        writer.writeFixInt(4, cursorStatementId);
        writer.writeFixInt(4, numOfRows);
        return writer.toByteArray();
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter)

Example 12 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLPacketUtil method generatePrepareOk.

public static byte[] generatePrepareOk(PreparedOKPacket preparedOKPacket) {
    long statementId = preparedOKPacket.getPreparedOkStatementId();
    int columnsCount = preparedOKPacket.getPrepareOkColumnsCount();
    int warningCount = preparedOKPacket.getPreparedOkWarningCount();
    int parametersCount = preparedOKPacket.getPrepareOkParametersCount();
    try (MySQLPayloadWriter writer = new MySQLPayloadWriter(512)) {
        writer.writeByte(0);
        writer.writeFixInt(4, statementId);
        writer.writeFixInt(2, columnsCount);
        writer.writeFixInt(2, parametersCount);
        writer.writeByte(0);
        writer.writeFixInt(2, warningCount);
        return writer.toByteArray();
    }
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter)

Example 13 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class ConnectHandler method sendHandshakeResponseMessage.

private void sendHandshakeResponseMessage(String username, String password, String database, byte[] nonce, String clientPluginName, int clientCapabilitiesFlags, int collationId, Map<String, String> clientConnectionAttributes) {
    AuthPacket authPacket = new AuthPacket();
    authPacket.setCapabilities(clientCapabilitiesFlags);
    authPacket.setMaxPacketSize(PACKET_PAYLOAD_LENGTH_LIMIT);
    authPacket.setCharacterSet((byte) collationId);
    authPacket.setUsername(username);
    authPacket.setDatabase(database);
    authPacket.setClientConnectAttrs(clientConnectionAttributes);
    String authMethod = clientPluginName;
    if (password.isEmpty()) {
        authPacket.setPassword(new byte[] {});
    } else {
        byte[] authResponse;
        switch(authMethod) {
            case "mysql_native_password":
                authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
            case "caching_sha2_password":
                authResponse = CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
            case "mysql_clear_password":
                authResponse = password.getBytes(StandardCharsets.UTF_8);
                break;
            default:
                authMethod = "mysql_native_password";
                authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
                break;
        }
        authPacket.setPassword(authResponse);
    }
    authPacket.setAuthPluginName(authMethod);
    MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter();
    authPacket.writePayload(mySQLPayloadWriter);
    super.socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(++packetId, mySQLPayloadWriter.toByteArray())));
}
Also used : MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) AuthPacket(io.mycat.beans.mysql.packet.AuthPacket)

Example 14 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class VertxMySQLAuthHandler method handle.

@Override
public void handle(Buffer event) {
    buffer.appendBuffer(event);
    if (buffer.length() > 3) {
        int length = readInt(buffer, 0, 3);
        if (length == buffer.length() - 4) {
            int packetId = buffer.getUnsignedByte(3);
            Buffer payload = buffer.slice(4, buffer.length());
            ReadView readView = new ReadView(payload);
            if (!authSwitchResponse) {
                this.authPacket = new AuthPacket();
                authPacket.readPayload(readView);
                if ("mysql_native_password".equalsIgnoreCase(authPacket.getAuthPluginName()) || authPacket.getAuthPluginName() == null) {
                    auth(packetId);
                } else {
                    authSwitchResponse = true;
                    buffer = Buffer.buffer();
                    AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
                    authSwitchRequestPacket.setStatus((byte) 0xfe);
                    authSwitchRequestPacket.setAuthPluginName("mysql_native_password");
                    authSwitchRequestPacket.setAuthPluginData(new String(seedParts[2]));
                    MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
                    authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
                    socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(packetId + 1, mySQLPayloadWriter)));
                    return;
                }
            } else {
                byte[] bytes = readView.readEOFStringBytes();
                authPacket.setPassword(bytes);
                auth(packetId);
            }
        }
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AuthSwitchRequestPacket(io.mycat.beans.mysql.packet.AuthSwitchRequestPacket) MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) AuthPacket(io.mycat.beans.mysql.packet.AuthPacket)

Example 15 with MySQLPayloadWriter

use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.

the class MySQLClientAuthHandler method createHandshakePayload.

public static byte[] createHandshakePayload(long sessionId, int serverCapabilities, byte[][] seedParts) {
    HandshakePacket hs = new HandshakePacket();
    hs.setProtocolVersion(MySQLVersion.PROTOCOL_VERSION);
    hs.setServerVersion(new String(MySQLVersion.SERVER_VERSION));
    hs.setConnectionId(sessionId);
    hs.setAuthPluginDataPartOne(new String(seedParts[0]));
    hs.setCapabilities(new MySQLServerCapabilityFlags(serverCapabilities));
    hs.setHasPartTwo(true);
    hs.setCharacterSet(8);
    hs.setStatusFlags(2);
    // 有插件的话,总长度必是21, seed
    hs.setAuthPluginDataLen(21);
    hs.setAuthPluginDataPartTwo(new String(seedParts[1]));
    hs.setAuthPluginName(MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME);
    MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter();
    hs.writePayload(mySQLPayloadWriter);
    return mySQLPayloadWriter.toByteArray();
}
Also used : HandshakePacket(io.mycat.beans.mysql.packet.HandshakePacket) MySQLServerCapabilityFlags(io.mycat.config.MySQLServerCapabilityFlags) MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter)

Aggregations

MySQLPayloadWriter (io.mycat.beans.mysql.MySQLPayloadWriter)31 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 MycatMySQLRowMetaData (io.mycat.beans.mycat.MycatMySQLRowMetaData)2 AuthPacket (io.mycat.beans.mysql.packet.AuthPacket)2 AuthSwitchRequestPacket (io.mycat.beans.mysql.packet.AuthSwitchRequestPacket)2 Authenticator (io.mycat.Authenticator)1 MycatUser (io.mycat.MycatUser)1 HandshakePacket (io.mycat.beans.mysql.packet.HandshakePacket)1 MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)1 MySQLServerCapabilityFlags (io.mycat.config.MySQLServerCapabilityFlags)1 UserConfig (io.mycat.config.UserConfig)1 Buffer (io.vertx.core.buffer.Buffer)1 SocketAddress (java.net.SocketAddress)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1