Skip to content

Commit

Permalink
#241 delete useHandshakeV10 in server.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsun314 committed Sep 11, 2017
1 parent b8de519 commit 0a8ba1a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,12 @@ private static Calendar getLocalCalendar() {
return cal;
}


public byte[] readBytesWithInputLength(int size) {
byte[] ab = new byte[size];
System.arraycopy(data, position, ab, 0, ab.length);
position += size;
return ab;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static long initClientFlags() {
private MySQLDataSource pool;
private boolean fromSlaveDB;
private long threadId;
private HandshakePacket handshake;
private HandshakeV10Packet handshake;
private long clientFlags;
private boolean isAuthenticated;
private String user;
Expand Down Expand Up @@ -212,11 +212,11 @@ public void setPassword(String password) {
this.password = password;
}

public HandshakePacket getHandshake() {
public HandshakeV10Packet getHandshake() {
return handshake;
}

public void setHandshake(HandshakePacket handshake) {
public void setHandshake(HandshakeV10Packet handshake) {
this.handshake = handshake;
}

Expand Down Expand Up @@ -526,7 +526,7 @@ public void writeQueueAvailable() {
}
}

private static byte[] passwd(String pass, HandshakePacket hs)
private static byte[] passwd(String pass, HandshakeV10Packet hs)
throws NoSuchAlgorithmException {
if (pass == null || pass.length() == 0) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void handle(byte[] data) {
try {
switch (data[4]) {
case OkPacket.FIELD_COUNT:
HandshakePacket packet = source.getHandshake();
HandshakeV10Packet packet = source.getHandshake();
if (packet == null) {
processHandShakePacket(data);
// send auth packet
Expand Down Expand Up @@ -114,7 +114,7 @@ public void handle(byte[] data) {
}

private void processHandShakePacket(byte[] data) {
HandshakePacket packet = new HandshakePacket();
HandshakeV10Packet packet = new HandshakeV10Packet();
packet.read(data);
source.setHandshake(packet);
source.setThreadId(packet.getThreadId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private long getClientFlags() {
}


private byte[] passwd(String pass, HandshakePacket hs) throws NoSuchAlgorithmException {
private byte[] passwd(String pass, HandshakeV10Packet hs) throws NoSuchAlgorithmException {
if (pass == null || pass.length() == 0) {
return null;
}
Expand Down Expand Up @@ -119,7 +119,7 @@ public boolean testConnection(String schema) throws IOException {
BinaryPacket bin1 = new BinaryPacket();
bin1.read(in);

HandshakePacket handshake = new HandshakePacket();
HandshakeV10Packet handshake = new HandshakeV10Packet();
handshake.read(bin1);

/**
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/actiontech/dble/config/model/SystemConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ public final class SystemConfig {
private int useCompression = 0;
private int useSqlStat = 1;

//TODO: DELETE ,just USE HandshakeV10Packet
private int useHandshakeV10 = 1;
private int checkTableConsistency = 0;
private long checkTableConsistencyPeriod = CHECK_TABLE_CONSISTENCY_PERIOD;
private int useGlobleTableCheck = 1;
Expand Down Expand Up @@ -745,15 +743,6 @@ public void setCheckTableConsistencyPeriod(long checkTableConsistencyPeriod) {
this.checkTableConsistencyPeriod = checkTableConsistencyPeriod;
}

public int getUseHandshakeV10() {
return useHandshakeV10;
}

@SuppressWarnings("unused")
public void setUseHandshakeV10(int useHandshakeV10) {
this.useHandshakeV10 = useHandshakeV10;
}

public int getNestLoopRowsSize() {
return nestLoopRowsSize;
}
Expand Down
44 changes: 13 additions & 31 deletions src/main/java/com/actiontech/dble/net/FrontendConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,33 +420,18 @@ public void register() throws IOException {
System.arraycopy(rand2, 0, rand, rand1.length, rand2.length);
this.seed = rand;

// send auth data
boolean useHandshakeV10 = DbleServer.getInstance().getConfig().getSystem().getUseHandshakeV10() == 1;
if (useHandshakeV10) {
HandshakeV10Packet hs = new HandshakeV10Packet();
hs.setPacketId(0);
hs.setProtocolVersion(Versions.PROTOCOL_VERSION);
hs.setServerVersion(Versions.getServerVersion());
hs.setThreadId(id);
hs.setSeed(rand1);
hs.setServerCapabilities(getServerCapabilities());
hs.setServerCharsetIndex((byte) (charsetIndex & 0xff));
hs.setServerStatus(2);
hs.setRestOfScrambleBuff(rand2);
hs.write(this);
} else {
HandshakePacket hs = new HandshakePacket();
hs.setPacketId(0);
hs.setProtocolVersion(Versions.PROTOCOL_VERSION);
hs.setServerVersion(Versions.getServerVersion());
hs.setThreadId(id);
hs.setSeed(rand1);
hs.setServerCapabilities(getServerCapabilities());
hs.setServerCharsetIndex((byte) (charsetIndex & 0xff));
hs.setServerStatus(2);
hs.setRestOfScrambleBuff(rand2);
hs.write(this);
}

HandshakeV10Packet hs = new HandshakeV10Packet();
hs.setPacketId(0);
hs.setProtocolVersion(Versions.PROTOCOL_VERSION); // [0a] protocol version V10
hs.setServerVersion(Versions.getServerVersion()); // 版本号
hs.setThreadId(id);
hs.setSeed(rand1);
hs.setServerCapabilities(getServerCapabilities());
hs.setServerCharsetIndex((byte) (charsetIndex & 0xff));
hs.setServerStatus(2);
hs.setRestOfScrambleBuff(rand2);
hs.write(this);

// asynread response
this.asynRead();
Expand Down Expand Up @@ -510,10 +495,7 @@ protected int getServerCapabilities() {
flag |= Capabilities.CLIENT_SECURE_CONNECTION;
flag |= Capabilities.CLIENT_MULTI_STATEMENTS;
flag |= Capabilities.CLIENT_MULTI_RESULTS;
boolean useHandshakeV10 = DbleServer.getInstance().getConfig().getSystem().getUseHandshakeV10() == 1;
if (useHandshakeV10) {
flag |= Capabilities.CLIENT_PLUGIN_AUTH;
}
flag |= Capabilities.CLIENT_PLUGIN_AUTH;
return flag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public void handle(byte[] data) {
AuthPacket auth = new AuthPacket();
auth.read(data);

//check mysql_native_password
if (!"mysql_native_password".equals(auth.getAuthPlugin())) {
failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "only mysql_native_password auth check is supported");
return;
}

// check user
if (!checkUser(auth.getUser(), source.getHost())) {
failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.getUser() + "' with host '" + source.getHost() + "'");
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/com/actiontech/dble/net/mysql/AuthPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;

import static com.actiontech.dble.config.Capabilities.CLIENT_PLUGIN_AUTH;


/**
* From client to server during initial handshake.
* <p>
Expand Down Expand Up @@ -63,31 +66,36 @@ public class AuthPacket extends MySQLPacket {
private String user;
private byte[] password;
private String database;
private String authPlugin;

public void read(byte[] data) {
MySQLMessage mm = new MySQLMessage(data);
packetLength = mm.readUB3();
packetId = mm.read();
clientFlags = mm.readUB4();
maxPacketSize = mm.readUB4();
charsetIndex = (mm.read() & 0xff);
clientFlags = mm.readUB4(); // capability flags
maxPacketSize = mm.readUB4(); //maxoacketSize
charsetIndex = (mm.read() & 0xff); // character set
// read extra
int current = mm.position();
int len = (int) mm.readLength();
if (len > 0 && len < FILLER.length) {
if (len > 0 && len < FILLER.length) { //reserved
byte[] ab = new byte[len];
System.arraycopy(mm.bytes(), mm.position(), ab, 0, len);
this.extra = ab;
}
mm.position(current + FILLER.length);
user = mm.readStringWithNull();
user = mm.readStringWithNull(); //user name end by a [00]
password = mm.readBytesWithLength();
if (((clientFlags & Capabilities.CLIENT_CONNECT_WITH_DB) != 0) && mm.hasRemaining()) {
database = mm.readStringWithNull();
if (database != null && DbleServer.getInstance().getConfig().getSystem().isLowerCaseTableNames()) {
database = database.toLowerCase();
}
}

if ((clientFlags & CLIENT_PLUGIN_AUTH) != 0) {
authPlugin = mm.readStringWithNull();
}
}

public void write(OutputStream out) throws IOException {
Expand Down Expand Up @@ -119,10 +127,10 @@ public void write(BackendAIOConnection c) {
ByteBuffer buffer = c.allocate();
BufferUtil.writeUB3(buffer, calcPacketSize());
buffer.put(packetId);
BufferUtil.writeUB4(buffer, clientFlags);
BufferUtil.writeUB4(buffer, maxPacketSize);
buffer.put((byte) charsetIndex);
buffer = c.writeToBuffer(FILLER, buffer);
BufferUtil.writeUB4(buffer, clientFlags); // capability flags
BufferUtil.writeUB4(buffer, maxPacketSize); // max-packet size
buffer.put((byte) charsetIndex); //character set
buffer = c.writeToBuffer(FILLER, buffer); // reserved (all [0])
if (user == null) {
buffer = c.checkWriteBuffer(buffer, 1, true);
buffer.put((byte) 0);
Expand All @@ -146,6 +154,11 @@ public void write(BackendAIOConnection c) {
buffer = c.checkWriteBuffer(buffer, databaseData.length + 1, true);
BufferUtil.writeWithNull(buffer, databaseData);
}
if ((clientFlags & CLIENT_PLUGIN_AUTH) != 0) {
//if use the mysql_native_password is used for auth this need be replay
BufferUtil.writeWithNull(buffer, "mysql_native_password".getBytes());
}

c.write(buffer);
}

Expand Down Expand Up @@ -218,4 +231,8 @@ public String getDatabase() {
public void setDatabase(String database) {
this.database = database;
}

public String getAuthPlugin() {
return authPlugin;
}
}
Loading

0 comments on commit 0a8ba1a

Please sign in to comment.