-
Notifications
You must be signed in to change notification settings - Fork 6
/
dns.go
134 lines (108 loc) · 2.64 KB
/
dns.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
package main
import (
"encoding/json"
"flag"
"net"
"strings"
"pkg/commands"
log "pkg/minilog"
)
type CommandDNS struct {
commands.Base // embed
cidr, domain string
}
func init() {
base := commands.Base{
Flags: flag.NewFlagSet("dns", flag.ExitOnError),
Usage: "dns [OPTION]... <ID>",
Short: "collect IP->hostname entries for DNS",
Long: `
Create a IP->hostname mapping for all nodes matching the filters and add the
mapping to the given node. This node can then resolve DNS requests.
`,
}
cmd := &CommandDNS{Base: base}
cmd.Flags.StringVar(&cmd.cidr, "filter-cidr", "", "filter by cidr")
cmd.Flags.StringVar(&cmd.domain, "filter-domain", "", "filter by domain")
commands.Append(cmd)
}
func (c *CommandDNS) Run() error {
if c.Flags.NArg() != 1 {
c.PrintUsage()
return nil
}
router := c.Flags.Arg(0)
log.Debug("using router id: %v", router)
// get the router
rtr, err := dc.GetEndpoint("nid", router)
if err != nil {
return err
}
endpoints, err := dc.GetEndpoints("", "")
if err != nil {
return err
}
// default filters don't filter anything
filterCIDR := func(net.IP) bool {
return false
}
filterDomain := func(string) bool {
return false
}
if c.cidr != "" {
log.Info("filtering on cidr %v", c.cidr)
_, ipnet, err := net.ParseCIDR(c.cidr)
if err != nil {
log.Fatalln(err)
}
filterCIDR = func(v net.IP) bool {
return !ipnet.Contains(v)
}
}
if c.domain != "" {
log.Info("filtering on domain %v", c.domain)
filterDomain = func(v string) bool {
return !strings.Contains(v, c.domain)
}
}
var resolv = make(map[string]string)
for _, v := range endpoints {
ips := GetIPs(v)
if len(ips) == 0 {
log.Error("no ip on endpoint: %v", v.NID)
continue
}
// TODO: what should we do if the node has more than one IP? We should
// probably need to track hostnames on a per-edge basis.
if len(ips) > 1 {
log.Info("%v has multiple IPs: %v", v.NID, ips)
}
ip := ips[0]
if filterCIDR(ip) {
log.Debug("filtering %v based on cidr filter", ip)
continue
}
if hostnames, ok := v.D["hostname"]; ok {
hosts := strings.Split(hostnames, ",")
for _, h := range hosts {
if filterDomain(h) {
log.Debug("filtering %v based on domain filter", h)
continue
}
resolv[h] = ip.String()
}
}
}
if len(resolv) > 0 {
j, err := json.Marshal(resolv)
if err != nil {
log.Fatalln(err)
}
rtr.D["dns"] = string(j)
return UpdateEndpoint(rtr)
}
return nil
}