proxymysql/app/mysqlserver/record_query.go

281 lines
5.4 KiB
Go
Raw Permalink Normal View History

2024-02-04 18:17:06 +08:00
package mysqlserver
import (
"bytes"
"io"
"proxymysql/app/conf"
"proxymysql/app/db"
"proxymysql/app/zlog"
"regexp"
"strings"
"time"
2024-03-11 12:09:59 +08:00
"github.com/huandu/go-sqlbuilder"
jsoniter "github.com/json-iterator/go"
2024-02-04 18:17:06 +08:00
)
var _ io.Writer = (*RecordQuery)(nil)
type RecordQuery struct {
pipeReader *io.PipeReader
pipeWriter *io.PipeWriter
stmtId uint32
stmtMap map[uint32]string
}
func NewRecordQuery() *RecordQuery {
r := &RecordQuery{}
r.pipeReader, r.pipeWriter = io.Pipe()
r.stmtMap = make(map[uint32]string)
go r.readQuery()
return r
}
func (r *RecordQuery) Write(p []byte) (n int, err error) {
r.pipeWriter.Write(p)
return len(p), err
}
func (r *RecordQuery) Close() error {
r.pipeWriter.Close()
r.pipeReader.Close()
return nil
}
func (r *RecordQuery) readQuery() {
for {
packet, err := ReadMysqlPacket(r.pipeReader)
if err != nil {
// errors.Is(err, io.ErrClosedPipe)
zlog.Errorf("read pipe pack err: %s", err)
return
}
if len(packet.Payload) < 2 {
continue
}
switch packet.Payload[0] {
case ComQuery:
query := string(packet.Payload[1:])
zlog.Debugf("query: %s\n", query)
2024-03-11 12:09:59 +08:00
r.saveToDb(query, false)
2024-02-04 18:17:06 +08:00
case ComPrepare:
query := string(packet.Payload[1:])
zlog.Debugf("prepare: %s\n", query)
r.stmtId++
r.stmtMap[r.stmtId] = query
2024-03-11 12:09:59 +08:00
r.saveToDb(query, true)
2024-02-04 18:17:06 +08:00
case ComStmtExecute:
query := r.stmtMap[r.stmtId]
_, args := r.parseStmtArgs(strings.Count(query, "?"), packet.Payload)
2024-03-11 12:09:59 +08:00
// fmt.Printf("ComStmtExecute: %s %+v\n", query, args)
2024-02-04 18:17:06 +08:00
fullSqlQuery, err := sqlbuilder.MySQL.Interpolate(query, args)
if err != nil {
zlog.Errorf("ComStmtExecute builder sql err: %s", err)
} else {
zlog.Debugf("stmt: %s\n", fullSqlQuery)
}
2024-03-11 12:09:59 +08:00
r.saveToDb(fullSqlQuery, false)
2024-02-04 18:17:06 +08:00
case ComStmtClose:
delete(r.stmtMap, r.stmtId)
}
}
}
type BindArg struct {
ArgType uint8
Unsigned uint8
ArgValue interface{}
}
func (r *RecordQuery) parseStmtArgs(argNum int, data []byte) ([]*BindArg, []any) {
if argNum == 0 {
return nil, nil
}
if len(data) == 0 {
return nil, nil
}
skipPos := 1 + 4 + 1 + 4
buf := bytes.NewBuffer(data)
2024-03-11 12:09:59 +08:00
// fmt.Printf("%+v\n", buf.Bytes())
2024-02-04 18:17:06 +08:00
buf.Next(skipPos)
nullBitMapLen := (argNum + 7) / 8
2024-03-11 12:09:59 +08:00
// fmt.Println(nullBitMapLen)
2024-02-04 18:17:06 +08:00
nullBitMap := buf.Next(nullBitMapLen)
2024-03-11 12:09:59 +08:00
// fmt.Println("nullBitMap", nullBitMap)
2024-02-04 18:17:06 +08:00
newParamsBindFlag := ReadByte(buf.Next(1))
2024-03-11 12:09:59 +08:00
// fmt.Println("newParamsBindFlag", ReadByte(newParamsBindFlag))
2024-02-04 18:17:06 +08:00
if newParamsBindFlag != 0x01 {
return nil, nil
}
bindArgs := make([]*BindArg, argNum)
args := make([]interface{}, argNum)
for i := 0; i < argNum; i++ {
filedType := ReadByte(buf.Next(1))
2024-03-11 12:09:59 +08:00
// fmt.Printf("filedType: %+v\n", filedType)
2024-02-04 18:17:06 +08:00
unsigned := ReadByte(buf.Next(1))
2024-03-11 12:09:59 +08:00
// fmt.Printf("unsigned: %+v\n", unsigned)
2024-02-04 18:17:06 +08:00
bindArgs[i] = &BindArg{
ArgType: filedType,
Unsigned: unsigned,
ArgValue: nil,
}
}
2024-03-11 12:09:59 +08:00
// fmt.Printf("val: %+v\n", buf.Bytes())
2024-02-04 18:17:06 +08:00
2024-03-11 12:09:59 +08:00
// fmt.Printf("%+v\n", nullBitMap)
2024-02-04 18:17:06 +08:00
for i := 0; i < argNum; i++ {
nullBytePos := i / 8
nullBitPos := i % 8
2024-03-11 12:09:59 +08:00
// fmt.Printf("nullBytePos: %08b\n", nullBitMap[nullBytePos])
// fmt.Printf("nullBitPos: %08b\n", 1<<nullBitPos)
2024-02-04 18:17:06 +08:00
if (nullBitMap[nullBytePos] & (1 << nullBitPos)) > 0 {
2024-03-11 12:09:59 +08:00
// buf.Next(1)
2024-02-04 18:17:06 +08:00
bindArgs[i].ArgValue = nil
args[i] = nil
2024-03-11 12:09:59 +08:00
// fmt.Printf("%+v\n", bindArgs[i])
2024-02-04 18:17:06 +08:00
continue
}
switch bindArgs[i].ArgType {
case FieldTypeTiny, FieldTypeBit:
val := ReadByte(buf.Next(1))
bindArgs[i].ArgValue = val
args[i] = val
case FieldTypeInt24, FieldTypeLong:
val := ReadUint32(buf.Next(4))
bindArgs[i].ArgValue = val
args[i] = val
case FieldTypeLongLong:
val := ReadUint64(buf.Next(8))
bindArgs[i].ArgValue = val
args[i] = val
default:
length, pos, ok := ReadLengthEncodedInt(buf.Bytes())
if !ok {
zlog.Errorf("read args err %+v", buf.Bytes())
continue
}
buf.Next(pos)
val := string(buf.Next(int(length)))
bindArgs[i].ArgValue = val
args[i] = val
2024-03-11 12:09:59 +08:00
// fmt.Printf("str: %s\n", val)
2024-02-04 18:17:06 +08:00
}
}
return bindArgs, args
}
type SqlComment struct {
AdminId int64
AdminName string
AdminRealName string
QueryGameId int32
HeaderGameId int32
Ip string
RequestPath string
RequestInfo string
UnixMilli int64
Query string
CallInfo string
CreateTime string
2024-03-11 12:09:59 +08:00
IsPrepare int32
2024-02-04 18:17:06 +08:00
}
var adminCommentReg = regexp.MustCompile(`/\*\s+TzAdmin-([\s\S]+)-TzAdmin\s+\*/`)
func (r *RecordQuery) parseSqlComment(query string) (sc *SqlComment) {
sc = &SqlComment{}
sc.Query = query
if sc.UnixMilli == 0 {
sc.UnixMilli = time.Now().UnixMilli()
}
sc.CreateTime = time.Now().Format("2006-01-02 15:04:05.000")
if !strings.Contains(query, " TzAdmin-") {
return
}
subMatch := adminCommentReg.FindStringSubmatch(query)
if len(subMatch) >= 2 {
err := jsoniter.Unmarshal([]byte(subMatch[1]), sc)
if err != nil {
zlog.Warnf("解析sql admin信息失败 %s [%s]", err, subMatch[1])
return
}
_sql := strings.TrimSpace(adminCommentReg.ReplaceAllString(sc.Query, ""))
sc.Query = _sql
2024-03-11 12:09:59 +08:00
// zlog.Infof("%+v\n", sc)
2024-02-04 18:17:06 +08:00
}
return
}
2024-03-11 12:09:59 +08:00
func (r *RecordQuery) saveToDb(query string, isPrepare bool) {
2024-02-04 18:17:06 +08:00
if conf.App.SaveLog == false {
return
}
sc := r.parseSqlComment(query)
2024-03-11 12:09:59 +08:00
if isPrepare {
sc.IsPrepare = 1
}
// sc.Query = base64.StdEncoding.EncodeToString([]byte(sc.Query))
2024-02-04 18:17:06 +08:00
err := db.GetDB().Table("sql_query_log").Create(sc).Error
if err != nil {
zlog.Errorf("save to db err: %s", err)
return
}
}