-
Notifications
You must be signed in to change notification settings - Fork 193
/
geodns.go
112 lines (88 loc) · 2.08 KB
/
geodns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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)
}