sacrebleu-dns/utils/sql.go

85 lines
2.5 KiB
Go
Raw Normal View History

2020-12-13 03:01:04 +00:00
package utils
import (
"database/sql"
"fmt"
"time"
"github.com/go-redis/redis"
_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
)
2020-12-14 22:20:24 +00:00
//SQL database as global var
2020-12-13 03:01:04 +00:00
var DB *sql.DB
2020-12-14 22:20:24 +00:00
//Initialize the (My)SQL Database
//Requires a conf struct
2020-12-13 03:01:04 +00:00
func SqlDatabase(conf *Conf) {
logrus.WithFields(logrus.Fields{"database": conf.Database.Db}).Infof("SQL : Connection to DB")
2020-12-14 22:20:24 +00:00
//Connect to the Database
2020-12-13 03:01:04 +00:00
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", conf.Database.Username, conf.Database.Password, conf.Database.Ip, conf.Database.Port, conf.Database.Db))
CheckErr(err)
DB = db
2020-12-14 22:20:24 +00:00
SqlTest() //Test SQL connexion
2020-12-13 03:01:04 +00:00
}
2020-12-14 22:20:24 +00:00
//Test the SQL connexion by selecting all records from the database
2020-12-13 03:01:04 +00:00
func SqlTest() {
2020-12-14 01:15:43 +00:00
_, err := DB.Query("SELECT name, content FROM records")
2020-12-14 22:20:24 +00:00
CheckErr(err) //Panic if any error
2020-12-13 03:01:04 +00:00
}
2020-12-14 22:20:24 +00:00
//Check for a record in the SQL database
2020-12-13 03:01:04 +00:00
func sqlCheckForRecord(redisKey string, dKey string, entry Record) (Record, int) {
dbg := DB.QueryRow(
2020-12-13 18:30:29 +00:00
"SELECT id, content, ttl FROM records WHERE `name` = ? AND `type` = ?;", dKey, entry.Qtype).Scan(
2020-12-13 03:01:04 +00:00
&entry.Id,
&entry.Content,
&entry.TTL,
)
2020-12-14 22:20:24 +00:00
if dbg != nil { //If any err
2020-12-13 03:01:04 +00:00
logrus.Debugf("SQL : %v", dbg)
}
2020-12-14 22:20:24 +00:00
logrus.Debugf("SQL : %s => %s", entry.Fqdn, entry.Content) //log the result
2020-12-13 03:01:04 +00:00
2020-12-14 22:20:24 +00:00
if entry.Content != "" { //If Record content not empty
2020-12-13 03:01:04 +00:00
//Cache the request in Redis if any result
logrus.Debugf("REDIS : Set entry for %s", redisKey)
2020-12-14 22:20:24 +00:00
_ = redisSet(redisDb, redisKey, 30*time.Second, entry) //Set it in the Redis database for 30sec
2020-12-13 03:01:04 +00:00
return entry, 0
} else {
2020-12-14 22:20:24 +00:00
//Else return 1 for err
2020-12-13 03:01:04 +00:00
return entry, 1
}
}
2020-12-14 22:20:24 +00:00
//Check for a wildcard record in the SQL database
2020-12-13 03:01:04 +00:00
func sqlCheckForReverse6Wildcard(redisKey string, dKey string, entry Record) (Record, error) {
returnedEntry := entry
2020-12-14 22:20:24 +00:00
results, err := DB.Query("SELECT id, content, name FROM records WHERE name LIKE '*%.ip6.arpa.';") //Get ALL reverse IPs
DbgErr(err) //Check for empty row or non important error
2020-12-13 03:01:04 +00:00
2020-12-14 22:20:24 +00:00
//For each result check if it match the reverse IP
2020-12-13 03:01:04 +00:00
for results.Next() {
err = results.Scan(&returnedEntry.Id, &returnedEntry.Content, &returnedEntry.Fqdn)
CheckErr(err)
2020-12-14 22:20:24 +00:00
//Check if the record is matching the reversed IP
2020-12-13 03:01:04 +00:00
if checkReverse6(entry, returnedEntry) {
logrus.Debug("REVERSE : Correct wildcard reverse.")
//Cache the request in Redis if any result
_ = redisSet(redisDb, redisKey, 10*time.Second, returnedEntry)
return returnedEntry, err
} else {
logrus.Debug("REVERSE : WRONG wildcard reverse .")
}
}
return entry, redis.Nil
}