2020-12-13 04:01:04 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-12-14 02:04:29 +01:00
|
|
|
"flag"
|
2020-12-13 04:01:04 +01:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2020-12-13 19:26:55 +01:00
|
|
|
"github.com/outout14/sacrebleu-dns/core"
|
|
|
|
"github.com/outout14/sacrebleu-dns/utils"
|
2020-12-13 04:01:04 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Global vars
|
2020-12-22 04:12:02 +01:00
|
|
|
//conf Configuration
|
2020-12-13 04:01:04 +01:00
|
|
|
var conf *utils.Conf
|
2020-12-22 04:12:02 +01:00
|
|
|
|
|
|
|
//DB the SQL database
|
2020-12-13 04:01:04 +01:00
|
|
|
var DB *sql.DB
|
|
|
|
|
|
|
|
//Main loop
|
|
|
|
func main() {
|
2020-12-24 01:08:32 +01:00
|
|
|
configPatch := flag.String("config", "config.ini", "the patch to the config file") //Get the config patch from --config flag
|
2020-12-23 02:01:18 +01:00
|
|
|
sqlMigration := flag.Bool("sqlmigrate", false, "initialize / migrate the database") //Detect if migration asked
|
2020-12-14 02:04:29 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Load the INI configuration file
|
2020-12-13 04:01:04 +01:00
|
|
|
conf = new(utils.Conf)
|
2020-12-14 02:04:29 +01:00
|
|
|
err := ini.MapTo(conf, *configPatch)
|
2020-12-13 04:01:04 +01:00
|
|
|
utils.CheckErr(err)
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Set up the Logrus logger
|
2020-12-13 04:01:04 +01:00
|
|
|
utils.InitLogger(conf)
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Attach DNS request handler func for all domains
|
2020-12-22 04:12:02 +01:00
|
|
|
dns.HandleFunc(".", core.HandleDNSRequest)
|
2020-12-13 04:01:04 +01:00
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Initialize the redis database
|
2020-12-13 04:01:04 +01:00
|
|
|
utils.RedisDatabase(conf)
|
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Initialize the sql database
|
2020-12-22 04:12:02 +01:00
|
|
|
utils.SQLDatabase(conf)
|
2020-12-23 02:01:18 +01:00
|
|
|
if *sqlMigration {
|
|
|
|
utils.SQLMigrate()
|
|
|
|
}
|
2020-12-13 04:01:04 +01:00
|
|
|
|
2020-12-14 23:20:24 +01:00
|
|
|
//Start the DNS server
|
2020-12-23 02:26:39 +01:00
|
|
|
server := &dns.Server{Addr: conf.App.IP + ":" + strconv.Itoa(conf.App.Port), Net: "udp"} //define the server
|
2020-12-22 04:12:02 +01:00
|
|
|
logrus.WithFields(logrus.Fields{"ip": conf.App.IP, "port": conf.App.Port}).Infof("SERVER : Started") //log
|
2020-12-14 23:20:24 +01:00
|
|
|
err = server.ListenAndServe() //start it
|
2020-12-13 04:01:04 +01:00
|
|
|
utils.CheckErr(err)
|
|
|
|
|
|
|
|
defer server.Shutdown() //shut down on application closing
|
|
|
|
}
|