use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generateChangeUser.
public static final byte[] generateChangeUser(String username, int serverCapabilities, String authenticationResponse, String defaultSchemaName, int clientCharSet, String authenticationPluginName, Map<String, String> attr) {
try (MySQLPayloadWriter writer = new MySQLPayloadWriter(512)) {
writer.write(0x11);
writer.writeNULString(username);
if (MySQLServerCapabilityFlags.isCanDo41Anthentication(serverCapabilities)) {
byte[] bytes = authenticationResponse.getBytes();
writer.write(bytes.length);
writer.write(bytes);
} else {
writer.writeNULString(authenticationResponse);
}
if (MySQLServerCapabilityFlags.isConnectionWithDatabase(serverCapabilities)) {
writer.writeNULString(defaultSchemaName);
}
writer.writeFixInt(2, clientCharSet);
if (MySQLServerCapabilityFlags.isPluginAuth(serverCapabilities)) {
writer.writeNULString(authenticationPluginName);
}
if (MySQLServerCapabilityFlags.isConnectAttrs(serverCapabilities)) {
if (attr != null && !attr.isEmpty()) {
try (MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(128)) {
for (Entry<String, String> entry : attr.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
mySQLPayloadWriter.writeLenencString(key);
mySQLPayloadWriter.writeLenencString(value);
}
byte[] bytes = mySQLPayloadWriter.toByteArray();
writer.writeLenencInt(bytes.length);
writer.writeBytes(bytes);
}
}
}
return writer.toByteArray();
}
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generateEof.
public static final byte[] generateEof(int warningCount, int status) {
try (MySQLPayloadWriter writer = new MySQLPayloadWriter(12)) {
writer.writeByte(0xfe);
writer.writeFixInt(2, warningCount);
writer.writeFixInt(2, status);
return writer.toByteArray();
}
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generateResultSetCount.
public static final byte[] generateResultSetCount(int fieldCount) {
MySQLPayloadWriter writer = new MySQLPayloadWriter(1);
writer.writeLenencInt(fieldCount);
return writer.toByteArray();
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLPacketUtil method generateColumnDefPayload.
public static final byte[] generateColumnDefPayload(String database, String table, String originalTable, String columnName, String orgName, int type, int columnFlags, int columnDecimals, int charsetIndex, int length, Charset charset) {
ColumnDefPacketImpl c = new ColumnDefPacketImpl();
c.setColumnSchema(database.getBytes(charset));
c.setColumnOrgTable(originalTable.getBytes(charset));
c.setColumnTable(table.getBytes(charset));
c.setColumnCharsetSet(charsetIndex);
c.setColumnLength(length);
c.setColumnName(encode(columnName, charset));
c.setColumnOrgName(encode(orgName, charset));
c.setColumnType(type);
c.setColumnFlags(columnFlags);
c.setColumnDecimals((byte) columnDecimals);
MySQLPayloadWriter writer = new MySQLPayloadWriter(64);
c.writePayload(writer);
return writer.toByteArray();
}
use of io.mycat.beans.mysql.MySQLPayloadWriter in project Mycat2 by MyCATApache.
the class MySQLClientAuthHandler method onSocketRead.
// public void setMycatSession(MycatSession mycatSession) {
// this.mycat = mycatSession;
// }
@Override
public void onSocketRead(MycatSession mycat) {
try {
if (mycat.getCurNIOHandler() != this) {
return;
}
if (!mycat.readFromChannel()) {
return;
}
mycat.setResponseFinished(ProcessState.READY);
// MycatSecurityConfig securityManager = runtime.getSecurityManager();
byte[] password = new byte[] {};
if (!isChangeAuthPlugin) {
// 密码读取与验证
this.auth = readResponseAuthPacket(mycat);
String authPluginName = auth.getAuthPluginName();
int capabilities = auth.getCapabilities();
// 切换auth_plugin
if (MySQLServerCapabilityFlags.isPluginAuth(capabilities) && !authPluginName.equals(clientAuthPluginName)) {
// 发送切换包的auth_response
isChangeAuthPlugin = true;
AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
clientAuthPluginName = StringUtil.isEmpty(authPluginName) ? MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME : authPluginName;
authSwitchRequestPacket.setAuthPluginName(MysqlNativePasswordPluginUtil.PROTOCOL_PLUGIN_NAME);
authSwitchRequestPacket.setStatus((byte) 0xfe);
authSwitchRequestPacket.setAuthPluginData(new String(seed));
MySQLPayloadWriter mySQLPayloadWriter = new MySQLPayloadWriter(1024);
authSwitchRequestPacket.writePayload(mySQLPayloadWriter);
mycat.writeBytes(mySQLPayloadWriter.toByteArray(), true);
return;
}
// 握手包中的加密密码
password = auth.getPassword();
} else {
MySQLPacket mySQLPacket = mycat.currentProxyPayload();
password = mySQLPacket.readEOFStringBytes();
}
int capabilities = auth.getCapabilities();
if (MySQLServerCapabilityFlags.isCanUseCompressionProtocol(capabilities)) {
String message = "Can Not Use Compression Protocol!";
failture(mycat, MySQLErrorCode.ER_UNKNOWN_ERROR, message);
mycat.lazyClose(true, message);
return;
}
String username = auth.getUsername();
int maxPacketSize = auth.getMaxPacketSize();
String database = auth.getDatabase();
int characterSet = auth.getCharacterSet();
Map<String, String> attrs = auth.getClientConnectAttrs();
MycatUser user = null;
SocketAddress remoteSocketAddress = mycat.channel().socket().getRemoteSocketAddress();
Authenticator authenticator = MetaClusterCurrent.wrapper(Authenticator.class);
String ip = SocketAddressUtil.simplySocketAddress(remoteSocketAddress);
Authenticator.AuthInfo authInfo = authenticator.getPassword(username, ip);
if (!authInfo.isOk()) {
failture(mycat, authInfo.getErrorCode(), authInfo.getException());
return;
} else {
String rightPassword = authInfo.getRightPassword();
if (rightPassword != null) {
if (!checkPassword(rightPassword, password)) {
// may be bug
String message = "Access denied for user '" + username + "'@'" + remoteSocketAddress.toString() + "' (using password: YES)";
mycat.setLastMessage(message);
mycat.setLastErrorCode(ER_ACCESS_DENIED_ERROR);
LOGGER.error("login fail: {}", message);
mycat.writeErrorEndPacketBySyncInProcessError(ER_ACCESS_DENIED_ERROR);
LOGGER.error("remoteSocketAddress:{} password is wrong", remoteSocketAddress);
return;
}
}
UserConfig userInfo = authenticator.getUserInfo(username);
user = new MycatUser(username, null, null, ip, remoteSocketAddress, userInfo);
}
mycat.getDataContext().setUser(user);
mycat.setSchema(database);
mycat.setServerCapabilities(auth.getCapabilities());
mycat.setIsolation(MySQLIsolation.REPEATED_READ);
mycat.setCharset(characterSet);
finished = true;
mycatSessionManager.initCommandDispatcher(mycat);
mycat.writeOkEndPacket();
} catch (Exception e) {
LOGGER.error("", e);
MycatMonitor.onAuthHandlerReadException(mycat, e);
onClear(mycat);
failture(mycat, e);
}
}
Aggregations