123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package main
- import (
- "flag"
- "log"
- "net"
- "os"
- "os/signal"
- "runtime/pprof"
- "strings"
- "time"
- )
- var VERSION string = "2.2.0"
- var gitVersion string
- var serverId string
- var timeStarted = time.Now()
- var qCounter uint64 = 0
- var (
- flagconfig = flag.String("config", "./dns/", "directory of zone files")
- flaginter = flag.String("interface", "*", "set the listener address")
- flagport = flag.String("port", "53", "default port number")
- flaghttp = flag.String("http", ":8053", "http listen address (:8053)")
- flaglog = flag.Bool("log", false, "be more verbose")
- cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
- memprofile = flag.String("memprofile", "", "write memory profile to this file")
- )
- func init() {
- if len(gitVersion) > 0 {
- VERSION = VERSION + "/" + gitVersion
- }
- log.SetPrefix("geodns ")
- log.SetFlags(log.Lmicroseconds | log.Lshortfile)
- }
- func main() {
- flag.Parse()
- log.Printf("Starting geodns %s\n", VERSION)
- if *cpuprofile != "" {
- prof, err := os.Create(*cpuprofile)
- if err != nil {
- panic(err.Error())
- }
- pprof.StartCPUProfile(prof)
- defer func() {
- log.Println("closing file")
- prof.Close()
- }()
- defer func() {
- log.Println("stopping profile")
- pprof.StopCPUProfile()
- }()
- }
- if *flaginter == "*" {
- addrs, _ := net.InterfaceAddrs()
- ips := make([]string, 0)
- for _, addr := range addrs {
- ip, _, err := net.ParseCIDR(addr.String())
- if err != nil {
- continue
- }
- if !(ip.IsLoopback() || ip.IsGlobalUnicast()) {
- continue
- }
- ips = append(ips, ip.String())
- }
- *flaginter = strings.Join(ips, ",")
- }
- inter := getInterfaces()
- go monitor()
- dirName := *flagconfig
- Zones := make(Zones)
- setupPgeodnsZone(Zones)
- go configReader(dirName, Zones)
- for _, host := range inter {
- go listenAndServe(host, &Zones)
- }
- terminate := make(chan os.Signal)
- signal.Notify(terminate, os.Interrupt)
- <-terminate
- log.Printf("geodns: signal received, stopping")
- if *memprofile != "" {
- f, err := os.Create(*memprofile)
- if err != nil {
- log.Fatal(err)
- }
- pprof.WriteHeapProfile(f)
- f.Close()
- }
- //os.Exit(0)
- }
|