1
0
Prechádzať zdrojové kódy

Continued work on handshake response.

Josh Brickner 7 rokov pred
rodič
commit
0e83f45883
4 zmenil súbory, kde vykonal 42 pridanie a 10 odobranie
  1. 28 0
      binlog/authentication.go
  2. 2 1
      binlog/connection.go
  3. 11 8
      binlog/handshake.go
  4. 1 1
      main.go

+ 28 - 0
binlog/authentication.go

@@ -0,0 +1,28 @@
+package binlog
+
+import (
+	"crypto/sha256"
+)
+
+func (c *Conn) cachingSha2Auth(salt []byte, password []byte) []byte {
+	if len(password) < 1 {
+		return nil
+	}
+
+	pHash := c.sha256Hash(password)
+	pHashHash := c.sha256Hash(pHash)
+	pHashHashHash := c.sha256Hash(pHashHash)
+	authData := c.sha256Hash(append(pHashHashHash, salt...))
+
+	for i := range pHash {
+		pHash[i] ^= authData[i]
+	}
+
+	return pHash
+}
+
+func (c *Conn) sha256Hash(word []byte) []byte {
+	s := sha256.New()
+	s.Write(word)
+	return s.Sum(nil)
+}

+ 2 - 1
binlog/connection.go

@@ -18,6 +18,7 @@ const MaxPacketSize = 16777216
 const TypeNullTerminatedString = int(0)
 const TypeFixedString = int(1)
 const TypeFixedInt = int(2)
+const TypeLenEncodedInt = int(3)
 
 type Config struct {
 	Host       string `json:"host"`
@@ -90,7 +91,7 @@ func (d Driver) Open(dsn string) (driver.Conn, error) {
 	blConn.Handshake = hsp
 
 	resp := blConn.handshakeResponse()
-	b := resp.encode()
+	b := resp.encode(&blConn)
 	fmt.Printf("%d", b)
 	_, err = blConn.tcpConn.Write(b)
 

+ 11 - 8
binlog/handshake.go

@@ -221,7 +221,7 @@ type HandshakeResponse struct {
 	KeyValues          map[string]string
 }
 
-func (hr *HandshakeResponse) encode() []byte {
+func (hr *HandshakeResponse) encode(c *Conn) []byte {
 	buf := bytes.NewBuffer(make([]byte, 0))
 
 	// Capabilities flag.
@@ -358,17 +358,20 @@ func (hr *HandshakeResponse) encode() []byte {
 	u := append([]byte(hr.Username), NullByte)
 	buf.Write(u)
 
-	if hr.ClientFlag.PluginAuth && hr.AuthResponseLength > 0 {
-		pal := make([]byte, 2)
-		binary.LittleEndian.PutUint16(pal, uint16(hr.AuthResponseLength))
-		buf.Write(pal[:1])
-		buf.Write([]byte(hr.AuthResponse))
+	if hr.ClientFlag.PluginAuthLenEncClientData {
+
 	}
 
+	// Write database name
 	if hr.ClientFlag.ConnectWithDb {
 		buf.Write(append([]byte(hr.Database), NullByte))
 	}
 
+	// Write auth plugin
+	if hr.ClientFlag.PluginAuth {
+		buf.Write([]byte(hr.ClientPluginName))
+	}
+
 	pl := make([]byte, 4)
 	binary.LittleEndian.PutUint32(pl, uint32(buf.Len()))
 	p := append(pl[:3], 1)
@@ -413,8 +416,8 @@ func (c *Conn) handshakeResponse() *HandshakeResponse {
 		MaxPacketSize:      MaxPacketSize,
 		CharacterSet:       45,
 		Username:           c.Config.User,
-		AuthResponseLength: 5,
-		AuthResponse:       "Hello",
+		AuthResponseLength: 4,
+		AuthResponse:       "root",
 		Database:           c.Config.Database,
 		ClientPluginName:   c.Handshake.AuthPluginName,
 		KeyValues:          nil,

+ 1 - 1
main.go

@@ -7,7 +7,7 @@ import (
 )
 
 func main() {
-	conn, err := sql.Open("mysql-binlog", "{\"host\": \"127.0.0.1\", \"port\": 3306, \"user\": \"root\", \"password\": \"root\", \"database\": \"test\", \"ssl\": false}")
+	conn, err := sql.Open("mysql-binlog", "{\"host\": \"127.0.0.1\", \"port\": 3306, \"user\": \"root\", \"password\": \"root\", \"database\": \"information_schema\", \"ssl\": false}")
 	if err != nil {
 		fmt.Printf("Open Error: %+v\n", err)
 	}