Search in sources :

Example 1 with MySQLPacket

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

the class MySQLClientAuthHandler method readResponseAuthPacket.

private AuthPacket readResponseAuthPacket(MycatSession mycat) {
    MySQLPacket mySQLPacket = mycat.currentProxyPayload();
    AuthPacket auth = new AuthPacket();
    auth.readPayload(mySQLPacket);
    mycat.resetCurrentProxyPayload();
    return auth;
}
Also used : AuthPacket(io.mycat.beans.mysql.packet.AuthPacket) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 2 with MySQLPacket

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

the class ResultSetHandler method requestEmptyPacket.

/**
 * loaddata empty packet
 *
 * @param nextPacketId content of file后的packetId
 */
default void requestEmptyPacket(MySQLClientSession mysql, byte nextPacketId, ResultSetCallBack<MySQLClientSession> callBack) {
    assert (mysql.currentProxyBuffer() == null);
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(4);
    request(mysql, mySQLPacket, nextPacketId, callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 3 with MySQLPacket

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

the class ResultSetHandler method request.

/**
 * COM_QUERY 命令请求报文调用此方法 head是payload的第一个字节 data根据实际报文构造 该方法自动构造请求报文,生成报文序列号以及长度,
 * 但是被限制整个报文长度不超过proxybuffer的chunk大小,大小也不应该超过mysql拆分报文大小 如需构造大的报文,可以自行替换proxbuffer即可
 */
default void request(MySQLClientSession mysql, int head, byte[] data, ResultSetCallBack<MySQLClientSession> callBack) {
    if (data == null) {
        data = EMPTY;
    }
    assert (mysql.currentProxyBuffer() == null);
    int chunkSize = mysql.getIOThread().getBufPool().chunkSize();
    if (data.length > (chunkSize - 5) || data.length > MySQLPacketSplitter.MAX_PACKET_SIZE) {
        throw new MycatException("ResultSetHandler unsupport request length more than 1024 bytes");
    }
    mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
    MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(data.length + 5);
    mySQLPacket.writeByte((byte) head);
    mySQLPacket.writeBytes(data);
    request(mysql, mySQLPacket, mysql.setPacketId(0), callBack);
}
Also used : ProxyBufferImpl(io.mycat.proxy.buffer.ProxyBufferImpl) MycatException(io.mycat.MycatException) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket)

Example 4 with MySQLPacket

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

the class MySQLClientSession method newCurrentProxyPacket.

/**
 * 把当前的proxybuffer作为报文构造
 */
public MySQLPacket newCurrentProxyPacket(int packetLength) {
    ProxyBuffer proxyBuffer = currentProxyBuffer();
    proxyBuffer.reset();
    proxyBuffer.newBuffer(packetLength);
    MySQLPacket mySQLPacket = (MySQLPacket) proxyBuffer;
    mySQLPacket.writeSkipInWriting(4);
    return mySQLPacket;
}
Also used : MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket) ProxyBuffer(io.mycat.beans.mysql.packet.ProxyBuffer)

Example 5 with MySQLPacket

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

the class MySQLClientAuthHandler method onSocketRead.

