Search in sources :

Example 1 with HandshakePacket

use of io.mycat.net.mysql.HandshakePacket in project Mycat-Server by MyCATApache.

the class MySQLConnectionAuthenticator method handle.

@Override
public void handle(byte[] data) {
    try {
        switch(data[4]) {
            case OkPacket.FIELD_COUNT:
                HandshakePacket packet = source.getHandshake();
                if (packet == null) {
                    processHandShakePacket(data);
                    // 发送认证数据包
                    source.authenticate();
                    break;
                }
                // 处理认证结果
                source.setHandler(new MySQLConnectionHandler(source));
                source.setAuthenticated(true);
                boolean clientCompress = Capabilities.CLIENT_COMPRESS == (Capabilities.CLIENT_COMPRESS & packet.serverCapabilities);
                boolean usingCompress = MycatServer.getInstance().getConfig().getSystem().getUseCompression() == 1;
                if (clientCompress && usingCompress) {
                    source.setSupportCompress(true);
                }
                if (listener != null) {
                    listener.connectionAcquired(source);
                }
                break;
            case ErrorPacket.FIELD_COUNT:
                ErrorPacket err = new ErrorPacket();
                err.read(data);
                String errMsg = new String(err.message);
                LOGGER.warn("can't connect to mysql server ,errmsg:" + errMsg + " " + source);
                //source.close(errMsg);
                throw new ConnectionException(err.errno, errMsg);
            case EOFPacket.FIELD_COUNT:
                auth323(data[3]);
                break;
            default:
                packet = source.getHandshake();
                if (packet == null) {
                    processHandShakePacket(data);
                    // 发送认证数据包
                    source.authenticate();
                    break;
                } else {
                    throw new RuntimeException("Unknown Packet!");
                }
        }
    } catch (RuntimeException e) {
        if (listener != null) {
            listener.connectionError(e, source);
            return;
        }
        throw e;
    }
}
Also used : HandshakePacket(io.mycat.net.mysql.HandshakePacket) ErrorPacket(io.mycat.net.mysql.ErrorPacket) ConnectionException(io.mycat.net.ConnectionException)

Example 2 with HandshakePacket

use of io.mycat.net.mysql.HandshakePacket in project Mycat-Server by MyCATApache.

the class MySQLConnectionAuthenticator method processHandShakePacket.

private void processHandShakePacket(byte[] data) {
    // 设置握手数据包
    HandshakePacket packet = new HandshakePacket();
    packet.read(data);
    source.setHandshake(packet);
    source.setThreadId(packet.threadId);
    // 设置字符集编码
    int charsetIndex = (packet.serverCharsetIndex & 0xff);
    String charset = CharsetUtil.getCharset(charsetIndex);
    if (charset != null) {
        source.setCharset(charset);
    } else {
        throw new RuntimeException("Unknown charsetIndex:" + charsetIndex);
    }
}
Also used : HandshakePacket(io.mycat.net.mysql.HandshakePacket)

Example 3 with HandshakePacket

use of io.mycat.net.mysql.HandshakePacket in project Mycat-Server by MyCATApache.

the class MySQLDataSource method testConnection.

@Override
public boolean testConnection(String schema) throws IOException {
    boolean isConnected = true;
    Socket socket = null;
    InputStream in = null;
    OutputStream out = null;
    try {
        socket = new Socket(this.getConfig().getIp(), this.getConfig().getPort());
        socket.setSoTimeout(1000 * 20);
        socket.setReceiveBufferSize(32768);
        socket.setSendBufferSize(32768);
        socket.setTcpNoDelay(true);
        socket.setKeepAlive(true);
        in = new BufferedInputStream(socket.getInputStream(), 32768);
        out = new BufferedOutputStream(socket.getOutputStream(), 32768);
        /**
	         * Phase 1: MySQL to client. Send handshake packet.
	        */
        BinaryPacket bin1 = new BinaryPacket();
        bin1.read(in);
        HandshakePacket handshake = new HandshakePacket();
        handshake.read(bin1);
        /**
	         * Phase 2: client to MySQL. Send auth packet.
	         */
        AuthPacket authPacket = new AuthPacket();
        authPacket.packetId = 1;
        authPacket.clientFlags = getClientFlags();
        authPacket.maxPacketSize = 1024 * 1024 * 16;
        authPacket.charsetIndex = handshake.serverCharsetIndex & 0xff;
        authPacket.user = this.getConfig().getUser();
        ;
        try {
            authPacket.password = passwd(this.getConfig().getPassword(), handshake);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        }
        authPacket.database = schema;
        authPacket.write(out);
        out.flush();
        /**
	         * Phase 3: MySQL to client. send OK/ERROR packet.
	         */
        BinaryPacket bin2 = new BinaryPacket();
        bin2.read(in);
        switch(bin2.data[0]) {
            case OkPacket.FIELD_COUNT:
                break;
            case ErrorPacket.FIELD_COUNT:
                ErrorPacket err = new ErrorPacket();
                err.read(bin2);
                isConnected = false;
            case EOFPacket.FIELD_COUNT:
                // 发送323响应认证数据包
                Reply323Packet r323 = new Reply323Packet();
                r323.packetId = ++bin2.packetId;
                String passwd = this.getConfig().getPassword();
                if (passwd != null && passwd.length() > 0) {
                    r323.seed = SecurityUtil.scramble323(passwd, new String(handshake.seed)).getBytes();
                }
                r323.write(out);
                out.flush();
                break;
        }
    } catch (IOException e) {
        isConnected = false;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        try {
            if (out != null) {
                out.write(QuitPacket.QUIT);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
        }
        try {
            if (socket != null)
                socket.close();
        } catch (IOException e) {
        }
    }
    return isConnected;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) HandshakePacket(io.mycat.net.mysql.HandshakePacket) ErrorPacket(io.mycat.net.mysql.ErrorPacket) BufferedInputStream(java.io.BufferedInputStream) AuthPacket(io.mycat.net.mysql.AuthPacket) Reply323Packet(io.mycat.net.mysql.Reply323Packet) BufferedOutputStream(java.io.BufferedOutputStream) Socket(java.net.Socket) BinaryPacket(io.mycat.net.mysql.BinaryPacket)

Example 4 with HandshakePacket

use of io.mycat.net.mysql.HandshakePacket in project Mycat-Server by MyCATApache.

the class FrontendConnection method register.

@Override
public void register() throws IOException {
    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;
        // 发送握手数据包
        boolean useHandshakeV10 = MycatServer.getInstance().getConfig().getSystem().getUseHandshakeV10() == 1;
        if (useHandshakeV10) {
            HandshakeV10Packet hs = new HandshakeV10Packet();
            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);
        } else {
            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);
        }
        // asynread response
        this.asynRead();
    }
}
Also used : HandshakePacket(io.mycat.net.mysql.HandshakePacket) HandshakeV10Packet(io.mycat.net.mysql.HandshakeV10Packet)

Aggregations

HandshakePacket (io.mycat.net.mysql.HandshakePacket)4 ErrorPacket (io.mycat.net.mysql.ErrorPacket)2 ConnectionException (io.mycat.net.ConnectionException)1 AuthPacket (io.mycat.net.mysql.AuthPacket)1 BinaryPacket (io.mycat.net.mysql.BinaryPacket)1 HandshakeV10Packet (io.mycat.net.mysql.HandshakeV10Packet)1 Reply323Packet (io.mycat.net.mysql.Reply323Packet)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1