package redis

import (
	"git.loafle.net/overflow/overflow_probe/matcher/packet"
	"git.loafle.net/overflow/overflow_probe/model/scaninfo"
)

const (
	COMPARE_STR_1 = "-"
	COMPARE_STR_2 = "DENIED"
)

type RedisProtectedMatcher struct {
}

func NewRedisProtectedMatcher() *RedisProtectedMatcher {

	redisMatcher := &RedisProtectedMatcher{}

	return redisMatcher
}

func (r *RedisProtectedMatcher) ServiceName() string {
	return "RedisProtectedMatcher"
}

func (r *RedisProtectedMatcher) PacketCount() int {
	return 0
}
func (r *RedisProtectedMatcher) Packet(index int) *packet.Packet {
	return nil
}

func (r *RedisProtectedMatcher) IsError(index int, packet *packet.Packet, info scaninfo.ServiceScanInfo) bool {
	return false
}

func (r *RedisProtectedMatcher) IsNoResponse(index int) bool {
	return false
}

func (r *RedisProtectedMatcher) IsPrePacket() bool {
	return true
}

func (r *RedisProtectedMatcher) Match(index int, packet *packet.Packet, info scaninfo.ServiceScanInfo) bool {

	if packet == nil {
		return false
	}

	switch index {
	case 0:
		str := string(packet.Buffer[:packet.Len])

		if str == "" {
			return false
		}
		if len(str) <= 0 {
			return false
		}

		firstCompare := str[0:1]
		seconcdCompare := str[1 : len(COMPARE_STR_2)+1]

		if firstCompare != COMPARE_STR_1 {
			return false
		}
		if seconcdCompare != COMPARE_STR_2 {
			return false
		}

		return true
	}

	return false

}
