use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class BackendConCreateHandler method handle.
public void handle(MySQLClientSession mysql) throws Exception {
ProxyBuffer proxyBuffer = mysql.currentProxyBuffer().newBufferIfNeed();
int totalPacketEndIndex = proxyBuffer.channelReadEndIndex();
if (!mysql.readProxyPayloadFully()) {
return;
}
MySQLPayloadType payloadType = mysql.getPayloadType();
if (!welcomePkgReceived) {
writeClientAuth(mysql);
return;
}
// 收到切换登陆插件的包
if (mysql.getPayloadType() == MySQLPayloadType.FIRST_EOF && mysql.getBackendPacketResolver().getState() == ComQueryState.AUTH_SWITCH_PLUGIN_RESPONSE) {
// 重新发送密码验证
MySQLPacket mySQLPacket = mysql.currentProxyPayload();
AuthSwitchRequestPacket authSwitchRequestPacket = new AuthSwitchRequestPacket();
authSwitchRequestPacket.readPayload(mySQLPacket);
byte[] password = generatePassword(authSwitchRequestPacket);
mySQLPacket = mysql.newCurrentProxyPacket(1024);
mySQLPacket.writeBytes(password);
mysql.writeCurrentProxyPacket(mySQLPacket, mysql.getPacketId() + 1);
mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
return;
}
// 验证成功
if (payloadType == MySQLPayloadType.FIRST_OK) {
mysql.switchNioHandler(null);
mysql.resetPacket();
mysql.getBackendPacketResolver().setIsClientLoginRequest(false);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("successful create mysql backend connection:{}", mysql.sessionId());
}
callback.complete(mysql);
return;
}
MySQLPacket mySQLPacket = mysql.getBackendPacketResolver().currentPayload();
// 用公钥进行密码加密
if (STR_CACHING_AUTH_STAGE.equals(stage) && authPluginName.equals(CachingSha2PasswordPlugin.PROTOCOL_PLUGIN_NAME)) {
LOGGER.info("authPluginName:{} ", authPluginName);
String publicKeyString = mySQLPacket.readEOFString();
byte[] payload = CachingSha2PasswordPlugin.encrypt(mysqlVersion, publicKeyString, datasource.getPassword(), seed, CharsetUtil.getCharset(charsetIndex));
mySQLPacket = mysql.newCurrentProxyPacket(1024);
mySQLPacket.writeBytes(payload);
mysql.writeCurrentProxyPacket(mySQLPacket, mysql.getPacketId() + 1);
mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
stage = null;
return;
}
int status = mySQLPacket.getByte(4) & 0xff;
int fastAuthResult = mySQLPacket.getByte(5) & 0xff;
if (status == 1 && fastAuthResult == 3) {
// 验证成功继续 读取认证的ok包
mysql.resetCurrentProxyPayload();
proxyBuffer.channelReadEndIndex(totalPacketEndIndex);
MySQLPacketResolver packetResolver = mysql.getBackendPacketResolver();
mySQLPacket.packetReadStartIndex(packetResolver.getEndPos());
mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
handle(mysql);
return;
}
if (status == 1 && fastAuthResult == 4) {
// 发送payload为02的包 然后读取公钥加密密码
byte[] payload = { (byte) 2 };
mySQLPacket = mysql.newCurrentProxyPacket(1024);
mySQLPacket.writeBytes(payload);
mysql.writeCurrentProxyPacket(mySQLPacket, 3);
stage = STR_CACHING_AUTH_STAGE;
mysql.getBackendPacketResolver().setIsClientLoginRequest(true);
return;
}
// 连接不上
ErrorPacketImpl errorPacket = new ErrorPacketImpl();
errorPacket.readPayload(mySQLPacket);
onException(mysql, new MycatException(errorPacket.getErrorCode(), errorPacket.getErrorMessageString()));
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class ResultSetHandler method request.
/**
* COM_QUERY 命令请求报文调用此方法 head是payload的第一个字节 data根据实际报文构造 该方法自动构造请求报文,生成报文序列号以及长度,
* 但是被限制整个报文长度不超过proxybuffer的chunk大小,大小也不应该超过mysql拆分报文大小 如需构造大的报文,可以自行替换proxbuffer即可
*/
default void request(MySQLClientSession mysql, int head, byte[] data, ResultSetCallBack<MySQLClientSession> callBack) {
if (data == null) {
data = EMPTY;
}
assert (mysql.currentProxyBuffer() == null);
int chunkSize = mysql.getIOThread().getBufPool().chunkSize();
if (data.length > (chunkSize - 5) || data.length > MySQLPacketSplitter.MAX_PACKET_SIZE) {
throw new MycatException("ResultSetHandler unsupport request length more than 1024 bytes");
}
mysql.setCurrentProxyBuffer(new ProxyBufferImpl(mysql.getIOThread().getBufPool()));
MySQLPacket mySQLPacket = mysql.newCurrentProxyPacket(data.length + 5);
mySQLPacket.writeByte((byte) head);
mySQLPacket.writeBytes(data);
request(mysql, mySQLPacket, mysql.setPacketId(0), callBack);
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class ProxyBufferImpl method readFromChannel.
/**
* 从读通道获取数据
*/
@Override
public boolean readFromChannel(SocketChannel channel) throws IOException {
buffer.limit(buffer.capacity());
int readEndIndex = this.readEndIndex;
buffer.position(readEndIndex);
int readed = channel.read(buffer);
if (readed == -1) {
throw new ClosedChannelException();
} else if (readed == 0) {
throw new MycatException("readed zero bytes ,Maybe a bug ,please fix it !!!!");
}
this.channelReadEndIndex(buffer.position());
return readed > 0;
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class ShowTriggersSQLHandler method onExecute.
@Override
protected Future<Void> onExecute(SQLRequest<MySqlShowTriggersStatement> request, MycatDataContext dataContext, Response response) {
MySqlShowTriggersStatement ast = request.getAst();
if (ast.getDatabase() == null && dataContext.getDefaultSchema() != null) {
ast.setDatabase(new SQLIdentifierExpr(dataContext.getDefaultSchema()));
}
String database = ast.getDatabase().getSimpleName();
SQLExpr like = ast.getLike();
SQLExpr where = ast.getWhere();
if (database == null) {
return response.sendError(new MycatException("NO DATABASES SELECTED"));
}
String sql = toNormalSQL(request.getAst());
return DrdsRunnerHelper.runOnDrds(dataContext, DrdsRunnerHelper.preParse(sql, dataContext.getDefaultSchema()), response);
}
use of io.mycat.MycatException in project Mycat2 by MyCATApache.
the class MycatCalciteMySqlNodeVisitor method visit.
@Override
public boolean visit(SQLExprTableSource x) {
SqlNode table;
SQLExpr expr = x.getExpr();
SqlNodeList sqlNodes = convertHints((List) x.getHints(), x.computeAlias());
if (expr instanceof SQLIdentifierExpr) {
table = new SqlTableRef(SqlParserPos.ZERO, (SqlIdentifier) buildIdentifier((SQLIdentifierExpr) expr), sqlNodes);
} else if (expr instanceof SQLPropertyExpr) {
table = new SqlTableRef(SqlParserPos.ZERO, (SqlIdentifier) buildIdentifier((SQLPropertyExpr) expr), sqlNodes);
} else {
throw new MycatException("not support : " + expr);
}
if (x.getAlias() != null) {
SqlIdentifier alias = new SqlIdentifier(SQLUtils.normalize(x.computeAlias()), SqlParserPos.ZERO);
SqlBasicCall as = new SqlBasicCall(SqlStdOperatorTable.AS, new SqlNode[] { table, alias }, SqlParserPos.ZERO);
sqlNode = as;
} else {
sqlNode = table;
}
return false;
}
Aggregations