Explorar el Código

Misc. Cleanup from IDE

Josh Brickner hace 6 años
padre
commit
fb6cd3c05c
Se han modificado 8 ficheros con 31 adiciones y 30 borrados
  1. 2 0
      .gitignore
  2. 3 3
      binlog/authentication.go
  3. 5 5
      binlog/binlog.go
  4. 7 7
      binlog/commands.go
  5. 9 12
      binlog/connection.go
  6. 1 1
      config.json
  7. 3 0
      go.mod
  8. 1 2
      main.go

+ 2 - 0
.gitignore

@@ -2,3 +2,5 @@ tags
 .idea
 .DS_Store
 mysql-binlog-filter
+
+!.idea

+ 3 - 3
binlog/authentication.go

@@ -6,9 +6,9 @@ import (
 	"crypto/sha256"
 )
 
-const SHA2_REQUEST_PUBLIC_KEY = 0x02
-const SHA2_FAST_AUTH_SUCCESS = 0x03
-const SHA2_PERFORM_FULL_AUTHENTICATION = 0x04
+const Sha2RequestPublicKey = 0x02
+const Sha2FastAuthSuccess = 0x03
+const Sha2PerformFullAuthentication = 0x04
 
 type AuthMoreDataPacket struct {
 	*PacketHeader

+ 5 - 5
binlog/binlog.go

@@ -3,8 +3,8 @@ package binlog
 import "fmt"
 
 func (c *Conn) registerAsSlave() error {
-	brsc := &BinlogRegisterSlaveCommand{
-		Status:   COMMAND_REGISTER_SLAVE,
+	brsc := &RegisterSlaveCommand{
+		Status:   CommandRegisterSlave,
 		ServerId: c.Config.ServerId,
 		Hostname: "",
 		User:     "",
@@ -18,10 +18,10 @@ func (c *Conn) registerAsSlave() error {
 }
 
 func (c *Conn) startBinlogStream() error {
-	bldc := &BinlogDumpCommand{
-		Status:   COMMAND_BIN_LOG_DUMP,
+	bldc := &DumpCommand{
+		Status:   CommandBinLogDump,
 		Position: 120,
-		Flags:    BINLOG_DUMP_NON_BLOCK,
+		Flags:    DumpNonBlock,
 		ServerId: c.Config.ServerId,
 		Filename: c.Config.BinlogFile,
 	}

+ 7 - 7
binlog/commands.go

@@ -1,11 +1,11 @@
 package binlog
 
-const BINLOG_DUMP_NON_BLOCK = 0x00 // Set to 0 because we do want the binlog to block.
+const DumpNonBlock = 0x00 // Set to 0 because we do want the binlog to block.
 
-const COMMAND_REGISTER_SLAVE = 0x15
-const COMMAND_BIN_LOG_DUMP = 0x12
+const CommandRegisterSlave = 0x15
+const CommandBinLogDump = 0x12
 
-type BinlogRegisterSlaveCommand struct {
+type RegisterSlaveCommand struct {
 	Status   uint64
 	ServerId uint64
 	Hostname string // Length Encoded
@@ -16,7 +16,7 @@ type BinlogRegisterSlaveCommand struct {
 	MasterId uint64
 }
 
-func (c *Conn) writeBinlogRegisterSlaveCommand(brsc *BinlogRegisterSlaveCommand) error {
+func (c *Conn) writeBinlogRegisterSlaveCommand(brsc *RegisterSlaveCommand) error {
 	c.putInt(TypeFixedInt, brsc.Status, 1)
 	c.putInt(TypeFixedInt, brsc.ServerId, 4)
 	c.putString(TypeLenEncString, brsc.Hostname)
@@ -33,7 +33,7 @@ func (c *Conn) writeBinlogRegisterSlaveCommand(brsc *BinlogRegisterSlaveCommand)
 	return nil
 }
 
-type BinlogDumpCommand struct {
+type DumpCommand struct {
 	Status   uint64
 	Position uint64
 	Flags    uint64
@@ -41,7 +41,7 @@ type BinlogDumpCommand struct {
 	Filename string
 }
 
-func (c *Conn) writeBinlogDumpCommand(bldc *BinlogDumpCommand) error {
+func (c *Conn) writeBinlogDumpCommand(bldc *DumpCommand) error {
 	c.putInt(TypeFixedInt, bldc.Status, 1)
 	c.putInt(TypeFixedInt, bldc.Position, 4)
 	c.putInt(TypeFixedInt, bldc.Flags, 2)

+ 9 - 12
binlog/connection.go

@@ -20,8 +20,6 @@ import (
 // Misc. Constants
 const NullByte byte = 0
 
-var EOF = bytes.NewBuffer([]byte{NullByte})
-
 const MaxPacketSize = MaxUint16
 
 // MySQL Packet Data Types
@@ -219,9 +217,9 @@ func (c *Conn) readPacket() (interface{}, error) {
 		}
 
 		switch res.Data {
-		case SHA2_FAST_AUTH_SUCCESS:
-		case SHA2_REQUEST_PUBLIC_KEY:
-		case SHA2_PERFORM_FULL_AUTHENTICATION:
+		case Sha2FastAuthSuccess:
+		case Sha2RequestPublicKey:
+		case Sha2PerformFullAuthentication:
 			c.putBytes(append([]byte(c.Config.Pass), NullByte))
 			if c.Flush() != nil {
 				return nil, c.Flush()
@@ -241,7 +239,7 @@ func (c *Conn) readPacket() (interface{}, error) {
 		}
 
 		err = fmt.Errorf(
-			"Error %d: %s",
+			"error %d: %s",
 			res.(*ErrorPacket).ErrorCode,
 			res.(*ErrorPacket).ErrorMessage,
 		)
@@ -317,7 +315,7 @@ func (c *Conn) getBytesUntilNull() *bytes.Buffer {
 			break
 		}
 
-		s = c.readBytes(uint64(l))
+		s = c.readBytes(l)
 		b = append(b, s.Bytes()...)
 	}
 
@@ -460,7 +458,7 @@ func (c *Conn) encLenEncInt(v uint64) []byte {
 	case v >= MaxUint24 && v < MaxUint64:
 		prefix[0] = 0xFE
 		b = make([]byte, 9)
-		binary.LittleEndian.PutUint64(b, uint64(v))
+		binary.LittleEndian.PutUint64(b, v)
 	}
 
 	if len(b) > 1 {
@@ -635,12 +633,11 @@ func (c *Conn) Flush() error {
 
 func (c *Conn) addHeader() *bytes.Buffer {
 	pl := uint64(c.writeBuf.Len())
-	sId := uint64(c.sequenceId)
+	sId := c.sequenceId
 	c.sequenceId++
 
-	plB := c.encFixedLenInt(pl, 3)
-	sIdB := c.encFixedLenInt(sId, 1)
-
+	var plB = c.encFixedLenInt(pl, 3)
+	var sIdB = c.encFixedLenInt(sId, 1)
 	return bytes.NewBuffer(append(append(plB, sIdB...), c.writeBuf.Bytes()...))
 }
 

+ 1 - 1
config.json

@@ -1,5 +1,5 @@
 {
-  "host": "127.0.0.1",
+  "host": "192.168.99.101",
   "port": 3316,
   "user": "root",
   "password": "root",

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module github.com/joshwbrick/mysql-binlog-filter
+
+go 1.12

+ 1 - 2
main.go

@@ -3,8 +3,7 @@ package main
 import (
 	"database/sql"
 	"fmt"
-
-	_ "github.com/macinjosh/mysql-binlog-filter/binlog"
+	_ "github.com/joshwbrick/mysql-binlog-filter/binlog"
 )
 
 func main() {