From c02f4b5a1f982944ab9986c09e37d96af8e266b6 Mon Sep 17 00:00:00 2001
From: Brad Fitzpatrick <bradfitz@tailscale.com>
Date: Fri, 21 Feb 2020 14:01:51 -0800
Subject: [PATCH] control/controlclient: add temporary mechanism to force derp
 on

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
---
 control/controlclient/direct.go | 9 +++++++++
 wgengine/magicsock/magicsock.go | 7 +++++++
 2 files changed, 16 insertions(+)

diff --git a/control/controlclient/direct.go b/control/controlclient/direct.go
index a91b52dff..7fd9997dc 100644
--- a/control/controlclient/direct.go
+++ b/control/controlclient/direct.go
@@ -18,6 +18,7 @@ import (
 	"net/http"
 	"os"
 	"runtime"
+	"strconv"
 	"strings"
 	"sync"
 	"time"
@@ -538,6 +539,14 @@ func (c *Direct) PollNetMap(ctx context.Context, maxPolls int, cb func(*NetworkM
 			Hostinfo:     resp.Node.Hostinfo,
 			PacketFilter: resp.PacketFilter,
 		}
+		// Temporary (2020-02-21) knob to force debug, during DERP testing:
+		if ok, _ := strconv.ParseBool(os.Getenv("DEBUG_FORCE_DERP")); ok {
+			c.logf("debug: adding DERP endpoints to all peers")
+			for i := range nm.Peers {
+				peer := &nm.Peers[i]
+				peer.Endpoints = append(peer.Endpoints, "127.3.3.40:1")
+			}
+		}
 		for _, profile := range resp.UserProfiles {
 			nm.UserProfiles[profile.ID] = profile
 		}
diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go
index 6f3bbec6a..33cbefecf 100644
--- a/wgengine/magicsock/magicsock.go
+++ b/wgengine/magicsock/magicsock.go
@@ -13,6 +13,8 @@ import (
 	"fmt"
 	"log"
 	"net"
+	"os"
+	"strconv"
 	"strings"
 	"sync"
 	"sync/atomic"
@@ -376,6 +378,8 @@ func shouldSprayPacket(b []byte) bool {
 	return false
 }
 
+var logPacketDests, _ = strconv.ParseBool(os.Getenv("DEBUG_LOG_PACKET_DESTS"))
+
 // appendDests appends to dsts the destinations that b should be
 // written to in order to reach as. Some of the returned UDPAddrs may
 // be fake addrs representing DERP servers.
@@ -403,6 +407,9 @@ func appendDests(dsts []*net.UDPAddr, as *AddrSet, b []byte) (_ []*net.UDPAddr,
 			break
 		}
 	}
+	if logPacketDests {
+		log.Printf("spray=%v; roam=%v; dests=%v", spray, roamAddr, dsts)
+	}
 	return dsts, roamAddr
 }