mirror of
https://github.com/taigrr/godns
synced 2025-01-18 04:03:25 -08:00
83 lines
1.7 KiB
Go
83 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
|
|
"log"
|
|
|
|
"github.com/TimothyYe/godns"
|
|
"github.com/TimothyYe/godns/handler"
|
|
"github.com/fatih/color"
|
|
)
|
|
|
|
var (
|
|
configuration godns.Settings
|
|
optConf = flag.String("c", "./config.json", "Specify a config file")
|
|
optHelp = flag.Bool("h", false, "Show help")
|
|
Version = "0.1"
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if *optHelp {
|
|
color.Cyan(godns.Logo, Version)
|
|
flag.Usage()
|
|
return
|
|
}
|
|
|
|
// Load settings from configurations file
|
|
if err := godns.LoadSettings(*optConf, &configuration); err != nil {
|
|
fmt.Println(err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
if err := godns.CheckSettings(&configuration); err != nil {
|
|
fmt.Println("Settings is invalid! ", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
if configuration.LogPath == "" {
|
|
configuration.LogPath = "./godns.log"
|
|
}
|
|
|
|
// Init log file
|
|
f, err := os.OpenFile(configuration.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
fmt.Println("Failed to create log file:", configuration.LogPath)
|
|
os.Exit(1)
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
log.Println("GoDNS started, entering main loop...")
|
|
dnsLoop()
|
|
}
|
|
|
|
func dnsLoop() {
|
|
panicChan := make(chan godns.Domain)
|
|
|
|
log.Println("Creating DNS handler with provider:", configuration.Provider)
|
|
handler := handler.CreateHandler(configuration.Provider)
|
|
handler.SetConfiguration(&configuration)
|
|
for _, domain := range configuration.Domains {
|
|
go handler.DomainLoop(&domain, panicChan)
|
|
}
|
|
|
|
panicCount := 0
|
|
for {
|
|
select {
|
|
case failDomain := <-panicChan:
|
|
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
|
|
go handler.DomainLoop(&failDomain, panicChan)
|
|
}
|
|
|
|
panicCount++
|
|
if panicCount >= godns.PanicMax {
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|