-
Notifications
You must be signed in to change notification settings - Fork 221
/
ping.go
320 lines (264 loc) · 6.49 KB
/
ping.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package ping
import (
"bytes"
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/dig"
"github.com/superfly/flyctl/internal/flag"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv6"
)
func New() *cobra.Command {
var (
long = strings.Trim(`
Test connectivity with ICMP ping messages.
This runs over WireGuard; tell us which WireGuard tunnel to use by
running from within an app directory (with a 'fly.toml'), passing the
'-a' flag with an app name, or the '-o' flag with an org name.
With no arguments, test connectivity to your gateway, the first hop
in our network, to see if your WireGuard connection is working.
The target argument can be either a ".internal" DNS name in our network
(the name of your application) or "gateway".
`, "\n")
short = `Test connectivity with ICMP ping messages`
)
cmd := command.New("ping [hostname] [flags]", short, long, run,
command.RequireSession, command.LoadAppNameIfPresent)
cmd.Args = cobra.RangeArgs(0, 1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Org(),
flag.String{
Name: "interval",
Shorthand: "i",
Default: "1s",
Description: "Interval between ping probes",
},
flag.Int{
Name: "count",
Shorthand: "n",
Default: 0,
Description: "Number of probes to send (0=indefinite)",
},
flag.Int{
Name: "size",
Shorthand: "s",
Default: 12,
Description: "Size of probe to send (not including headers)",
},
)
return cmd
}
func run(ctx context.Context) error {
client := client.FromContext(ctx).API()
var (
err error
name = flag.FirstArg(ctx)
)
switch {
case name == "":
case name == "gateway":
case strings.HasSuffix(name, ".internal"):
case strings.HasPrefix(name, "fdaa:"):
if net.ParseIP(name) == nil {
return fmt.Errorf("bad target name: malformed 6pn address")
}
default:
return fmt.Errorf("bad target name: Fly.io DNS names end in '.internal'")
}
// BUG(tqbf): DRY this up with dig
orgSlug := flag.GetOrg(ctx)
if orgSlug == "" {
appName := appconfig.NameFromContext(ctx)
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
orgSlug = app.Organization.Slug
}
aClient, err := agent.Establish(ctx, client)
if err != nil {
return err
}
r, ns, err := dig.ResolverForOrg(ctx, aClient, orgSlug)
if err != nil {
return err
}
var mu sync.RWMutex
targets := map[string]string{}
mu.Lock()
if name == "" || name == "gateway" {
targets[ns] = "gateway"
} else if strings.HasPrefix(name, "fdaa:") {
targets[name] = name
} else {
addrs, err := r.LookupHost(ctx, name)
if err != nil {
return fmt.Errorf("look up %s: %w", name, err)
}
for _, a := range addrs {
targets[a] = name
}
}
mu.Unlock()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if name != "" && name != "gateway" && !strings.HasPrefix(name, "fdaa:") {
// look up names in the background because I was too
// lazy to implement PTR in our DNS server
go func() {
// we already checked the format of this string
labels := strings.Split(name, ".internal")
app := labels[len(labels)-2]
regionName := fmt.Sprintf("regions.%s.internal", app)
regionFrags, err := r.LookupTXT(ctx, regionName)
if err != nil {
return
}
regions := strings.Join(regionFrags, "")
for _, region := range strings.Split(regions, ",") {
regHost := fmt.Sprintf("%s.%s.internal", region, app)
addrs, err := r.LookupHost(ctx, regHost)
if err == nil {
mu.Lock()
for _, addr := range addrs {
targets[addr] = regHost
}
mu.Unlock()
}
}
}()
}
pinger, err := aClient.Pinger(ctx, orgSlug)
if err != nil {
return err
}
ivString := flag.GetString(ctx, "interval")
interval, err := time.ParseDuration(ivString)
if err != nil {
return err
}
if interval < (100 * time.Millisecond) {
interval = 100 * time.Millisecond
}
count := flag.GetInt(ctx, "count")
pad := uint(flag.GetInt(ctx, "size"))
if pad > 1000 {
pad = 1000
}
ticker := time.NewTicker(interval)
timeLen := 15
type reply struct {
src net.Addr
pkt *icmp.Echo
lat time.Duration
}
replies := make(chan reply, 2)
go func() {
for {
if ctx.Err() != nil {
return
}
var (
replyBuf = make([]byte, 1500)
rmsg *icmp.Message
echoRep *icmp.Echo
ok bool
)
pinger.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n64, raddr, err := pinger.ReadFrom(replyBuf)
if err != nil {
continue
}
rmsg, err = icmp.ParseMessage(58, replyBuf[:n64])
if err == nil {
echoRep, ok = rmsg.Body.(*icmp.Echo)
}
if err != nil || !ok || len(echoRep.Data) < timeLen {
fmt.Printf("bogus ICMP from %s: %s", raddr, err)
continue
}
var t time.Time
err = t.UnmarshalBinary(echoRep.Data[:timeLen])
if err != nil {
fmt.Printf("malformed timestamp from %s: %s", raddr, err)
}
replies <- reply{
src: raddr,
pkt: echoRep,
lat: time.Since(t),
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
case reply := <-replies:
mu.RLock()
srcName := targets[reply.src.String()]
mu.RUnlock()
if srcName != "" {
srcName = " (" + srcName + ")"
}
lat := reply.lat.Truncate(100 * time.Microsecond)
fmt.Printf("%d bytes from %s%s, seq=%d time=%s\n", len(reply.pkt.Data)+8, reply.src, srcName, reply.pkt.Seq, lat)
}
}
}()
stp := make(chan os.Signal, 1)
signal.Notify(stp, syscall.SIGINT, syscall.SIGTERM)
for i := 0; count == 0 || i <= count; i++ {
select {
case <-stp:
return nil
case <-ticker.C:
}
for target := range targets {
// BUG(tqbf): stop re-parsing these stupid addresses
_, err = pinger.WriteTo(EchoRequest(0, i, time.Now(), pad), &net.IPAddr{IP: net.ParseIP(target)})
if err != nil {
return err
}
}
}
return nil
}
func EchoRequest(id, seq int, t time.Time, pad uint) []byte {
tbuf, _ := t.MarshalBinary()
buf := &bytes.Buffer{}
buf.Write(tbuf)
buf.Grow(int(pad))
for i := uint(0); i < pad; i++ {
buf.WriteByte('A')
}
msg := icmp.Message{
Type: ipv6.ICMPTypeEchoRequest,
Code: 0,
Body: &icmp.Echo{
ID: id,
Seq: seq,
Data: buf.Bytes(),
},
}
raw, err := msg.Marshal(nil)
if err != nil {
log.Panicf("marshal icmp: %s", err)
}
return raw
}