-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailscale.go
130 lines (110 loc) · 3.25 KB
/
tailscale.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
package main
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"
"tailscale.com/client/tailscale"
"tailscale.com/ipn"
"tailscale.com/tailcfg"
"tailscale.com/types/netmap"
)
type Tailscale struct {
signal chan bool
zone string
lc *tailscale.LocalClient
domain string
mu sync.RWMutex
entries map[string]map[string][]string
}
// Name implements the Handler interface.
func (t *Tailscale) Name() string { return "tailscale" }
// start connects the Tailscale plugin to a tailscale daemon and populates DNS entries for nodes in the tailnet.
// DNS entries are automatically kept up to date with any node changes.
func (t *Tailscale) start() error {
// zero value LocalClient will connect to local tailscaled
t.lc = &tailscale.LocalClient{}
go t.watchIPNBus()
return nil
}
// watchIPNBus watches the Tailscale IPN Bus and updates DNS entries for any netmap update.
// This function does not return. If it is unable to read from the IPN Bus, it will continue to retry.
func (t *Tailscale) watchIPNBus() {
for {
watcher, err := t.lc.WatchIPNBus(context.Background(), ipn.NotifyInitialNetMap)
if err != nil {
log.Print("unable to read from Tailscale event bus, retrying in 1 minute")
time.Sleep(1 * time.Minute)
continue
}
defer watcher.Close()
for {
n, err := watcher.Next()
if err != nil {
// If we're unable to read, then close watcher and reconnect
watcher.Close()
break
}
t.processNetMap(n.NetMap)
// Signal update
t.signal <- true
}
}
}
func (t *Tailscale) processNetMap(nm *netmap.NetworkMap) {
if nm == nil {
return
}
log.Printf("Self tags: %+v", nm.SelfNode.Tags().AsSlice())
nodes := []tailcfg.NodeView{nm.SelfNode}
nodes = append(nodes, nm.Peers...)
entries := map[string]map[string][]string{}
for _, node := range nodes {
if node != nm.SelfNode && (node.Online() == nil || !*node.Online()) {
// No connection from perspective of Tailscale considered offline
continue
}
if node.IsWireGuardOnly() {
// IsWireGuardOnly identifies a node as a Mullvad exit node.
continue
}
if !node.Sharer().IsZero() {
// Skip shared nodes, since they don't necessarily have unique hostnames within this tailnet.
// TODO: possibly make it configurable to include shared nodes and figure out what hostname to use.
continue
}
hostname := node.ComputedName()
entry, ok := entries[hostname]
if !ok {
entry = map[string][]string{}
}
// Currently entry["A"/"AAAA"] will have max one element
for _, pfx := range node.Addresses().AsSlice() {
addr := pfx.Addr()
if addr.Is4() {
entry["A"] = append(entry["A"], addr.String())
} else if addr.Is6() {
entry["AAAA"] = append(entry["AAAA"], addr.String())
}
}
// Process Tags looking for cname- prefixed ones
if node.Tags().Len() > 0 {
for _, raw := range node.Tags().AsSlice() {
if tag, ok := strings.CutPrefix(raw, "tag:cname-"); ok {
if _, ok := entries[tag]; !ok {
entries[tag] = map[string][]string{}
}
entries[tag]["CNAME"] = append(entries[tag]["CNAME"], fmt.Sprintf("%s.%s.", hostname, t.zone))
}
}
}
entries[hostname] = entry
}
t.mu.Lock()
t.entries = entries
t.domain = nm.MagicDNSSuffix()
t.mu.Unlock()
log.Printf("updated %d Tailscale entries", len(entries))
}