Search in sources :

Example 1 with HandshakePacket

use of com.alibaba.cobar.net.mysql.HandshakePacket in project cobar by alibaba.

the class FrontendConnection method register.

@Override
public void register(Selector selector) throws IOException {
    super.register(selector);
    if (!isClosed.get()) {
        // 生成认证数据
        byte[] rand1 = RandomUtil.randomBytes(8);
        byte[] rand2 = RandomUtil.randomBytes(12);
        // 保存认证数据
        byte[] seed = new byte[rand1.length + rand2.length];
        System.arraycopy(rand1, 0, seed, 0, rand1.length);
        System.arraycopy(rand2, 0, seed, rand1.length, rand2.length);
        this.seed = seed;
        // 发送握手数据包
        HandshakePacket hs = new HandshakePacket();
        hs.packetId = 0;
        hs.protocolVersion = Versions.PROTOCOL_VERSION;
        hs.serverVersion = Versions.SERVER_VERSION;
        hs.threadId = id;
        hs.seed = rand1;
        hs.serverCapabilities = getServerCapabilities();
        hs.serverCharsetIndex = (byte) (charsetIndex & 0xff);
        hs.serverStatus = 2;
        hs.restOfScrambleBuff = rand2;
        hs.write(this);
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket)

Example 2 with HandshakePacket

use of com.alibaba.cobar.net.mysql.HandshakePacket in project cobar by alibaba.

the class MySQLChannel method handshake.

private MySQLChannel handshake() throws IOException {
    // 读取握手数据包
    BinaryPacket initPacket = new BinaryPacket();
    initPacket.read(in);
    HandshakePacket hsp = new HandshakePacket();
    hsp.read(initPacket);
    // 设置通道参数
    this.threadId = hsp.threadId;
    int ci = hsp.serverCharsetIndex & 0xff;
    if ((dbCharset = CharsetUtil.getDbCharset(ci)) != null) {
        this.charsetIndex = ci;
        this.charset = CharsetUtil.getCharset(ci);
    } else {
        throw new UnknownCharsetException("charset:" + ci);
    }
    // 发送认证数据包
    BinaryPacket bin = null;
    try {
        bin = sendAuth411(hsp);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    switch(bin.data[0]) {
        case OkPacket.FIELD_COUNT:
            afterSuccess();
            break;
        case ErrorPacket.FIELD_COUNT:
            ErrorPacket err = new ErrorPacket();
            err.read(bin);
            throw new ErrorPacketException(new String(err.message, charset));
        case EOFPacket.FIELD_COUNT:
            auth323(bin.packetId, hsp.seed);
            break;
        default:
            throw new UnknownPacketException(bin.toString());
    }
    return this;
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket) ErrorPacketException(com.alibaba.cobar.exception.ErrorPacketException) UnknownPacketException(com.alibaba.cobar.exception.UnknownPacketException) UnknownCharsetException(com.alibaba.cobar.exception.UnknownCharsetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BinaryPacket(com.alibaba.cobar.net.mysql.BinaryPacket)

Example 3 with HandshakePacket

use of com.alibaba.cobar.net.mysql.HandshakePacket in project cobar by alibaba.

the class MySQLConnectionAuthenticator method handle.

@Override
public void handle(byte[] data) {
    try {
        HandshakePacket packet = source.getHandshake();
        if (packet == null) {
            // 设置握手数据包
            packet = new HandshakePacket();
            packet.read(data);
            source.setHandshake(packet);
            source.setThreadId(packet.threadId);
            // 设置字符集编码
            int charsetIndex = (packet.serverCharsetIndex & 0xff);
            String charset = CharsetUtil.getDbCharset(charsetIndex);
            if (charset != null) {
                source.setCharsetIndex(charsetIndex);
                source.setCharset(CharsetUtil.getCharset(charsetIndex));
                source.setDbCharset(charset);
            } else {
                throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
            }
            // 发送认证数据包
            source.authenticate();
        } else {
            // 处理认证结果
            switch(data[4]) {
                case OkPacket.FIELD_COUNT:
                    source.setHandler(new MySQLConnectionHandler(source));
                    source.setAuthenticated(true);
                    if (listener != null) {
                        listener.connectionAcquired(source);
                    }
                    break;
                case ErrorPacket.FIELD_COUNT:
                    ErrorPacket err = new ErrorPacket();
                    err.read(data);
                    throw new RuntimeException(new String(err.message));
                case EOFPacket.FIELD_COUNT:
                    auth323(data[3]);
                    break;
                default:
                    throw new RuntimeException("Unknown Packet!");
            }
        }
    } catch (RuntimeException e) {
        if (listener != null) {
            listener.connectionError(e, source);
        }
        throw e;
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Example 4 with HandshakePacket

use of com.alibaba.cobar.net.mysql.HandshakePacket in project cobar by alibaba.

the class MySQLDetectorAuthenticator method handle.

@Override
public void handle(byte[] data) {
    MySQLDetector source = this.source;
    HandshakePacket hsp = source.getHandshake();
    if (hsp == null) {
        // 设置握手数据包
        hsp = new HandshakePacket();
        hsp.read(data);
        source.setHandshake(hsp);
        // 设置字符集编码
        int charsetIndex = (hsp.serverCharsetIndex & 0xff);
        String charset = CharsetUtil.getDbCharset(charsetIndex);
        if (charset != null) {
            source.setCharsetIndex(charsetIndex);
        } else {
            throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
        }
        // 发送认证数据包
        source.authenticate();
    } else {
        switch(data[4]) {
            case OkPacket.FIELD_COUNT:
                source.setHandler(new MySQLDetectorHandler(source));
                source.setAuthenticated(true);
                // 成功后发起心跳。
                source.heartbeat();
                break;
            case ErrorPacket.FIELD_COUNT:
                ErrorPacket err = new ErrorPacket();
                err.read(data);
                throw new RuntimeException(new String(err.message));
            case EOFPacket.FIELD_COUNT:
                auth323(data[3], hsp.seed);
                break;
            default:
                throw new RuntimeException("Unknown packet");
        }
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Example 5 with HandshakePacket

use of com.alibaba.cobar.net.mysql.HandshakePacket in project cobar by alibaba.

the class CobarDetectorAuthenticator method handle.

@Override
public void handle(byte[] data) {
    CobarDetector source = this.source;
    HandshakePacket hsp = source.getHandshake();
    if (hsp == null) {
        // 设置握手数据包
        hsp = new HandshakePacket();
        hsp.read(data);
        source.setHandshake(hsp);
        // 设置字符集编码
        int charsetIndex = (hsp.serverCharsetIndex & 0xff);
        String charset = CharsetUtil.getDbCharset(charsetIndex);
        if (charset != null) {
            source.setCharsetIndex(charsetIndex);
        } else {
            throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
        }
        // 发送认证数据包
        source.authenticate();
    } else {
        // 处理认证结果
        switch(data[4]) {
            case OkPacket.FIELD_COUNT:
                source.setHandler(new CobarDetectorHandler(source));
                source.setAuthenticated(true);
                // 认证成功后,发起心跳。
                source.heartbeat();
                break;
            case ErrorPacket.FIELD_COUNT:
                ErrorPacket err = new ErrorPacket();
                err.read(data);
                throw new RuntimeException(new String(err.message));
            default:
                throw new RuntimeException("Unknown packet");
        }
    }
}
Also used : HandshakePacket(com.alibaba.cobar.net.mysql.HandshakePacket) ErrorPacket(com.alibaba.cobar.net.mysql.ErrorPacket)

Aggregations

HandshakePacket (com.alibaba.cobar.net.mysql.HandshakePacket)5 ErrorPacket (com.alibaba.cobar.net.mysql.ErrorPacket)4 ErrorPacketException (com.alibaba.cobar.exception.ErrorPacketException)1 UnknownCharsetException (com.alibaba.cobar.exception.UnknownCharsetException)1 UnknownPacketException (com.alibaba.cobar.exception.UnknownPacketException)1 BinaryPacket (com.alibaba.cobar.net.mysql.BinaryPacket)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1