|
|
@ -152,9 +152,7 @@ func (d *derpProber) probeMesh(from, to string) ProbeFunc {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Unlock()
|
|
|
|
d.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: instead of ignoring latency, export it as a separate metric.
|
|
|
|
return derpProbeNodePair(ctx, dm, fromN, toN)
|
|
|
|
_, err := derpProbeNodePair(ctx, dm, fromN, toN)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -205,15 +203,14 @@ func (d *derpProber) updateMap(ctx context.Context) error {
|
|
|
|
|
|
|
|
|
|
|
|
func (d *derpProber) ProbeUDP(ipaddr string, port int) ProbeFunc {
|
|
|
|
func (d *derpProber) ProbeUDP(ipaddr string, port int) ProbeFunc {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
_, err := derpProbeUDP(ctx, ipaddr, port)
|
|
|
|
return derpProbeUDP(ctx, ipaddr, port)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func derpProbeUDP(ctx context.Context, ipStr string, port int) (latency time.Duration, err error) {
|
|
|
|
func derpProbeUDP(ctx context.Context, ipStr string, port int) error {
|
|
|
|
pc, err := net.ListenPacket("udp", ":0")
|
|
|
|
pc, err := net.ListenPacket("udp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer pc.Close()
|
|
|
|
defer pc.Close()
|
|
|
|
uc := pc.(*net.UDPConn)
|
|
|
|
uc := pc.(*net.UDPConn)
|
|
|
@ -228,7 +225,7 @@ func derpProbeUDP(ctx context.Context, ipStr string, port int) (latency time.Dur
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
_, err := uc.WriteToUDP(req, &net.UDPAddr{IP: ip, Port: port})
|
|
|
|
_, err := uc.WriteToUDP(req, &net.UDPAddr{IP: ip, Port: port})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Binding requests and responses are fairly small (~40 bytes),
|
|
|
|
// Binding requests and responses are fairly small (~40 bytes),
|
|
|
|
// but in practice a STUN response can be up to the size of the
|
|
|
|
// but in practice a STUN response can be up to the size of the
|
|
|
@ -240,38 +237,35 @@ func derpProbeUDP(ctx context.Context, ipStr string, port int) (latency time.Dur
|
|
|
|
d := time.Since(t0)
|
|
|
|
d := time.Since(t0)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return 0, fmt.Errorf("timeout reading from %v: %v", ip, err)
|
|
|
|
return fmt.Errorf("timeout reading from %v: %v", ip, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d < time.Second {
|
|
|
|
if d < time.Second {
|
|
|
|
return 0, fmt.Errorf("error reading from %v: %v", ip, err)
|
|
|
|
return fmt.Errorf("error reading from %v: %v", ip, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txBack, _, err := stun.ParseResponse(buf[:n])
|
|
|
|
txBack, _, err := stun.ParseResponse(buf[:n])
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("parsing STUN response from %v: %v", ip, err)
|
|
|
|
return fmt.Errorf("parsing STUN response from %v: %v", ip, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if txBack != tx {
|
|
|
|
if txBack != tx {
|
|
|
|
return 0, fmt.Errorf("read wrong tx back from %v", ip)
|
|
|
|
return fmt.Errorf("read wrong tx back from %v", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
if latency == 0 || d < latency {
|
|
|
|
|
|
|
|
latency = d
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return latency, nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func derpProbeNodePair(ctx context.Context, dm *tailcfg.DERPMap, from, to *tailcfg.DERPNode) (latency time.Duration, err error) {
|
|
|
|
func derpProbeNodePair(ctx context.Context, dm *tailcfg.DERPMap, from, to *tailcfg.DERPNode) (err error) {
|
|
|
|
fromc, err := newConn(ctx, dm, from)
|
|
|
|
fromc, err := newConn(ctx, dm, from)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer fromc.Close()
|
|
|
|
defer fromc.Close()
|
|
|
|
toc, err := newConn(ctx, dm, to)
|
|
|
|
toc, err := newConn(ctx, dm, to)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer toc.Close()
|
|
|
|
defer toc.Close()
|
|
|
|
|
|
|
|
|
|
|
@ -282,23 +276,20 @@ func derpProbeNodePair(ctx context.Context, dm *tailcfg.DERPMap, from, to *tailc
|
|
|
|
time.Sleep(100 * time.Millisecond) // pretty arbitrary
|
|
|
|
time.Sleep(100 * time.Millisecond) // pretty arbitrary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
latency, err = runDerpProbeNodePair(ctx, from, to, fromc, toc)
|
|
|
|
if err := runDerpProbeNodePair(ctx, from, to, fromc, toc); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
// Record pubkeys on failed probes to aid investigation.
|
|
|
|
// Record pubkeys on failed probes to aid investigation.
|
|
|
|
err = fmt.Errorf("%s -> %s: %w",
|
|
|
|
return fmt.Errorf("%s -> %s: %w",
|
|
|
|
fromc.SelfPublicKey().ShortString(),
|
|
|
|
fromc.SelfPublicKey().ShortString(),
|
|
|
|
toc.SelfPublicKey().ShortString(), err)
|
|
|
|
toc.SelfPublicKey().ShortString(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return latency, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func runDerpProbeNodePair(ctx context.Context, from, to *tailcfg.DERPNode, fromc, toc *derphttp.Client) (latency time.Duration, err error) {
|
|
|
|
func runDerpProbeNodePair(ctx context.Context, from, to *tailcfg.DERPNode, fromc, toc *derphttp.Client) error {
|
|
|
|
// Make a random packet
|
|
|
|
// Make a random packet
|
|
|
|
pkt := make([]byte, 8)
|
|
|
|
pkt := make([]byte, 8)
|
|
|
|
crand.Read(pkt)
|
|
|
|
crand.Read(pkt)
|
|
|
|
|
|
|
|
|
|
|
|
t0 := time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send the random packet.
|
|
|
|
// Send the random packet.
|
|
|
|
sendc := make(chan error, 1)
|
|
|
|
sendc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
@ -306,10 +297,10 @@ func runDerpProbeNodePair(ctx context.Context, from, to *tailcfg.DERPNode, fromc
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, fmt.Errorf("timeout sending via %q: %w", from.Name, ctx.Err())
|
|
|
|
return fmt.Errorf("timeout sending via %q: %w", from.Name, ctx.Err())
|
|
|
|
case err := <-sendc:
|
|
|
|
case err := <-sendc:
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("error sending via %q: %w", from.Name, err)
|
|
|
|
return fmt.Errorf("error sending via %q: %w", from.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -333,20 +324,20 @@ func runDerpProbeNodePair(ctx context.Context, from, to *tailcfg.DERPNode, fromc
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, fmt.Errorf("timeout receiving from %q: %w", to.Name, ctx.Err())
|
|
|
|
return fmt.Errorf("timeout receiving from %q: %w", to.Name, ctx.Err())
|
|
|
|
case v := <-recvc:
|
|
|
|
case v := <-recvc:
|
|
|
|
if err, ok := v.(error); ok {
|
|
|
|
if err, ok := v.(error); ok {
|
|
|
|
return 0, fmt.Errorf("error receiving from %q: %w", to.Name, err)
|
|
|
|
return fmt.Errorf("error receiving from %q: %w", to.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p := v.(derp.ReceivedPacket)
|
|
|
|
p := v.(derp.ReceivedPacket)
|
|
|
|
if p.Source != fromc.SelfPublicKey() {
|
|
|
|
if p.Source != fromc.SelfPublicKey() {
|
|
|
|
return 0, fmt.Errorf("got data packet from unexpected source, %v", p.Source)
|
|
|
|
return fmt.Errorf("got data packet from unexpected source, %v", p.Source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !bytes.Equal(p.Data, pkt) {
|
|
|
|
if !bytes.Equal(p.Data, pkt) {
|
|
|
|
return 0, fmt.Errorf("unexpected data packet %q", p.Data)
|
|
|
|
return fmt.Errorf("unexpected data packet %q", p.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Since(t0), nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newConn(ctx context.Context, dm *tailcfg.DERPMap, n *tailcfg.DERPNode) (*derphttp.Client, error) {
|
|
|
|
func newConn(ctx context.Context, dm *tailcfg.DERPMap, n *tailcfg.DERPNode) (*derphttp.Client, error) {
|
|
|
|