1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00
godns/godns.go
2016-05-26 23:54:20 +08:00

105 lines
2.0 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"time"
)
const (
PANIC_MAX = 5
INTERVAL = 5 //Minute
)
var (
configuration Settings
optConf = flag.String("c", "./config.json", "config file")
optCommand = flag.String("s", "", "send signal to a master process: stop, quit, reopen, reload")
optHelp = flag.Bool("h", false, "this help")
panicCount = 0
)
func usage() {
log.Println("[command] -c=[config file path]")
flag.PrintDefaults()
}
func main() {
flag.Parse()
if *optHelp {
usage()
return
}
var err error
configuration, err = LoadSettings(*optConf)
err = InitLogger(configuration.Log_Path, configuration.Log_Size, configuration.Log_Num)
if err != nil {
log.Println("InitLogger error:", err)
return
}
if err != nil {
fmt.Println(err.Error())
log.Println(err.Error())
os.Exit(1)
}
dns_loop()
}
func dns_loop() {
defer func() {
if err := recover(); err != nil {
panicCount++
log.Printf("Recovered in %v: %v\n", err, debug.Stack())
if panicCount < PANIC_MAX {
log.Println("Got panic in goroutine, will start a new one... :", panicCount)
go dns_loop()
}
}
}()
for {
domain_id := getDomain(configuration.Domain)
if domain_id == -1 {
continue
}
currentIP, err := getCurrentIP(configuration.IP_Url)
if err != nil {
log.Println("get_currentIP:", err)
continue
}
sub_domain_id, ip := getSubDomain(domain_id, configuration.Sub_domain)
if sub_domain_id == "" || ip == "" {
log.Println("sub_domain:", sub_domain_id, ip)
continue
}
log.Println("currentIp is:", currentIP)
//Continue to check the IP of sub-domain
if len(ip) > 0 && !strings.Contains(currentIP, ip) {
log.Println("Start to update record IP...")
updateIP(domain_id, sub_domain_id, configuration.Sub_domain, currentIP)
} else {
log.Println("Current IP is same as domain IP, no need to update...")
}
//Interval is 5 minutes
time.Sleep(time.Minute * INTERVAL)
}
log.Printf("Loop %d exited...\n", panicCount)
}