sacrebleu-dns/main.go

50 lines
1.3 KiB
Go
Raw Normal View History

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-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
var conf *utils.Conf
var DB *sql.DB
//Main loop
func main() {
2020-12-14 22:20:24 +00:00
//Get the config patch from --config flag
2020-12-14 01:04:29 +00:00
configPatch := flag.String("config", "extra/config.ini.example", "the patch to the config file")
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-13 03:01:04 +00:00
dns.HandleFunc(".", core.HandleDnsRequest)
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-13 03:01:04 +00:00
utils.SqlDatabase(conf)
2020-12-14 22:20:24 +00:00
//Start the DNS server
server := &dns.Server{Addr: conf.App.Ip + strconv.Itoa(conf.App.Port), Net: "udp"} //define the server
logrus.WithFields(logrus.Fields{"ip": conf.App.Ip, "port": conf.App.Port}).Infof("SERVER : Started") //log
err = server.ListenAndServe() //start it
2020-12-13 03:01:04 +00:00
utils.CheckErr(err)
defer server.Shutdown() //shut down on application closing
}