2018-08-13 07:48:32 +00:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
const REDIS_PING string = "*1\r\n$4\r\nPING\r\n"
|
|
|
|
const REDIS_INFO string = "*1\r\n$4\r\nINFO\r\n"
|
|
|
|
const REDIS_QUIT string = "*1\r\n$4\r\nQUIT\r\n"
|
|
|
|
|
|
|
|
type RedisMatcher struct {
|
2018-08-15 07:17:18 +00:00
|
|
|
osm.Matchers
|
|
|
|
meta osm.Metadata
|
2018-08-13 07:48:32 +00:00
|
|
|
|
|
|
|
protected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) Key() string {
|
|
|
|
return "REDIS"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) Name() string {
|
|
|
|
name := "Redis"
|
|
|
|
if r.protected {
|
|
|
|
return name + " (protected)"
|
|
|
|
}
|
|
|
|
if v, ok := r.meta["redis_mode"]; ok {
|
|
|
|
name = name + " " + v
|
|
|
|
}
|
|
|
|
if v, ok := r.meta["redis_version"]; ok {
|
|
|
|
name = name + " (" + v + ")"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
func (r *RedisMatcher) Meta() osm.Metadata {
|
2018-08-13 07:48:32 +00:00
|
|
|
return r.meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) IsPrePacket() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) HasResponse(index int) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
func (r *RedisMatcher) IsError(info osm.MatchInfo, index int, packet *osm.Packet) bool {
|
2018-08-13 07:48:32 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
func (r *RedisMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error {
|
2018-08-13 07:48:32 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-03 06:42:56 +00:00
|
|
|
resp := strings.Split(string(packet.Bytes()), "\r\n")[0]
|
2018-08-13 07:48:32 +00:00
|
|
|
if len(resp) <= 0 {
|
2018-08-15 07:17:18 +00:00
|
|
|
return osm.NotMatchedError()
|
2018-08-13 07:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch index {
|
|
|
|
case 0:
|
|
|
|
sign := string([]rune(resp)[0])
|
|
|
|
if len(sign) <= 0 {
|
2018-08-15 07:17:18 +00:00
|
|
|
return osm.NotMatchedError()
|
2018-08-13 07:48:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sign == "+" {
|
|
|
|
if resp == "+PONG" || resp == "+OK" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sign == "-" {
|
|
|
|
if resp == "-NOAUTH" || resp == "-ERR" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.protected = r.checkProtectedMode(packet)
|
|
|
|
if r.protected {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1: // INFO
|
|
|
|
|
2018-09-03 06:42:56 +00:00
|
|
|
info := string(packet.Bytes())
|
2018-08-13 07:48:32 +00:00
|
|
|
if !r.protected {
|
|
|
|
r.parseInfo(info)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case 2:
|
|
|
|
sign := string([]rune(resp)[0])
|
|
|
|
if sign == "+" || sign == "-" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
2018-08-15 07:17:18 +00:00
|
|
|
return osm.NotMatchedError()
|
2018-08-13 07:48:32 +00:00
|
|
|
}
|
2018-08-15 07:17:18 +00:00
|
|
|
return osm.NotMatchedError()
|
2018-08-13 07:48:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
func (r *RedisMatcher) checkProtectedMode(packet *osm.Packet) bool {
|
2018-08-13 07:48:32 +00:00
|
|
|
var (
|
|
|
|
compareSign = "-"
|
|
|
|
compareMsg = "DENIED"
|
|
|
|
)
|
2018-09-03 06:42:56 +00:00
|
|
|
str := string(packet.Bytes())
|
2018-08-13 07:48:32 +00:00
|
|
|
|
|
|
|
if str == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if 0 >= len(str) || len(compareMsg)+2 > len(str) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
firstCompare := str[0:1]
|
|
|
|
seconcdCompare := str[1 : len(compareMsg)+1]
|
|
|
|
|
|
|
|
if firstCompare != compareSign {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if seconcdCompare != compareMsg {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
r.protected = true
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) parseInfo(info string) {
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(info))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.Compare(line, "") == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(line) > 0 && strings.Contains(line, ":") {
|
|
|
|
kv := strings.Split(line, ":")
|
|
|
|
if len(kv[0]) > 0 && len(kv[1]) > 0 {
|
|
|
|
r.meta[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisMatcher) PacketCount() int {
|
|
|
|
if r.protected {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
func NewMatcher() osm.Matcher {
|
2018-08-13 07:48:32 +00:00
|
|
|
|
|
|
|
m := &RedisMatcher{}
|
2018-08-15 07:17:18 +00:00
|
|
|
m.meta = osm.NewMetadata()
|
2018-08-13 07:48:32 +00:00
|
|
|
|
2018-08-15 07:17:18 +00:00
|
|
|
m.AddPacket(osm.NewPacket([]byte(REDIS_PING), len(REDIS_PING)))
|
|
|
|
m.AddPacket(osm.NewPacket([]byte(REDIS_INFO), len(REDIS_INFO)))
|
|
|
|
m.AddPacket(osm.NewPacket([]byte(REDIS_QUIT), len(REDIS_QUIT)))
|
2018-08-13 07:48:32 +00:00
|
|
|
return m
|
|
|
|
}
|