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);
}
}
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;
}
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;
}
}
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");
}
}
}
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");
}
}
}
Aggregations