service_matcher-go/redis/redis.go
2018-09-03 22:36:57 +09:00

171 lines
3.3 KiB
Go

package redis
import (
"bufio"
"strings"
osm "git.loafle.net/overflow/service_matcher-go"
)
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 {
osm.Matchers
}
func (m *RedisMatcher) Key() string {
return "REDIS"
}
func (m *RedisMatcher) Name(matchCtx *osm.MatchCtx) string {
name := "Redis"
if v, ok := matchCtx.GetAttribute("protected"); ok {
if v.(bool) {
return name + " (protected)"
}
}
if v, ok := matchCtx.GetAttribute("redis_mode"); ok {
name = name + " " + v.(string)
}
if v, ok := matchCtx.GetAttribute("redis_version"); ok {
name = name + " (" + v.(string) + ")"
}
return name
}
func (m *RedisMatcher) IsPrePacket(matchCtx *osm.MatchCtx) bool {
return false
}
func (m *RedisMatcher) HasResponse(matchCtx *osm.MatchCtx, index int) bool {
return true
}
func (m *RedisMatcher) IsError(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) bool {
return false
}
func (m *RedisMatcher) Match(matchCtx *osm.MatchCtx, index int, packet *osm.Packet) error {
if packet == nil || !packet.Valid() {
return osm.NoPacketReceivedError()
}
resp := strings.Split(string(packet.Bytes()), "\r\n")[0]
if len(resp) <= 0 {
return osm.NotMatchedError()
}
switch index {
case 0:
sign := string([]rune(resp)[0])
if len(sign) <= 0 {
return osm.NotMatchedError()
}
if sign == "+" {
if resp == "+PONG" || resp == "+OK" {
return nil
}
}
if sign == "-" {
if resp == "-NOAUTH" || resp == "-ERR" {
return nil
}
}
protected := m.checkProtectedMode(packet)
matchCtx.SetAttribute("protected", protected)
if protected {
return nil
}
case 1: // INFO
response := string(packet.Bytes())
if v, ok := matchCtx.GetAttribute("protected"); ok {
if !v.(bool) {
m.parseResponse(matchCtx, response)
}
}
return nil
case 2:
sign := string([]rune(resp)[0])
if sign == "+" || sign == "-" {
return nil
}
return nil
default:
return osm.NotMatchedError()
}
return osm.NotMatchedError()
}
func (m *RedisMatcher) checkProtectedMode(packet *osm.Packet) bool {
var (
compareSign = "-"
compareMsg = "DENIED"
)
str := string(packet.Bytes())
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
}
return true
}
func (m *RedisMatcher) parseResponse(matchCtx *osm.MatchCtx, response string) {
scanner := bufio.NewScanner(strings.NewReader(response))
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 {
matchCtx.SetAttribute(kv[0], kv[1])
}
}
}
}
func (m *RedisMatcher) PacketCount(matchCtx *osm.MatchCtx) int {
if v, ok := matchCtx.GetAttribute("protected"); ok {
if v.(bool) {
return 1
}
}
return 3
}
func NewMatcher() osm.Matcher {
m := &RedisMatcher{}
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)))
return m
}