2020-12-13 04:01:04 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis/v8"
|
2020-12-28 01:34:26 +01:00
|
|
|
"github.com/outout14/sacrebleu-api/api/types"
|
2020-12-13 04:01:04 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-12-22 04:12:02 +01:00
|
|
|
//GetRecord : Check the SQL and REDIS database for a Record.
|
2020-12-14 23:20:24 +01:00
|
|
|
//A Record struct is used as input and output
|
2020-12-28 01:34:26 +01:00
|
|
|
func GetRecord(entry types.Record) []types.Record {
|
2020-12-13 04:01:04 +01:00
|
|
|
//Check for strict record in Redis cache
|
|
|
|
redisKey := entry.Fqdn + "--" + fmt.Sprint(entry.Qtype)
|
|
|
|
result, redisErr := redisCheckForRecord(redisKey, entry)
|
|
|
|
|
2020-12-23 01:20:06 +01:00
|
|
|
var sqlErr bool //The err returned for sqlCheckForRecord or sqlCheckForReverse6Wildcard
|
2020-12-13 04:01:04 +01:00
|
|
|
|
|
|
|
//If reverse DNS
|
|
|
|
reverseCheck := IsReverse(entry.Fqdn)
|
|
|
|
if reverseCheck > 0 {
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//If reverse record not found in redis
|
2020-12-13 04:01:04 +01:00
|
|
|
if redisErr == redis.Nil {
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check for it in the SQL database
|
2020-12-13 04:01:04 +01:00
|
|
|
logrus.Debug("QUERIES : Check for strict reverse in MySQL")
|
|
|
|
result, sqlErr = sqlCheckForRecord(redisKey, entry.Fqdn, entry)
|
2020-12-23 01:20:06 +01:00
|
|
|
if sqlErr {
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check for wildcard reverse in the SQL
|
2020-12-13 04:01:04 +01:00
|
|
|
logrus.Debug("QUERIES : Check for wildcard reverse in MySQL")
|
2020-12-28 00:05:16 +01:00
|
|
|
result = sqlCheckForReverse6Wildcard(redisKey, entry.Fqdn, entry)
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//For dynamic reverse dns
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check for it by looking for a "%s" in the record content
|
|
|
|
//If true, replace it with the formated IP
|
2020-12-28 00:05:16 +01:00
|
|
|
for _, r := range result {
|
|
|
|
if strings.Contains(r.Content, "%s") {
|
|
|
|
record := ExtractAddressFromReverse(entry.Fqdn)
|
|
|
|
var recordFormated string
|
|
|
|
if reverseCheck == 1 {
|
|
|
|
recordFormated = strings.ReplaceAll(record, ".", "-")
|
|
|
|
} else {
|
|
|
|
recordFormated = strings.ReplaceAll(record, ":", "-")
|
|
|
|
}
|
|
|
|
r.Content = fmt.Sprintf(r.Content, recordFormated)
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 00:05:16 +01:00
|
|
|
|
2020-12-13 04:01:04 +01:00
|
|
|
} else if redisErr == redis.Nil { //If strict record NOT in Redis cache & not Reverse
|
|
|
|
//Check for wildcard in Redis cache
|
|
|
|
logrus.Debug("QUERIES : Check for wildcard in redis cache")
|
|
|
|
mainDomainKey := fmt.Sprintf("*%s", entry.Fqdn[strings.Index(entry.Fqdn, "."):]) //Remove the last subdomain
|
|
|
|
redismdKey := fmt.Sprintf("%s--%v", mainDomainKey, entry.Qtype)
|
|
|
|
result, redisErr = redisCheckForRecord(redismdKey, entry)
|
|
|
|
//If none of both check in mysql
|
|
|
|
if redisErr == redis.Nil {
|
|
|
|
//Check for strict record in mysql
|
|
|
|
logrus.Debug("QUERIES : Check for strict record in MSQL")
|
|
|
|
result, sqlErr = sqlCheckForRecord(redisKey, entry.Fqdn, entry)
|
2020-12-23 01:20:06 +01:00
|
|
|
if sqlErr {
|
2020-12-13 04:01:04 +01:00
|
|
|
//Check for wildcard record in mysql
|
|
|
|
logrus.Debug("QUERIES : Check for wildcard in MSQL")
|
2020-12-13 19:43:31 +01:00
|
|
|
result, _ = sqlCheckForRecord(redismdKey, fmt.Sprint(mainDomainKey), entry)
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|