use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generateFetchPayload.
public static byte[] generateFetchPayload(long cursorStatementId, long numOfRows) {
try (MySQLPayloadWriter writer = new MySQLPayloadWriter(9)) {
writer.writeByte(0x1c);
writer.writeFixInt(4, cursorStatementId);
writer.writeFixInt(4, numOfRows);
return writer.toByteArray();
}
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generatePrepareOk.
public static byte[] generatePrepareOk(PreparedOKPacket preparedOKPacket) {
long statementId = preparedOKPacket.getPreparedOkStatementId();
int columnsCount = preparedOKPacket.getPrepareOkColumnsCount();
int warningCount = preparedOKPacket.getPreparedOkWarningCount();
int parametersCount = preparedOKPacket.getPrepareOkParametersCount();
try (MySQLPayloadWriter writer = new MySQLPayloadWriter(512)) {
writer.writeByte(0);
writer.writeFixInt(4, statementId);
writer.writeFixInt(2, columnsCount);
writer.writeFixInt(2, parametersCount);
writer.writeByte(0);
writer.writeFixInt(2, warningCount);
return writer.toByteArray();
}
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class ConnectHandler method sendHandshakeResponseMessage.
private void sendHandshakeResponseMessage(String username, String password, String database, byte[] nonce, String clientPluginName, int clientCapabilitiesFlags, int collationId, Map<String, String> clientConnectionAttributes) {
AuthPacket authPacket = new AuthPacket();
authPacket.setCapabilities(clientCapabilitiesFlags);
authPacket.setMaxPacketSize(PACKET_PAYLOAD_LENGTH_LIMIT);
authPacket.setCharacterSet((byte) collationId);
authPacket.setUsername(username);
authPacket.setDatabase(database);
authPacket.setClientConnectAttrs(clientConnectionAttributes);
String authMethod = clientPluginName;
if (password.isEmpty()) {
authPacket.setPassword(new byte[] {});
} else {
byte[] authResponse;
switch(authMethod) {
case "mysql_native_password":
authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
break;
case "caching_sha2_password":
authResponse = CachingSha2Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
break;
case "mysql_clear_password":
authResponse = password.getBytes(StandardCharsets.UTF_8);
break;
default:
authMethod = "mysql_native_password";
authResponse = Native41Authenticator.encode(password.getBytes(StandardCharsets.UTF_8), nonce);
break;
}
authPacket.setPassword(authResponse);
}
authPacket.setAuthPluginName(authMethod);
MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter();
authPacket.writePayload(mySQLPayloadWriter);
super.socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(++packetId, mySQLPayloadWriter.toByteArray())));
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class VertxMySQLAuthHandler method handle.
@Override
public void handle(Buffer event) {
buffer.appendBuffer(event);
if (buffer.length() > 3) {
int length = readInt(buffer, 0, 3);
if (length == buffer.length() - 4) {
int packetId = buffer.getUnsignedByte(3);
Buffer payload = buffer.slice(4, buffer.length());
ReadView readView = new ReadView(payload);
if (!authSwitchResponse) {
this.authPacket = new AuthPacket();
authPacket.readPayload(readView);
if ("mysql_native_password".equalsIgnoreCase(authPacket.getAuthPluginName()) || authPacket.getAuthPluginName() == null) {
auth(packetId);
} else {
authSwitchResponse = true;
buffer = Buffer.buffer();
AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
authSwitchRequestPacket.setStatus((byte) 0xfe);
authSwitchRequestPacket.setAuthPluginName("mysql_native_password");
authSwitchRequestPacket.setAuthPluginData(new String(seedParts[2]));
MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
socket.write(Buffer.buffer(MySQLPacketUtil.generateMySQLPacket(packetId + 1, mySQLPayloadWriter)));
return;
}
} else {
byte[] bytes = readView.readEOFStringBytes();
authPacket.setPassword(bytes);
auth(packetId);
}
}
}
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLClientAuthHandler method createHandshakePayload.
public static byte[] createHandshakePayload(long sessionId, int serverCapabilities, byte[][] seedParts) {
HandshakePacket hs = new HandshakePacket();
hs.setProtocolVersion(MySQLVersion.PROTOCOL_VERSION);
hs.setServerVersion(new String(MySQLVersion.SERVER_VERSION));
hs.setConnectionId(sessionId);
hs.setAuthPluginDataPartOne(new String(seedParts[0]));
hs.setCapabilities(new MySQLServerCapabilityFlags(serverCapabilities));
hs.setHasPartTwo(true);
hs.setCharacterSet(8);
hs.setStatusFlags(2);
// 有插件的话,总长度必是21, seed
hs.setAuthPluginDataLen(21);
hs.setAuthPluginDataPartTwo(new String(seedParts[1]));
hs.setAuthPluginName(MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME);
MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter();
hs.writePayload(mySQLPayloadWriter);
return mySQLPayloadWriter.toByteArray();
}
Aggregations