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