-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdnsmapper.go
113 lines (83 loc) · 2.58 KB
/
dnsmapper.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
113
package main
import (
"flag"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"github.com/devel/dnsmapper/storeapi"
lru "github.com/hashicorp/golang-lru"
"github.com/miekg/dns"
)
// Current version
var VERSION = "2.9.4"
var (
flagdomain = flag.String("domain", "example.com", "base domain for the dnsmapper")
flagip = flag.String("ip", "127.0.0.1", "set the IP address")
flagdnsport = flag.Int("dnsport", 53, "Set the DNS port")
flaghttpport = flag.Int("httpport", 80, "Set the HTTP port")
flaghttpsport = flag.Int("httpsport", 443, "Set the HTTP/TLS port")
flagtlskeyfile = flag.String("tlskeyfile", "", "Specify path to TLS key (optional)")
flagtlscrtfile = flag.String("tlscertfile", "", "Specify path to TLS certificate (optional)")
flagacmedomain = flag.String("acmedomain", "", "Domain to cname _acme-challenge.${domain} to")
flagreporthost = flag.String("reporthost", "", "Hostname for results host")
flagPrimaryNs = flag.String("ns", "ns.example.com", "nameserver names (comma separated)")
)
type logChannel chan *storeapi.RequestData
var baseLength int
var primaryNsList []string
var cache *lru.Cache
var ch logChannel
func getUUIDFromDomain(name string) string {
lx := dns.SplitDomainName(name)
if len(lx) <= baseLength {
return ""
}
ql := lx[0 : len(lx)-baseLength]
return strings.ToLower(strings.Join(ql, "."))
}
func setup() {
baseLength = dns.CountLabel(*flagdomain)
primaryNsList = strings.Split(*flagPrimaryNs, ",")
log.Println("Listening for requests to", *flagdomain)
}
func init() {
ch = make(logChannel, posterCount*20)
os.Setenv("PGSSLMODE", "disable")
// Setup a cache to support X DNS requests per time-period
// per server where time-period is how long the client
// gets to come back with http after doing the DNS request.
var err error
cache, err = lru.New(20000)
if err != nil {
log.Fatalf("Could not setup lru cache: %s", err)
}
}
func main() {
log.Printf("Starting dnsmapper %s\n", VERSION)
flag.Parse()
runtime.MemProfileRate = 1
setup()
dns.HandleFunc(*flagdomain, setupServerFunc())
for i := 0; i < posterCount; i++ {
go reportPoster(ch)
}
go httpHandler(*flagip, *flaghttpport, *flaghttpsport)
go listenAndServeDNS(*flagip, *flagdnsport)
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt)
<-terminate
log.Printf("dnsmapper: signal received, stopping")
f, err := os.Create("dnsmapper.pprof")
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
err = f.Close()
if err != nil {
log.Println("Error closing profile:", err)
}
log.Println("... exiting.")
}