overflow_discovery/service/matcher/redis/redis.go

70 lines
1.1 KiB
Go
Raw Normal View History

2017-11-21 12:47:55 +00:00
package redis
import (
"strings"
"git.loafle.net/overflow/overflow_discovery/service/matcher"
)
const REDIS_PING string = "*1\r\n$4\r\nping\r\n"
type RedisMatcher struct {
matcher.Matchers
}
func (t *RedisMatcher) ServiceName() string {
return "Redis"
}
func (t *RedisMatcher) IsPrePacket() bool {
return false
}
func (t *RedisMatcher) HasResponse(index int) bool {
return true
}
func (t *RedisMatcher) IsError(info matcher.MatchInfo, index int, packet *matcher.Packet) bool {
return false
}
func (t *RedisMatcher) Match(info matcher.MatchInfo, index int, packet *matcher.Packet) bool {
if packet == nil {
return false
}
resp := strings.Split(string(packet.Buffer), "\r\n")[0]
if len(resp) <= 0 {
return false
}
sign := string([]rune(resp)[0])
if len(sign) <= 0 {
return false
}
if sign == "+" {
if resp == "+PONG" || resp == "+OK" {
return true
}
}
if sign == "-" {
if resp == "-NOAUTH" || resp == "-ERR" {
return true
}
}
return false
}
func NewMatcher() matcher.Matcher {
m := &RedisMatcher{}
m.AddPacket(matcher.NewPacket([]byte(REDIS_PING), len(REDIS_PING)))
return m
}