-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
188 lines (161 loc) · 4.63 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"context"
"encoding/binary"
"errors"
"flag"
"fmt"
"log"
"math/rand/v2"
"net/netip"
"os"
"strings"
"github.com/tailscale/tailscale-client-go/tailscale"
"go4.org/netipx"
)
var (
fromPrefixes prefixSlice
toPrefixes prefixSlice
apply = flag.Bool("apply", false, "make changes, otherwise will just print devices found within -from-prefixes")
maxRetries = flag.Int("max-retries", 5, "max times to retry if random new IP is already in use")
continueOnError = flag.Bool("continue-on-error", false, "continue reassigning devices if an error for any device is encountered")
cgnatPfx = netip.MustParsePrefix("100.64.0.0/10")
)
type prefixSlice []netip.Prefix
func (i *prefixSlice) String() string {
return fmt.Sprintf("%s", *i)
}
func (i *prefixSlice) Set(value string) error {
values := strings.Split(value, ",")
for _, v := range values {
parsedPrefix, err := netip.ParsePrefix(strings.TrimSpace(v))
if err != nil {
return err
}
if !cgnatPfx.Overlaps(parsedPrefix) {
return errors.New(fmt.Sprintf("prefix [%s] is not within [%s]", v, cgnatPfx))
}
*i = append(*i, parsedPrefix)
}
return nil
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: tailscale-prefix-mover [flags]\n")
flag.PrintDefaults()
}
func checkArgs() error {
if fromPrefixes == nil || len(fromPrefixes) == 0 {
return errors.New("missing required flag -from-prefixes")
}
return nil
}
func main() {
flag.Var(&fromPrefixes, "from-prefixes", fmt.Sprintf("prefixes to move devices FROM - must be within %s", cgnatPfx))
flag.Var(&toPrefixes, "to-prefixes", fmt.Sprintf("prefixes to move devices to - must be within %s", cgnatPfx))
flag.Parse()
err := checkArgs()
if err != nil {
fmt.Printf("%s\n", err)
usage()
os.Exit(1)
}
apiKey := os.Getenv("TAILSCALE_API_KEY")
tailnet := os.Getenv("TAILSCALE_TAILNET")
tailscaleClient, err := tailscale.NewClient(apiKey, tailnet)
if err != nil {
log.Fatalln(err)
}
availablePrefixes := toPrefixes
if availablePrefixes == nil {
availablePrefixes, err = calculateAvailablePrefixes(fromPrefixes)
if err != nil {
log.Fatalln(err)
}
}
fmt.Printf("Moving devices from %s to %s\n", fromPrefixes, availablePrefixes)
ctx := context.Background()
devices, err := tailscaleClient.Devices(ctx)
if err != nil {
log.Fatalln(err)
}
errCount := 0
for _, fromPrefix := range fromPrefixes {
for _, device := range devices {
v4Address, err := netip.ParseAddr(device.Addresses[0])
if err != nil {
log.Fatalln(err)
}
if fromPrefix.Contains(v4Address) {
err = reassignDeviceAddress(ctx, tailscaleClient, device, availablePrefixes)
if err != nil {
errCount++
fmt.Printf("error setting address for device [nodeid:%-16s / name:%s] - [%s]\n", device.ID, device.Name, err)
if *continueOnError {
fmt.Printf(" Continuing...\n")
continue
} else {
fmt.Printf(" Stopping.\n")
break // unnecessary because log.Fatal will exit, but seems good to have here anyway
}
}
}
}
}
if !*apply {
fmt.Printf("Pass -apply to make changes.\n")
}
if errCount > 0 {
fmt.Printf("Done.\n")
os.Exit(1)
} else {
fmt.Printf("Done.\n")
}
}
func reassignDeviceAddress(ctx context.Context, tailscaleClient *tailscale.Client, device tailscale.Device, availablePrefixes []netip.Prefix) error {
for i := 0; i < *maxRetries; i++ {
prefix := availablePrefixes[rand.IntN(len(availablePrefixes))]
var newAddress string
if *apply {
newAddress = randV4(prefix).String()
} else {
newAddress = "v.x.y.z"
}
fmt.Printf("Setting v4 address [%-15s] to [nodeid:%-18s / name:%s]... ", newAddress, device.ID, device.Name)
if !*apply {
fmt.Printf("done.\n")
return nil
}
err := tailscaleClient.SetDeviceIPv4Address(ctx, device.ID, newAddress)
if err != nil && err.Error() == "address already in use (500)" {
fmt.Printf("[%s] - retrying...\n", err)
continue
} else if err != nil {
return err
} else {
fmt.Printf("done.\n")
return nil
}
}
return errors.New(fmt.Sprintf("Unable to set new address after [%v] tries", *maxRetries))
}
func calculateAvailablePrefixes(prefixes []netip.Prefix) ([]netip.Prefix, error) {
var b netipx.IPSetBuilder
b.AddPrefix(cgnatPfx)
for _, p := range prefixes {
b.RemovePrefix(p)
}
s, err := b.IPSet()
if err != nil {
return nil, err
}
return s.Prefixes(), nil
}
// credit to https://github.com/maisem
func randV4(maskedPfx netip.Prefix) netip.Addr {
bits := 32 - maskedPfx.Bits()
randBits := rand.Uint32N(1 << uint(bits))
ip4 := maskedPfx.Addr().As4()
pn := binary.BigEndian.Uint32(ip4[:])
binary.BigEndian.PutUint32(ip4[:], randBits|pn)
return netip.AddrFrom4(ip4)
}