matchCtx added

This commit is contained in:
crusader
2018-09-03 22:36:57 +09:00
parent 3e7b2fda79
commit c15fd7d46e
49 changed files with 505 additions and 542 deletions

View File

@@ -11,37 +11,32 @@ import (
type MySqlMatcher struct {
osm.Matchers
meta osm.Metadata
}
func (m *MySqlMatcher) Key() string {
return "MYSQL"
}
func (m *MySqlMatcher) Name() string {
func (m *MySqlMatcher) Name(matchCtx *osm.MatchCtx) string {
name := "MySQL"
if v, ok := m.meta["version"]; ok {
if strings.Contains(v, "MariaDB") {
if v, ok := matchCtx.GetAttribute("version"); ok {
if strings.Contains(v.(string), "MariaDB") {
name = "MariaDB"
}
name = name + " (" + v + ")"
name = name + " (" + v.(string) + ")"
}
return name
}
func (m *MySqlMatcher) Meta() osm.Metadata {
return m.meta
}
func (m *MySqlMatcher) IsPrePacket() bool {
func (m *MySqlMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool {
return true
}
func (m *MySqlMatcher) HasResponse(index int) bool {
func (m *MySqlMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *MySqlMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
func (m *MySqlMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
@@ -55,7 +50,7 @@ type serverSettings struct {
keepalive int64
}
func (m *MySqlMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
func (m *MySqlMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
@@ -76,7 +71,7 @@ func (m *MySqlMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet)
if err != nil {
return osm.NotMatchedError()
}
m.meta["version"] = string(slice)
matchCtx.SetAttribute("version", string(slice))
pos += len(slice) + 1
p.threadID = bytesToUint32(packet.Buffer[pos : pos+4])
@@ -105,6 +100,6 @@ func bytesToUint32(b []byte) (n uint32) {
func NewMatcher() osm.Matcher {
m := &MySqlMatcher{}
m.meta = osm.NewMetadata()
return m
}

View File

@@ -11,10 +11,14 @@ func TestMySql(t *testing.T) {
m := NewMatcher()
conn, _ := net.Dial("tcp", "192.168.1.201:23306")
conn, err := net.Dial("tcp", "192.168.1.201:23306")
if err != nil {
t.Errorf("ERR %s", err)
}
defer conn.Close()
matchCtx := osm.NewMatchCtx("192.168.1.201", 23306)
bytes := make([]byte, 1024)
n, _ := conn.Read(bytes)
p := osm.NewPacket(bytes, n)
@@ -22,7 +26,7 @@ func TestMySql(t *testing.T) {
if err := m.Match(nil, 0, p); err != nil {
t.Error(err)
}
t.Log(m.Name())
t.Log(m.Meta())
t.Log(m.Name(matchCtx))
t.Log(matchCtx)
}