71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
|
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
|
||
|
}
|