network_service_matcher/redis/redis.go

71 lines
1.1 KiB
Go
Raw Normal View History

2017-12-04 07:27:08 +00:00
package redis
import (
"strings"
cnsm "git.loafle.net/commons_go/network_service_matcher"
)
const REDIS_PING string = "*1\r\n$4\r\nping\r\n"
type RedisMatcher struct {
cnsm.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 cnsm.MatchInfo, index int, packet *cnsm.Packet) bool {
return false
}
func (t *RedisMatcher) Match(info cnsm.MatchInfo, index int, packet *cnsm.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() cnsm.Matcher {
m := &RedisMatcher{}
m.AddPacket(cnsm.NewPacket([]byte(REDIS_PING), len(REDIS_PING)))
return m
}