// public void setMycatSession(MycatSession mycatSession) {
// this.mycat = mycatSession;
// }
@Override
public void onSocketRead(MycatSession mycat) {
    try {
        if (mycat.getCurNIOHandler() != this) {
            return;
        }
        if (!mycat.readFromChannel()) {
            return;
        }
        mycat.setResponseFinished(ProcessState.READY);
        // MycatSecurityConfig securityManager = runtime.getSecurityManager();
        byte[] password = new byte[] {};
        if (!isChangeAuthPlugin) {
            // 密码读取与验证
            this.auth = readResponseAuthPacket(mycat);
            String authPluginName = auth.getAuthPluginName();
            int capabilities = auth.getCapabilities();
            // 切换auth_plugin
            if (MySQLServerCapabilityFlags.isPluginAuth(capabilities) && !authPluginName.equals(clientAuthPluginName)) {
                // 发送切换包的auth_response
                isChangeAuthPlugin = true;
                AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
                clientAuthPluginName = StringUtil.isEmpty(authPluginName) ? MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME : authPluginName;
                authSwitchRequestPacket.setAuthPluginName(MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME);
                authSwitchRequestPacket.setStatus((byte) 0xfe);
                authSwitchRequestPacket.setAuthPluginData(new String(seed));
                MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
                authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
                mycat.writeBytes(mySQLPayloadWriter.toByteArray(), true);
                return;
            }
            // 握手包中的加密密码
            password = auth.getPassword();
        } else {
            MySQLPacket mySQLPacket = mycat.currentProxyPayload();
            password = mySQLPacket.readEOFStringBytes();
        }
        int capabilities = auth.getCapabilities();
        if (MySQLServerCapabilityFlags.isCanUseCompressionProtocol(capabilities)) {
            String message = "Can Not Use Compression Protocol!";
            failture(mycat, MySQLErrorCode.ER_UNKNOWN_ERROR, message);
            mycat.lazyClose(true, message);
            return;
        }
        String username = auth.getUsername();
        int maxPacketSize = auth.getMaxPacketSize();
        String database = auth.getDatabase();
        int characterSet = auth.getCharacterSet();
        Map<String, String> attrs = auth.getClientConnectAttrs();
        MycatUser user = null;
        SocketAddress remoteSocketAddress = mycat.channel().socket().getRemoteSocketAddress();
        Authenticator authenticator = MetaClusterCurrent.wrapper(Authenticator.class);
        String ip = SocketAddressUtil.simplySocketAddress(remoteSocketAddress);
        Authenticator.AuthInfo authInfo = authenticator.getPassword(username, ip);
        if (!authInfo.isOk()) {
            failture(mycat, authInfo.getErrorCode(), authInfo.getException());
            return;
        } else {
            String rightPassword = authInfo.getRightPassword();
            if (rightPassword != null) {
                if (!checkPassword(rightPassword, password)) {
                    // may be bug
                    String message = "Access denied for user '" + username + "'@'" + remoteSocketAddress.toString() + "' (using password: YES)";
                    mycat.setLastMessage(message);
                    mycat.setLastErrorCode(ER_ACCESS_DENIED_ERROR);
                    LOGGER.error("login fail: {}", message);
                    mycat.writeErrorEndPacketBySyncInProcessError(ER_ACCESS_DENIED_ERROR);
                    LOGGER.error("remoteSocketAddress:{} password is wrong", remoteSocketAddress);
                    return;
                }
            }
            UserConfig userInfo = authenticator.getUserInfo(username);
            user = new MycatUser(username, null, null, ip, remoteSocketAddress, userInfo);
        }
        mycat.getDataContext().setUser(user);
        mycat.setSchema(database);
        mycat.setServerCapabilities(auth.getCapabilities());
        mycat.setIsolation(MySQLIsolation.REPEATED_READ);
        mycat.setCharset(characterSet);
        finished = true;
        mycatSessionManager.initCommandDispatcher(mycat);
        mycat.writeOkEndPacket();
    } catch (Exception e) {
        LOGGER.error("", e);
        MycatMonitor.onAuthHandlerReadException(mycat, e);
        onClear(mycat);
        failture(mycat, e);
    }
}
Also used : AuthSwitchRequestPacket(io.mycat.beans.mysql.packet.AuthSwitchRequestPacket) MycatUser(io.mycat.MycatUser) UserConfig(io.mycat.config.UserConfig) MySQLPayloadWriter(io.mycat.beans.mysql.MySQLPayloadWriter) MySQLPacket(io.mycat.beans.mysql.packet.MySQLPacket) SocketAddress(java.net.SocketAddress) Authenticator(io.mycat.Authenticator)

Aggregations

MySQLPacket (io.mycat.beans.mysql.packet.MySQLPacket)9 ProxyBufferImpl (io.mycat.proxy.buffer.ProxyBufferImpl)3 MycatException (io.mycat.MycatException)2 ProxyBuffer (io.mycat.beans.mysql.packet.ProxyBuffer)2 Authenticator (io.mycat.Authenticator)1 MycatUser (io.mycat.MycatUser)1 MySQLPayloadWriter (io.mycat.beans.mysql.MySQLPayloadWriter)1 AuthPacket (io.mycat.beans.mysql.packet.AuthPacket)1 AuthSwitchRequestPacket (io.mycat.beans.mysql.packet.AuthSwitchRequestPacket)1 ErrorPacketImpl (io.mycat.beans.mysql.packet.ErrorPacketImpl)1 UserConfig (io.mycat.config.UserConfig)1 ResultSetCallBack (io.mycat.proxy.callback.ResultSetCallBack)1 MySQLPacketResolver (io.mycat.proxy.packet.MySQLPacketResolver)1 MySQLPayloadType (io.mycat.proxy.packet.MySQLPayloadType)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1