-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
78 lines (68 loc) · 2.14 KB
/
main.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
package main
import (
"context"
"net"
"os"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/mailgun/holster/v3/collections"
"github.com/miekg/dns"
log "github.com/sirupsen/logrus"
)
type CLI struct {
Iface string `kong:"arg,placeholder='eth0',help='Network interface.'"`
Debug bool `kong:"short='d',help='Enable debug logging.'"`
TTL time.Duration `kong:"short='t',help='Cache TTL.',default=30s"`
Listen string `kong:"short='l',default='127.0.0.1:5333',help='DNS server listen on.'"`
Domain string `kong:"short='n',default='.dnsharper.local',help='Domain.'"`
AliasesFile *os.File `kong:"name='aliases',short='a',help='MAC to hostname mapping file.'"`
}
func main() {
var cli CLI
kong.Parse(&cli)
if cli.Debug {
log.SetLevel(log.DebugLevel)
}
if _, ok := dns.IsDomainName(cli.Domain); !ok {
log.Fatalf("%q doesn't look like a valid domain", cli.Domain)
return
}
cli.Domain = strings.TrimPrefix(cli.Domain, ".")
cache := collections.NewLRUCache(256 * 256)
cache.OnEvicted = func(key collections.Key, value interface{}) {
domain := strings.ReplaceAll(key.(string), ":", "-") + "." + cli.Domain
domain, _ = strings.CutPrefix(domain, "ipv4-")
domain, _ = strings.CutPrefix(domain, "ipv6-")
log.WithFields(log.Fields{
"domain": domain,
"ip": value.(net.IP),
}).Debug("Evicted from cache")
}
var err error
var aliases map[string][]string
if cli.AliasesFile != nil {
if aliases, err = readAliases(cli.AliasesFile, cli.Domain); err != nil {
log.WithError(err).Fatal("Failed to read aliases file")
return
}
}
scanner, err := NewScanner(cli.Iface, cli.TTL, cli.Domain, aliases, cache)
if err != nil {
log.WithError(err).Fatal("Failed to initialize scanner")
return
}
server, err := NewServer(cli.Listen, cli.Domain, aliases, cache)
if err != nil {
log.WithError(err).Fatal("Failed to DNS server")
return
}
go func() {
if err := server.ListenAndServe(); err != nil {
log.WithError(err).Error("Failed to listen")
}
}()
if err := scanner.Scan(context.Background()); err != nil {
log.WithError(err).Error("Failed to scan")
}
}