Search in sources :

Example 1 with Handshake

use of com.alibaba.otter.canal.protocol.AdminPacket.Handshake in project canal by alibaba.

the class SimpleAdminConnector method connect.

@Override
public void connect() throws ServiceException {
    try {
        if (connected) {
            return;
        }
        channel = SocketChannel.open();
        channel.socket().setSoTimeout(soTimeout);
        channel.connect(address);
        readableChannel = Channels.newChannel(channel.socket().getInputStream());
        writableChannel = Channels.newChannel(channel.socket().getOutputStream());
        Packet p = Packet.parseFrom(readNextPacket());
        if (p.getVersion() != 1) {
            throw new CanalClientException("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new CanalClientException("expect handshake but found other type.");
        }
        Handshake handshake = Handshake.parseFrom(p.getBody());
        // seed for auth
        ByteString seed = handshake.getSeeds();
        String newPasswd = passwd;
        if (passwd != null) {
            // encode passwd
            newPasswd = SecurityUtil.byte2HexStr(SecurityUtil.scramble411(passwd.getBytes(), seed.toByteArray()));
        }
        ClientAuth ca = ClientAuth.newBuilder().setUsername(user != null ? user : "").setPassword(ByteString.copyFromUtf8(newPasswd != null ? newPasswd : "")).setNetReadTimeout(idleTimeout).setNetWriteTimeout(idleTimeout).build();
        writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        // 
        Packet ack = Packet.parseFrom(readNextPacket());
        if (ack.getType() != PacketType.ACK) {
            throw new CanalClientException("unexpected packet type when ack is expected");
        }
        Ack ackBody = Ack.parseFrom(ack.getBody());
        if (ackBody.getCode() > 0) {
            throw new ServiceException("something goes wrong when doing authentication: " + ackBody.getMessage());
        }
        connected = true;
    } catch (IOException | NoSuchAlgorithmException e) {
        throw new ServiceException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.AdminPacket.Packet) ServiceException(com.alibaba.otter.canal.admin.common.exception.ServiceException) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) ByteString(com.google.protobuf.ByteString) Ack(com.alibaba.otter.canal.protocol.AdminPacket.Ack) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientAuth(com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth) Handshake(com.alibaba.otter.canal.protocol.AdminPacket.Handshake)

Aggregations

ServiceException (com.alibaba.otter.canal.admin.common.exception.ServiceException)1 Ack (com.alibaba.otter.canal.protocol.AdminPacket.Ack)1 ClientAuth (com.alibaba.otter.canal.protocol.AdminPacket.ClientAuth)1 Handshake (com.alibaba.otter.canal.protocol.AdminPacket.Handshake)1 Packet (com.alibaba.otter.canal.protocol.AdminPacket.Packet)1 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1