2020-12-13 04:01:04 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
2020-12-22 04:12:02 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql" //MySQL driver
|
2020-12-22 19:56:54 +01:00
|
|
|
_ "github.com/jackc/pgx/stdlib"
|
|
|
|
|
2020-12-13 04:01:04 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-12-22 04:12:02 +01:00
|
|
|
//DB SQL database as global var
|
2020-12-13 04:01:04 +01:00
|
|
|
var DB *sql.DB
|
|
|
|
|
2020-12-22 04:12:02 +01:00
|
|
|
//SQLDatabase Initialize the (My)SQL Database
|
2020-12-14 23:20:24 +01:00
|
|
|
//Requires a conf struct
|
2020-12-22 04:12:02 +01:00
|
|
|
func SQLDatabase(conf *Conf) {
|
2020-12-22 19:56:54 +01:00
|
|
|
logrus.WithFields(logrus.Fields{"database": conf.Database.Db, "driver": conf.Database.Type}).Infof("SQL : Connection to DB")
|
2020-12-14 23:20:24 +01:00
|
|
|
//Connect to the Database
|
2020-12-22 19:56:54 +01:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if conf.Database.Type == "postgresql" {
|
|
|
|
conString := fmt.Sprintf("user=%s password=%s host=%s port=%s database=%s sslmode=disable", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db)
|
|
|
|
|
|
|
|
DB, err = sql.Open("pgx", conString)
|
|
|
|
} else {
|
|
|
|
conString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", conf.Database.Username, conf.Database.Password, conf.Database.IP, conf.Database.Port, conf.Database.Db)
|
|
|
|
|
|
|
|
DB, err = sql.Open("mysql", conString)
|
|
|
|
}
|
2020-12-13 04:01:04 +01:00
|
|
|
CheckErr(err)
|
2020-12-22 04:12:02 +01:00
|
|
|
SQLTest() //Test SQL connexion
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 04:12:02 +01:00
|
|
|
//SQLTest : Test the SQL connexion by selecting all records from the database
|
|
|
|
func SQLTest() {
|
2020-12-14 02:15:43 +01:00
|
|
|
_, err := DB.Query("SELECT name, content FROM records")
|
2020-12-14 23:20:24 +01:00
|
|
|
CheckErr(err) //Panic if any error
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check for a record in the SQL database
|
2020-12-13 04:01:04 +01:00
|
|
|
func sqlCheckForRecord(redisKey string, dKey string, entry Record) (Record, int) {
|
|
|
|
dbg := DB.QueryRow(
|
2020-12-13 19:30:29 +01:00
|
|
|
"SELECT id, content, ttl FROM records WHERE `name` = ? AND `type` = ?;", dKey, entry.Qtype).Scan(
|
2020-12-22 04:12:02 +01:00
|
|
|
&entry.ID,
|
2020-12-13 04:01:04 +01:00
|
|
|
&entry.Content,
|
|
|
|
&entry.TTL,
|
|
|
|
)
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
if dbg != nil { //If any err
|
2020-12-13 04:01:04 +01:00
|
|
|
logrus.Debugf("SQL : %v", dbg)
|
|
|
|
}
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
logrus.Debugf("SQL : %s => %s", entry.Fqdn, entry.Content) //log the result
|
2020-12-13 04:01:04 +01:00
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
if entry.Content != "" { //If Record content not empty
|
2020-12-13 04:01:04 +01:00
|
|
|
//Cache the request in Redis if any result
|
|
|
|
logrus.Debugf("REDIS : Set entry for %s", redisKey)
|
2020-12-14 23:20:24 +01:00
|
|
|
_ = redisSet(redisDb, redisKey, 30*time.Second, entry) //Set it in the Redis database for 30sec
|
2020-12-13 04:01:04 +01:00
|
|
|
return entry, 0
|
|
|
|
}
|
2020-12-22 04:12:02 +01:00
|
|
|
//Else return 1 for err
|
|
|
|
return entry, 1
|
|
|
|
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check for a wildcard record in the SQL database
|
2020-12-13 04:01:04 +01:00
|
|
|
func sqlCheckForReverse6Wildcard(redisKey string, dKey string, entry Record) (Record, error) {
|
|
|
|
returnedEntry := entry
|
|
|
|
|
2020-12-14 23:20:24 +01: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 04:01:04 +01:00
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//For each result check if it match the reverse IP
|
2020-12-13 04:01:04 +01:00
|
|
|
for results.Next() {
|
2020-12-22 04:12:02 +01:00
|
|
|
err = results.Scan(&returnedEntry.ID, &returnedEntry.Content, &returnedEntry.Fqdn)
|
2020-12-13 04:01:04 +01:00
|
|
|
CheckErr(err)
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Check if the record is matching the reversed IP
|
2020-12-13 04:01:04 +01: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
|
|
|
|
}
|
2020-12-22 04:12:02 +01:00
|
|
|
logrus.Debug("REVERSE : WRONG wildcard reverse .")
|
2020-12-13 04:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry, redis.Nil
|
|
|
|
|
|
|
|
}
|