service_matcher-go/mysql/mysql.go

141 lines
2.7 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package mysql
import (
"bytes"
"encoding/binary"
"io"
"strings"
2018-08-15 07:17:18 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
)
type MySqlMatcher struct {
2018-08-15 07:17:18 +00:00
osm.Matchers
2018-08-13 07:48:32 +00:00
}
2018-10-23 04:31:25 +00:00
func (m *MySqlMatcher) Key(matchCtx *osm.MatchCtx) string {
v := "MYSQL"
if _v, ok := matchCtx.GetAttribute("version"); ok {
if strings.Contains(_v, "MariaDB") {
v = "MARIADB"
}
}
return v
2018-08-13 07:48:32 +00:00
}
2018-10-23 04:31:25 +00:00
func (m *MySqlMatcher) Type(matchCtx *osm.MatchCtx) string {
2018-09-12 04:26:27 +00:00
return "DATABASE"
}
func (m *MySqlMatcher) Vendor(matchCtx *osm.MatchCtx) string {
2018-10-23 04:12:30 +00:00
v := "Oracle"
if _v, ok := matchCtx.GetAttribute("version"); ok {
if strings.Contains(_v, "MariaDB") {
v = "MariaDB"
}
}
return v
}
func (m *MySqlMatcher) Version(matchCtx *osm.MatchCtx) string {
2018-10-23 04:12:30 +00:00
v := "UNKNOWN"
if _v, ok := matchCtx.GetAttribute("version"); ok {
v = _v
}
return v
}
func (m *MySqlMatcher) OsType(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
func (m *MySqlMatcher) OsVersion(matchCtx *osm.MatchCtx) string {
return "UNKNOWN"
}
2018-09-03 13:36:57 +00:00
func (m *MySqlMatcher) Name(matchCtx *osm.MatchCtx) string {
2018-08-13 07:48:32 +00:00
name := "MySQL"
2018-09-03 13:36:57 +00:00
if v, ok := matchCtx.GetAttribute("version"); ok {
2018-09-03 13:52:01 +00:00
if strings.Contains(v, "MariaDB") {
2018-08-13 07:48:32 +00:00
name = "MariaDB"
}
}
return name
}
2018-09-03 13:41:28 +00:00
func (m *MySqlMatcher) IsPrePacket() bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *MySqlMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
2018-08-13 07:48:32 +00:00
return true
}
2018-09-03 13:36:57 +00:00
func (m *MySqlMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
2018-08-13 07:48:32 +00:00
return false
}
type serverSettings struct {
protocol byte
version string
flags uint32
charset uint8
scrambleBuff []byte
threadID uint32
keepalive int64
}
2018-09-03 13:36:57 +00:00
func (m *MySqlMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
2018-09-03 06:42:56 +00:00
if packet == nil || !packet.Valid() {
2018-08-15 07:17:18 +00:00
return osm.NoPacketReceivedError()
2018-08-13 07:48:32 +00:00
}
buf := bytes.NewBuffer(packet.Buffer[:3])
packetLen, _ := binary.ReadUvarint(buf)
if packetLen != uint64(packet.Len-4) {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
pos := 4
p := new(serverSettings)
p.protocol = packet.Buffer[pos]
if p.protocol != 9 && p.protocol != 10 {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
pos++
slice, err := readSlice(packet.Buffer[pos:], 0x00)
if err != nil {
2018-08-15 07:17:18 +00:00
return osm.NotMatchedError()
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
matchCtx.SetAttribute("version", string(slice))
2018-08-13 07:48:32 +00:00
pos += len(slice) + 1
p.threadID = bytesToUint32(packet.Buffer[pos : pos+4])
pos += 4
return nil
}
func readSlice(data []byte, delim byte) (slice []byte, e error) {
pos := bytes.IndexByte(data, delim)
if pos > -1 {
slice = data[:pos]
} else {
slice = data
e = io.EOF
}
return
}
func bytesToUint32(b []byte) (n uint32) {
for i := uint8(0); i < 4; i++ {
n |= uint32(b[i]) << (i * 8)
}
return
}
2018-08-15 07:17:18 +00:00
func NewMatcher() osm.Matcher {
2018-08-13 07:48:32 +00:00
m := &MySqlMatcher{}
2018-09-03 13:36:57 +00:00
2018-08-13 07:48:32 +00:00
return m
}