derpserver.go 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package derpserver implements a DERP server.
  4. package derpserver
  5. // TODO(crawshaw): with predefined serverKey in clients and HMAC on packets we could skip TLS
  6. import (
  7. "bufio"
  8. "bytes"
  9. "context"
  10. "crypto/ed25519"
  11. crand "crypto/rand"
  12. "crypto/tls"
  13. "crypto/x509"
  14. "crypto/x509/pkix"
  15. "encoding/binary"
  16. "encoding/json"
  17. "errors"
  18. "expvar"
  19. "fmt"
  20. "io"
  21. "log"
  22. "math"
  23. "math/big"
  24. "math/rand/v2"
  25. "net/http"
  26. "net/netip"
  27. "os"
  28. "os/exec"
  29. "runtime"
  30. "strconv"
  31. "strings"
  32. "sync"
  33. "sync/atomic"
  34. "time"
  35. "go4.org/mem"
  36. "golang.org/x/sync/errgroup"
  37. "tailscale.com/client/local"
  38. "tailscale.com/derp"
  39. "tailscale.com/derp/derpconst"
  40. "tailscale.com/disco"
  41. "tailscale.com/envknob"
  42. "tailscale.com/metrics"
  43. "tailscale.com/syncs"
  44. "tailscale.com/tailcfg"
  45. "tailscale.com/tstime"
  46. "tailscale.com/tstime/rate"
  47. "tailscale.com/types/key"
  48. "tailscale.com/types/logger"
  49. "tailscale.com/util/ctxkey"
  50. "tailscale.com/util/mak"
  51. "tailscale.com/util/set"
  52. "tailscale.com/util/slicesx"
  53. "tailscale.com/version"
  54. )
  55. // verboseDropKeys is the set of destination public keys that should
  56. // verbosely log whenever DERP drops a packet.
  57. var verboseDropKeys = map[key.NodePublic]bool{}
  58. // IdealNodeContextKey is the context key used to pass the IdealNodeHeader value
  59. // from the HTTP handler to the DERP server's Accept method.
  60. var IdealNodeContextKey = ctxkey.New("ideal-node", "")
  61. func init() {
  62. keys := envknob.String("TS_DEBUG_VERBOSE_DROPS")
  63. if keys == "" {
  64. return
  65. }
  66. for _, keyStr := range strings.Split(keys, ",") {
  67. k, err := key.ParseNodePublicUntyped(mem.S(keyStr))
  68. if err != nil {
  69. log.Printf("ignoring invalid debug key %q: %v", keyStr, err)
  70. } else {
  71. verboseDropKeys[k] = true
  72. }
  73. }
  74. }
  75. const (
  76. defaultPerClientSendQueueDepth = 32 // default packets buffered for sending
  77. DefaultTCPWiteTimeout = 2 * time.Second
  78. privilegedWriteTimeout = 30 * time.Second // for clients with the mesh key
  79. )
  80. func getPerClientSendQueueDepth() int {
  81. if v, ok := envknob.LookupInt("TS_DEBUG_DERP_PER_CLIENT_SEND_QUEUE_DEPTH"); ok {
  82. return v
  83. }
  84. return defaultPerClientSendQueueDepth
  85. }
  86. // dupPolicy is a temporary (2021-08-30) mechanism to change the policy
  87. // of how duplicate connection for the same key are handled.
  88. type dupPolicy int8
  89. const (
  90. // lastWriterIsActive is a dupPolicy where the connection
  91. // to send traffic for a peer is the active one.
  92. lastWriterIsActive dupPolicy = iota
  93. // disableFighters is a dupPolicy that detects if peers
  94. // are trying to send interleaved with each other and
  95. // then disables all of them.
  96. disableFighters
  97. )
  98. // packetKind is the kind of packet being sent through DERP
  99. type packetKind string
  100. const (
  101. packetKindDisco packetKind = "disco"
  102. packetKindOther packetKind = "other"
  103. )
  104. type align64 [0]atomic.Int64 // for side effect of its 64-bit alignment
  105. // Server is a DERP server.
  106. type Server struct {
  107. // WriteTimeout, if non-zero, specifies how long to wait
  108. // before failing when writing to a client.
  109. WriteTimeout time.Duration
  110. privateKey key.NodePrivate
  111. publicKey key.NodePublic
  112. logf logger.Logf
  113. memSys0 uint64 // runtime.MemStats.Sys at start (or early-ish)
  114. meshKey key.DERPMesh
  115. limitedLogf logger.Logf
  116. metaCert []byte // the encoded x509 cert to send after LetsEncrypt cert+intermediate
  117. dupPolicy dupPolicy
  118. debug bool
  119. localClient local.Client
  120. // Counters:
  121. packetsSent, bytesSent expvar.Int
  122. packetsRecv, bytesRecv expvar.Int
  123. packetsRecvByKind metrics.LabelMap
  124. packetsRecvDisco *expvar.Int
  125. packetsRecvOther *expvar.Int
  126. _ align64
  127. packetsForwardedOut expvar.Int
  128. packetsForwardedIn expvar.Int
  129. peerGoneDisconnectedFrames expvar.Int // number of peer disconnected frames sent
  130. peerGoneNotHereFrames expvar.Int // number of peer not here frames sent
  131. gotPing expvar.Int // number of ping frames from client
  132. sentPong expvar.Int // number of pong frames enqueued to client
  133. accepts expvar.Int
  134. curClients expvar.Int
  135. curClientsNotIdeal expvar.Int
  136. curHomeClients expvar.Int // ones with preferred
  137. dupClientKeys expvar.Int // current number of public keys we have 2+ connections for
  138. dupClientConns expvar.Int // current number of connections sharing a public key
  139. dupClientConnTotal expvar.Int // total number of accepted connections when a dup key existed
  140. unknownFrames expvar.Int
  141. homeMovesIn expvar.Int // established clients announce home server moves in
  142. homeMovesOut expvar.Int // established clients announce home server moves out
  143. multiForwarderCreated expvar.Int
  144. multiForwarderDeleted expvar.Int
  145. removePktForwardOther expvar.Int
  146. sclientWriteTimeouts expvar.Int
  147. avgQueueDuration *uint64 // In milliseconds; accessed atomically
  148. tcpRtt metrics.LabelMap // histogram
  149. meshUpdateBatchSize *metrics.Histogram
  150. meshUpdateLoopCount *metrics.Histogram
  151. bufferedWriteFrames *metrics.Histogram // how many sendLoop frames (or groups of related frames) get written per flush
  152. // verifyClientsLocalTailscaled only accepts client connections to the DERP
  153. // server if the clientKey is a known peer in the network, as specified by a
  154. // running tailscaled's client's LocalAPI.
  155. verifyClientsLocalTailscaled bool
  156. verifyClientsURL string
  157. verifyClientsURLFailOpen bool
  158. mu syncs.Mutex
  159. closed bool
  160. netConns map[derp.Conn]chan struct{} // chan is closed when conn closes
  161. clients map[key.NodePublic]*clientSet
  162. watchers set.Set[*sclient] // mesh peers
  163. // clientsMesh tracks all clients in the cluster, both locally
  164. // and to mesh peers. If the value is nil, that means the
  165. // peer is only local (and thus in the clients Map, but not
  166. // remote). If the value is non-nil, it's remote (+ maybe also
  167. // local).
  168. clientsMesh map[key.NodePublic]PacketForwarder
  169. // peerGoneWatchers is the set of watchers that subscribed to a
  170. // peer disconnecting from the region overall. When a peer
  171. // is gone from the region, we notify all of these watchers,
  172. // calling their funcs in a new goroutine.
  173. peerGoneWatchers map[key.NodePublic]set.HandleSet[func(key.NodePublic)]
  174. // maps from netip.AddrPort to a client's public key
  175. keyOfAddr map[netip.AddrPort]key.NodePublic
  176. // Sets the client send queue depth for the server.
  177. perClientSendQueueDepth int
  178. tcpWriteTimeout time.Duration
  179. clock tstime.Clock
  180. }
  181. // clientSet represents 1 or more *sclients.
  182. //
  183. // In the common case, client should only have one connection to the
  184. // DERP server for a given key. When they're connected multiple times,
  185. // we record their set of connections in dupClientSet and keep their
  186. // connections open to make them happy (to keep them from spinning,
  187. // etc) and keep track of which is the latest connection. If only the last
  188. // is sending traffic, that last one is the active connection and it
  189. // gets traffic. Otherwise, in the case of a cloned node key, the
  190. // whole set of dups doesn't receive data frames.
  191. //
  192. // All methods should only be called while holding Server.mu.
  193. //
  194. // TODO(bradfitz): Issue 2746: in the future we'll send some sort of
  195. // "health_error" frame to them that'll communicate to the end users
  196. // that they cloned a device key, and we'll also surface it in the
  197. // admin panel, etc.
  198. type clientSet struct {
  199. // activeClient holds the currently active connection for the set. It's nil
  200. // if there are no connections or the connection is disabled.
  201. //
  202. // A pointer to a clientSet can be held by peers for long periods of time
  203. // without holding Server.mu to avoid mutex contention on Server.mu, only
  204. // re-acquiring the mutex and checking the clients map if activeClient is
  205. // nil.
  206. activeClient atomic.Pointer[sclient]
  207. // dup is non-nil if there are multiple connections for the
  208. // public key. It's nil in the common case of only one
  209. // client being connected.
  210. //
  211. // dup is guarded by Server.mu.
  212. dup *dupClientSet
  213. }
  214. // Len returns the number of clients in s, which can be
  215. // 0, 1 (the common case), or more (for buggy or transiently
  216. // reconnecting clients).
  217. func (s *clientSet) Len() int {
  218. if s.dup != nil {
  219. return len(s.dup.set)
  220. }
  221. if s.activeClient.Load() != nil {
  222. return 1
  223. }
  224. return 0
  225. }
  226. // ForeachClient calls f for each client in the set.
  227. //
  228. // The Server.mu must be held.
  229. func (s *clientSet) ForeachClient(f func(*sclient)) {
  230. if s.dup != nil {
  231. for c := range s.dup.set {
  232. f(c)
  233. }
  234. } else if c := s.activeClient.Load(); c != nil {
  235. f(c)
  236. }
  237. }
  238. // A dupClientSet is a clientSet of more than 1 connection.
  239. //
  240. // This can occur in some reasonable cases (temporarily while users
  241. // are changing networks) or in the case of a cloned key. In the
  242. // cloned key case, both peers are speaking and the clients get
  243. // disabled.
  244. //
  245. // All fields are guarded by Server.mu.
  246. type dupClientSet struct {
  247. // set is the set of connected clients for sclient.key,
  248. // including the clientSet's active one.
  249. set set.Set[*sclient]
  250. // last is the most recent addition to set, or nil if the most
  251. // recent one has since disconnected and nobody else has sent
  252. // data since.
  253. last *sclient
  254. // sendHistory is a log of which members of set have sent
  255. // frames to the derp server, with adjacent duplicates
  256. // removed. When a member of set is removed, the same
  257. // element(s) are removed from sendHistory.
  258. sendHistory []*sclient
  259. }
  260. func (s *clientSet) pickActiveClient() *sclient {
  261. d := s.dup
  262. if d == nil {
  263. return s.activeClient.Load()
  264. }
  265. if d.last != nil && !d.last.isDisabled.Load() {
  266. return d.last
  267. }
  268. return nil
  269. }
  270. // removeClient removes c from s and reports whether it was in s
  271. // to begin with.
  272. func (s *dupClientSet) removeClient(c *sclient) bool {
  273. n := len(s.set)
  274. delete(s.set, c)
  275. if s.last == c {
  276. s.last = nil
  277. }
  278. if len(s.set) == n {
  279. return false
  280. }
  281. trim := s.sendHistory[:0]
  282. for _, v := range s.sendHistory {
  283. if s.set.Contains(v) && (len(trim) == 0 || trim[len(trim)-1] != v) {
  284. trim = append(trim, v)
  285. }
  286. }
  287. for i := len(trim); i < len(s.sendHistory); i++ {
  288. s.sendHistory[i] = nil
  289. }
  290. s.sendHistory = trim
  291. if s.last == nil && len(s.sendHistory) > 0 {
  292. s.last = s.sendHistory[len(s.sendHistory)-1]
  293. }
  294. return true
  295. }
  296. // PacketForwarder is something that can forward packets.
  297. //
  298. // It's mostly an interface for circular dependency reasons; the
  299. // typical implementation is derphttp.Client. The other implementation
  300. // is a multiForwarder, which this package creates as needed if a
  301. // public key gets more than one PacketForwarder registered for it.
  302. type PacketForwarder interface {
  303. ForwardPacket(src, dst key.NodePublic, payload []byte) error
  304. String() string
  305. }
  306. var packetsDropped = metrics.NewMultiLabelMap[dropReasonKindLabels](
  307. "derp_packets_dropped",
  308. "counter",
  309. "DERP packets dropped by reason and by kind")
  310. var bytesDropped = metrics.NewMultiLabelMap[dropReasonKindLabels](
  311. "derp_bytes_dropped",
  312. "counter",
  313. "DERP bytes dropped by reason and by kind",
  314. )
  315. // New returns a new DERP server. It doesn't listen on its own.
  316. // Connections are given to it via Server.Accept.
  317. func New(privateKey key.NodePrivate, logf logger.Logf) *Server {
  318. var ms runtime.MemStats
  319. runtime.ReadMemStats(&ms)
  320. s := &Server{
  321. debug: envknob.Bool("DERP_DEBUG_LOGS"),
  322. privateKey: privateKey,
  323. publicKey: privateKey.Public(),
  324. logf: logf,
  325. limitedLogf: logger.RateLimitedFn(logf, 30*time.Second, 5, 100),
  326. packetsRecvByKind: metrics.LabelMap{Label: "kind"},
  327. clients: map[key.NodePublic]*clientSet{},
  328. clientsMesh: map[key.NodePublic]PacketForwarder{},
  329. netConns: map[derp.Conn]chan struct{}{},
  330. memSys0: ms.Sys,
  331. watchers: set.Set[*sclient]{},
  332. peerGoneWatchers: map[key.NodePublic]set.HandleSet[func(key.NodePublic)]{},
  333. avgQueueDuration: new(uint64),
  334. tcpRtt: metrics.LabelMap{Label: "le"},
  335. meshUpdateBatchSize: metrics.NewHistogram([]float64{0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000}),
  336. meshUpdateLoopCount: metrics.NewHistogram([]float64{0, 1, 2, 5, 10, 20, 50, 100}),
  337. bufferedWriteFrames: metrics.NewHistogram([]float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 50, 100}),
  338. keyOfAddr: map[netip.AddrPort]key.NodePublic{},
  339. clock: tstime.StdClock{},
  340. tcpWriteTimeout: DefaultTCPWiteTimeout,
  341. }
  342. s.initMetacert()
  343. s.packetsRecvDisco = s.packetsRecvByKind.Get(string(packetKindDisco))
  344. s.packetsRecvOther = s.packetsRecvByKind.Get(string(packetKindOther))
  345. genDroppedCounters()
  346. s.perClientSendQueueDepth = getPerClientSendQueueDepth()
  347. return s
  348. }
  349. func genDroppedCounters() {
  350. initMetrics := func(reason dropReason) {
  351. packetsDropped.Add(dropReasonKindLabels{
  352. Kind: string(packetKindDisco),
  353. Reason: string(reason),
  354. }, 0)
  355. packetsDropped.Add(dropReasonKindLabels{
  356. Kind: string(packetKindOther),
  357. Reason: string(reason),
  358. }, 0)
  359. bytesDropped.Add(dropReasonKindLabels{
  360. Kind: string(packetKindDisco),
  361. Reason: string(reason),
  362. }, 0)
  363. bytesDropped.Add(dropReasonKindLabels{
  364. Kind: string(packetKindOther),
  365. Reason: string(reason),
  366. }, 0)
  367. }
  368. getMetrics := func(reason dropReason) []expvar.Var {
  369. return []expvar.Var{
  370. packetsDropped.Get(dropReasonKindLabels{
  371. Kind: string(packetKindDisco),
  372. Reason: string(reason),
  373. }),
  374. packetsDropped.Get(dropReasonKindLabels{
  375. Kind: string(packetKindOther),
  376. Reason: string(reason),
  377. }),
  378. bytesDropped.Get(dropReasonKindLabels{
  379. Kind: string(packetKindDisco),
  380. Reason: string(reason),
  381. }),
  382. bytesDropped.Get(dropReasonKindLabels{
  383. Kind: string(packetKindOther),
  384. Reason: string(reason),
  385. }),
  386. }
  387. }
  388. dropReasons := []dropReason{
  389. dropReasonUnknownDest,
  390. dropReasonUnknownDestOnFwd,
  391. dropReasonGoneDisconnected,
  392. dropReasonQueueHead,
  393. dropReasonQueueTail,
  394. dropReasonWriteError,
  395. dropReasonDupClient,
  396. }
  397. for _, dr := range dropReasons {
  398. initMetrics(dr)
  399. m := getMetrics(dr)
  400. if len(m) != 4 {
  401. panic("dropReason metrics out of sync")
  402. }
  403. for _, v := range m {
  404. if v == nil {
  405. panic("dropReason metrics out of sync")
  406. }
  407. }
  408. }
  409. }
  410. // SetMesh sets the pre-shared key that regional DERP servers used to mesh
  411. // amongst themselves.
  412. //
  413. // It must be called before serving begins.
  414. func (s *Server) SetMeshKey(v string) error {
  415. k, err := key.ParseDERPMesh(v)
  416. if err != nil {
  417. return err
  418. }
  419. s.meshKey = k
  420. return nil
  421. }
  422. // SetVerifyClients sets whether this DERP server verifies clients through tailscaled.
  423. //
  424. // It must be called before serving begins.
  425. func (s *Server) SetVerifyClient(v bool) {
  426. s.verifyClientsLocalTailscaled = v
  427. }
  428. // SetVerifyClientURL sets the admission controller URL to use for verifying clients.
  429. // If empty, all clients are accepted (unless restricted by SetVerifyClient checking
  430. // against tailscaled).
  431. func (s *Server) SetVerifyClientURL(v string) {
  432. s.verifyClientsURL = v
  433. }
  434. // SetVerifyClientURLFailOpen sets whether to allow clients to connect if the
  435. // admission controller URL is unreachable.
  436. func (s *Server) SetVerifyClientURLFailOpen(v bool) {
  437. s.verifyClientsURLFailOpen = v
  438. }
  439. // SetTailscaledSocketPath sets the unix socket path to use to talk to
  440. // tailscaled if client verification is enabled.
  441. //
  442. // If unset or set to the empty string, the default path for the operating
  443. // system is used.
  444. func (s *Server) SetTailscaledSocketPath(path string) {
  445. s.localClient.Socket = path
  446. s.localClient.UseSocketOnly = path != ""
  447. }
  448. // SetTCPWriteTimeout sets the timeout for writing to connected clients.
  449. // This timeout does not apply to mesh connections.
  450. // Defaults to 2 seconds.
  451. func (s *Server) SetTCPWriteTimeout(d time.Duration) {
  452. s.tcpWriteTimeout = d
  453. }
  454. // HasMeshKey reports whether the server is configured with a mesh key.
  455. func (s *Server) HasMeshKey() bool { return !s.meshKey.IsZero() }
  456. // MeshKey returns the configured mesh key, if any.
  457. func (s *Server) MeshKey() key.DERPMesh { return s.meshKey }
  458. // PrivateKey returns the server's private key.
  459. func (s *Server) PrivateKey() key.NodePrivate { return s.privateKey }
  460. // PublicKey returns the server's public key.
  461. func (s *Server) PublicKey() key.NodePublic { return s.publicKey }
  462. // Close closes the server and waits for the connections to disconnect.
  463. func (s *Server) Close() error {
  464. s.mu.Lock()
  465. wasClosed := s.closed
  466. s.closed = true
  467. s.mu.Unlock()
  468. if wasClosed {
  469. return nil
  470. }
  471. var closedChs []chan struct{}
  472. s.mu.Lock()
  473. for nc, closed := range s.netConns {
  474. nc.Close()
  475. closedChs = append(closedChs, closed)
  476. }
  477. s.mu.Unlock()
  478. for _, closed := range closedChs {
  479. <-closed
  480. }
  481. return nil
  482. }
  483. func (s *Server) isClosed() bool {
  484. s.mu.Lock()
  485. defer s.mu.Unlock()
  486. return s.closed
  487. }
  488. // IsClientConnectedForTest reports whether the client with specified key is connected.
  489. // This is used in tests to verify that nodes are connected.
  490. func (s *Server) IsClientConnectedForTest(k key.NodePublic) bool {
  491. s.mu.Lock()
  492. defer s.mu.Unlock()
  493. x, ok := s.clients[k]
  494. if !ok {
  495. return false
  496. }
  497. return x.activeClient.Load() != nil
  498. }
  499. // Accept adds a new connection to the server and serves it.
  500. //
  501. // The provided bufio ReadWriter must be already connected to nc.
  502. // Accept blocks until the Server is closed or the connection closes
  503. // on its own.
  504. //
  505. // Accept closes nc.
  506. func (s *Server) Accept(ctx context.Context, nc derp.Conn, brw *bufio.ReadWriter, remoteAddr string) {
  507. closed := make(chan struct{})
  508. s.mu.Lock()
  509. s.accepts.Add(1) // while holding s.mu for connNum read on next line
  510. connNum := s.accepts.Value() // expvar sadly doesn't return new value on Add(1)
  511. s.netConns[nc] = closed
  512. s.mu.Unlock()
  513. defer func() {
  514. nc.Close()
  515. close(closed)
  516. s.mu.Lock()
  517. delete(s.netConns, nc)
  518. s.mu.Unlock()
  519. }()
  520. if err := s.accept(ctx, nc, brw, remoteAddr, connNum); err != nil && !s.isClosed() {
  521. s.logf("derp: %s: %v", remoteAddr, err)
  522. }
  523. }
  524. // initMetacert initialized s.metaCert with a self-signed x509 cert
  525. // encoding this server's public key and protocol version. cmd/derper
  526. // then sends this after the Let's Encrypt leaf + intermediate certs
  527. // after the ServerHello (encrypted in TLS 1.3, not that it matters
  528. // much).
  529. //
  530. // Then the client can save a round trip getting that and can start
  531. // speaking DERP right away. (We don't use ALPN because that's sent in
  532. // the clear and we're being paranoid to not look too weird to any
  533. // middleboxes, given that DERP is an ultimate fallback path). But
  534. // since the post-ServerHello certs are encrypted we can have the
  535. // client also use them as a signal to be able to start speaking DERP
  536. // right away, starting with its identity proof, encrypted to the
  537. // server's public key.
  538. //
  539. // This RTT optimization fails where there's a corp-mandated
  540. // TLS proxy with corp-mandated root certs on employee machines and
  541. // and TLS proxy cleans up unnecessary certs. In that case we just fall
  542. // back to the extra RTT.
  543. func (s *Server) initMetacert() {
  544. pub, priv, err := ed25519.GenerateKey(crand.Reader)
  545. if err != nil {
  546. log.Fatal(err)
  547. }
  548. tmpl := &x509.Certificate{
  549. SerialNumber: big.NewInt(derp.ProtocolVersion),
  550. Subject: pkix.Name{
  551. CommonName: derpconst.MetaCertCommonNamePrefix + s.publicKey.UntypedHexString(),
  552. },
  553. // Windows requires NotAfter and NotBefore set:
  554. NotAfter: s.clock.Now().Add(30 * 24 * time.Hour),
  555. NotBefore: s.clock.Now().Add(-30 * 24 * time.Hour),
  556. // Per https://github.com/golang/go/issues/51759#issuecomment-1071147836,
  557. // macOS requires BasicConstraints when subject == issuer:
  558. BasicConstraintsValid: true,
  559. }
  560. cert, err := x509.CreateCertificate(crand.Reader, tmpl, tmpl, pub, priv)
  561. if err != nil {
  562. log.Fatalf("CreateCertificate: %v", err)
  563. }
  564. s.metaCert = cert
  565. }
  566. // MetaCert returns the server metadata cert that can be sent by the
  567. // TLS server to let the client skip a round trip during start-up.
  568. func (s *Server) MetaCert() []byte { return s.metaCert }
  569. // ModifyTLSConfigToAddMetaCert modifies c.GetCertificate to make
  570. // it append s.MetaCert to the returned certificates.
  571. //
  572. // It panics if c or c.GetCertificate is nil.
  573. func (s *Server) ModifyTLSConfigToAddMetaCert(c *tls.Config) {
  574. getCert := c.GetCertificate
  575. if getCert == nil {
  576. panic("c.GetCertificate is nil")
  577. }
  578. c.GetCertificate = func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
  579. cert, err := getCert(hi)
  580. if err != nil {
  581. return nil, err
  582. }
  583. cert.Certificate = append(cert.Certificate, s.MetaCert())
  584. return cert, nil
  585. }
  586. }
  587. // registerClient notes that client c is now authenticated and ready for packets.
  588. //
  589. // If c.key is connected more than once, the earlier connection(s) are
  590. // placed in a non-active state where we read from them (primarily to
  591. // observe EOFs/timeouts) but won't send them frames on the assumption
  592. // that they're dead.
  593. func (s *Server) registerClient(c *sclient) {
  594. s.mu.Lock()
  595. defer s.mu.Unlock()
  596. cs, ok := s.clients[c.key]
  597. if !ok {
  598. c.debugLogf("register single client")
  599. cs = &clientSet{}
  600. s.clients[c.key] = cs
  601. }
  602. was := cs.activeClient.Load()
  603. if was == nil {
  604. // Common case.
  605. } else {
  606. was.isDup.Store(true)
  607. c.isDup.Store(true)
  608. }
  609. dup := cs.dup
  610. if dup == nil && was != nil {
  611. s.dupClientKeys.Add(1)
  612. s.dupClientConns.Add(2) // both old and new count
  613. s.dupClientConnTotal.Add(1)
  614. dup = &dupClientSet{
  615. set: set.Of(c, was),
  616. last: c,
  617. sendHistory: []*sclient{was},
  618. }
  619. cs.dup = dup
  620. c.debugLogf("register duplicate client")
  621. } else if dup != nil {
  622. s.dupClientConns.Add(1) // the gauge
  623. s.dupClientConnTotal.Add(1) // the counter
  624. dup.set.Add(c)
  625. dup.last = c
  626. dup.sendHistory = append(dup.sendHistory, c)
  627. c.debugLogf("register another duplicate client")
  628. }
  629. cs.activeClient.Store(c)
  630. if _, ok := s.clientsMesh[c.key]; !ok {
  631. s.clientsMesh[c.key] = nil // just for varz of total users in cluster
  632. }
  633. s.keyOfAddr[c.remoteIPPort] = c.key
  634. s.curClients.Add(1)
  635. if c.isNotIdealConn {
  636. s.curClientsNotIdeal.Add(1)
  637. }
  638. s.broadcastPeerStateChangeLocked(c.key, c.remoteIPPort, c.presentFlags(), true)
  639. }
  640. // broadcastPeerStateChangeLocked enqueues a message to all watchers
  641. // (other DERP nodes in the region, or trusted clients) that peer's
  642. // presence changed.
  643. //
  644. // s.mu must be held.
  645. func (s *Server) broadcastPeerStateChangeLocked(peer key.NodePublic, ipPort netip.AddrPort, flags derp.PeerPresentFlags, present bool) {
  646. for w := range s.watchers {
  647. w.peerStateChange = append(w.peerStateChange, peerConnState{
  648. peer: peer,
  649. present: present,
  650. ipPort: ipPort,
  651. flags: flags,
  652. })
  653. go w.requestMeshUpdate()
  654. }
  655. }
  656. // unregisterClient removes a client from the server.
  657. func (s *Server) unregisterClient(c *sclient) {
  658. s.mu.Lock()
  659. defer s.mu.Unlock()
  660. set, ok := s.clients[c.key]
  661. if !ok {
  662. c.logf("[unexpected]; clients map is empty")
  663. return
  664. }
  665. dup := set.dup
  666. if dup == nil {
  667. // The common case.
  668. cur := set.activeClient.Load()
  669. if cur == nil {
  670. c.logf("[unexpected]; active client is nil")
  671. return
  672. }
  673. if cur != c {
  674. c.logf("[unexpected]; active client is not c")
  675. return
  676. }
  677. c.debugLogf("removed connection")
  678. set.activeClient.Store(nil)
  679. delete(s.clients, c.key)
  680. if v, ok := s.clientsMesh[c.key]; ok && v == nil {
  681. delete(s.clientsMesh, c.key)
  682. s.notePeerGoneFromRegionLocked(c.key)
  683. }
  684. s.broadcastPeerStateChangeLocked(c.key, netip.AddrPort{}, 0, false)
  685. } else {
  686. c.debugLogf("removed duplicate client")
  687. if dup.removeClient(c) {
  688. s.dupClientConns.Add(-1)
  689. } else {
  690. c.logf("[unexpected]; dup client set didn't shrink")
  691. }
  692. if dup.set.Len() == 1 {
  693. // If we drop down to one connection, demote it down
  694. // to a regular single client (a nil dup set).
  695. set.dup = nil
  696. s.dupClientConns.Add(-1) // again; for the original one's
  697. s.dupClientKeys.Add(-1)
  698. var remain *sclient
  699. for remain = range dup.set {
  700. break
  701. }
  702. if remain == nil {
  703. panic("unexpected nil remain from single element dup set")
  704. }
  705. remain.isDisabled.Store(false)
  706. remain.isDup.Store(false)
  707. set.activeClient.Store(remain)
  708. } else {
  709. // Still a duplicate. Pick a winner.
  710. set.activeClient.Store(set.pickActiveClient())
  711. }
  712. }
  713. if c.canMesh {
  714. delete(s.watchers, c)
  715. }
  716. delete(s.keyOfAddr, c.remoteIPPort)
  717. s.curClients.Add(-1)
  718. if c.preferred {
  719. s.curHomeClients.Add(-1)
  720. }
  721. if c.isNotIdealConn {
  722. s.curClientsNotIdeal.Add(-1)
  723. }
  724. }
  725. // addPeerGoneFromRegionWatcher adds a function to be called when peer is gone
  726. // from the region overall. It returns a handle that can be used to remove the
  727. // watcher later.
  728. //
  729. // The provided f func is usually [sclient.onPeerGoneFromRegion], added by
  730. // [sclient.noteSendFromSrc]; this func doesn't take a whole *sclient to make it
  731. // clear what has access to what.
  732. func (s *Server) addPeerGoneFromRegionWatcher(peer key.NodePublic, f func(key.NodePublic)) set.Handle {
  733. s.mu.Lock()
  734. defer s.mu.Unlock()
  735. hset, ok := s.peerGoneWatchers[peer]
  736. if !ok {
  737. hset = set.HandleSet[func(key.NodePublic)]{}
  738. s.peerGoneWatchers[peer] = hset
  739. }
  740. return hset.Add(f)
  741. }
  742. // removePeerGoneFromRegionWatcher removes a peer watcher previously added by
  743. // addPeerGoneFromRegionWatcher, using the handle returned by
  744. // addPeerGoneFromRegionWatcher.
  745. func (s *Server) removePeerGoneFromRegionWatcher(peer key.NodePublic, h set.Handle) {
  746. s.mu.Lock()
  747. defer s.mu.Unlock()
  748. hset, ok := s.peerGoneWatchers[peer]
  749. if !ok {
  750. return
  751. }
  752. delete(hset, h)
  753. if len(hset) == 0 {
  754. delete(s.peerGoneWatchers, peer)
  755. }
  756. }
  757. // notePeerGoneFromRegionLocked sends peerGone frames to parties that
  758. // key has sent to previously (whether those sends were from a local
  759. // client or forwarded). It must only be called after the key has
  760. // been removed from clientsMesh.
  761. func (s *Server) notePeerGoneFromRegionLocked(key key.NodePublic) {
  762. if _, ok := s.clientsMesh[key]; ok {
  763. panic("usage")
  764. }
  765. // Find still-connected peers and either notify that we've gone away
  766. // so they can drop their route entries to us (issue 150)
  767. // or move them over to the active client (in case a replaced client
  768. // connection is being unregistered).
  769. set := s.peerGoneWatchers[key]
  770. for _, f := range set {
  771. go f(key)
  772. }
  773. delete(s.peerGoneWatchers, key)
  774. }
  775. // requestPeerGoneWriteLimited sends a request to write a "peer gone"
  776. // frame, but only in reply to a disco packet, and only if we haven't
  777. // sent one recently.
  778. func (c *sclient) requestPeerGoneWriteLimited(peer key.NodePublic, contents []byte, reason derp.PeerGoneReasonType) {
  779. if disco.LooksLikeDiscoWrapper(contents) != true {
  780. return
  781. }
  782. if c.peerGoneLim.Allow() {
  783. go c.requestPeerGoneWrite(peer, reason)
  784. }
  785. }
  786. func (s *Server) addWatcher(c *sclient) {
  787. if !c.canMesh {
  788. panic("invariant: addWatcher called without permissions")
  789. }
  790. if c.key == s.publicKey {
  791. // We're connecting to ourself. Do nothing.
  792. return
  793. }
  794. s.mu.Lock()
  795. defer s.mu.Unlock()
  796. // Queue messages for each already-connected client.
  797. for peer, clientSet := range s.clients {
  798. ac := clientSet.activeClient.Load()
  799. if ac == nil {
  800. continue
  801. }
  802. c.peerStateChange = append(c.peerStateChange, peerConnState{
  803. peer: peer,
  804. present: true,
  805. ipPort: ac.remoteIPPort,
  806. flags: ac.presentFlags(),
  807. })
  808. }
  809. // And enroll the watcher in future updates (of both
  810. // connections & disconnections).
  811. s.watchers.Add(c)
  812. go c.requestMeshUpdate()
  813. }
  814. func (s *Server) accept(ctx context.Context, nc derp.Conn, brw *bufio.ReadWriter, remoteAddr string, connNum int64) error {
  815. br := brw.Reader
  816. nc.SetDeadline(time.Now().Add(10 * time.Second))
  817. bw := &lazyBufioWriter{w: nc, lbw: brw.Writer}
  818. if err := s.sendServerKey(bw); err != nil {
  819. return fmt.Errorf("send server key: %v", err)
  820. }
  821. nc.SetDeadline(time.Now().Add(10 * time.Second))
  822. clientKey, clientInfo, err := s.recvClientKey(br)
  823. if err != nil {
  824. return fmt.Errorf("receive client key: %v", err)
  825. }
  826. remoteIPPort, _ := netip.ParseAddrPort(remoteAddr)
  827. if err := s.verifyClient(ctx, clientKey, clientInfo, remoteIPPort.Addr()); err != nil {
  828. return fmt.Errorf("client %v rejected: %v", clientKey, err)
  829. }
  830. // At this point we trust the client so we don't time out.
  831. nc.SetDeadline(time.Time{})
  832. ctx, cancel := context.WithCancel(ctx)
  833. defer cancel()
  834. c := &sclient{
  835. connNum: connNum,
  836. s: s,
  837. key: clientKey,
  838. nc: nc,
  839. br: br,
  840. bw: bw,
  841. logf: logger.WithPrefix(s.logf, fmt.Sprintf("derp client %v%s: ", remoteAddr, clientKey.ShortString())),
  842. done: ctx.Done(),
  843. remoteIPPort: remoteIPPort,
  844. connectedAt: s.clock.Now(),
  845. sendQueue: make(chan pkt, s.perClientSendQueueDepth),
  846. discoSendQueue: make(chan pkt, s.perClientSendQueueDepth),
  847. sendPongCh: make(chan [8]byte, 1),
  848. peerGone: make(chan peerGoneMsg),
  849. canMesh: s.isMeshPeer(clientInfo),
  850. isNotIdealConn: IdealNodeContextKey.Value(ctx) != "",
  851. peerGoneLim: rate.NewLimiter(rate.Every(time.Second), 3),
  852. }
  853. if c.canMesh {
  854. c.meshUpdate = make(chan struct{}, 1) // must be buffered; >1 is fine but wasteful
  855. }
  856. if clientInfo != nil {
  857. c.info = *clientInfo
  858. if envknob.Bool("DERP_PROBER_DEBUG_LOGS") && clientInfo.IsProber {
  859. c.debug = true
  860. }
  861. }
  862. if s.debug {
  863. c.debug = true
  864. }
  865. s.registerClient(c)
  866. defer s.unregisterClient(c)
  867. err = s.sendServerInfo(c.bw, clientKey)
  868. if err != nil {
  869. return fmt.Errorf("send server info: %v", err)
  870. }
  871. return c.run(ctx)
  872. }
  873. func (s *Server) debugLogf(format string, v ...any) {
  874. if s.debug {
  875. s.logf(format, v...)
  876. }
  877. }
  878. // run serves the client until there's an error.
  879. // If the client hangs up or the server is closed, run returns nil, otherwise run returns an error.
  880. func (c *sclient) run(ctx context.Context) error {
  881. // Launch sender, but don't return from run until sender goroutine is done.
  882. var grp errgroup.Group
  883. sendCtx, cancelSender := context.WithCancel(ctx)
  884. grp.Go(func() error { return c.sendLoop(sendCtx) })
  885. defer func() {
  886. cancelSender()
  887. if err := grp.Wait(); err != nil && !c.s.isClosed() {
  888. if errors.Is(err, context.Canceled) {
  889. c.debugLogf("sender canceled by reader exiting")
  890. } else {
  891. if errors.Is(err, os.ErrDeadlineExceeded) {
  892. c.s.sclientWriteTimeouts.Add(1)
  893. }
  894. c.logf("sender failed: %v", err)
  895. }
  896. }
  897. }()
  898. c.startStatsLoop(sendCtx)
  899. for {
  900. ft, fl, err := derp.ReadFrameHeader(c.br)
  901. c.debugLogf("read frame type %d len %d err %v", ft, fl, err)
  902. if err != nil {
  903. if errors.Is(err, io.EOF) {
  904. c.debugLogf("read EOF")
  905. return nil
  906. }
  907. if c.s.isClosed() {
  908. c.logf("closing; server closed")
  909. return nil
  910. }
  911. return fmt.Errorf("client %s: readFrameHeader: %w", c.key.ShortString(), err)
  912. }
  913. c.s.noteClientActivity(c)
  914. switch ft {
  915. case derp.FrameNotePreferred:
  916. err = c.handleFrameNotePreferred(ft, fl)
  917. case derp.FrameSendPacket:
  918. err = c.handleFrameSendPacket(ft, fl)
  919. case derp.FrameForwardPacket:
  920. err = c.handleFrameForwardPacket(ft, fl)
  921. case derp.FrameWatchConns:
  922. err = c.handleFrameWatchConns(ft, fl)
  923. case derp.FrameClosePeer:
  924. err = c.handleFrameClosePeer(ft, fl)
  925. case derp.FramePing:
  926. err = c.handleFramePing(ft, fl)
  927. default:
  928. err = c.handleUnknownFrame(ft, fl)
  929. }
  930. if err != nil {
  931. return err
  932. }
  933. }
  934. }
  935. func (c *sclient) handleUnknownFrame(ft derp.FrameType, fl uint32) error {
  936. _, err := io.CopyN(io.Discard, c.br, int64(fl))
  937. return err
  938. }
  939. func (c *sclient) handleFrameNotePreferred(ft derp.FrameType, fl uint32) error {
  940. if fl != 1 {
  941. return fmt.Errorf("frameNotePreferred wrong size")
  942. }
  943. v, err := c.br.ReadByte()
  944. if err != nil {
  945. return fmt.Errorf("frameNotePreferred ReadByte: %v", err)
  946. }
  947. c.setPreferred(v != 0)
  948. return nil
  949. }
  950. func (c *sclient) handleFrameWatchConns(ft derp.FrameType, fl uint32) error {
  951. if fl != 0 {
  952. return fmt.Errorf("handleFrameWatchConns wrong size")
  953. }
  954. if !c.canMesh {
  955. return fmt.Errorf("insufficient permissions")
  956. }
  957. c.s.addWatcher(c)
  958. return nil
  959. }
  960. func (c *sclient) handleFramePing(ft derp.FrameType, fl uint32) error {
  961. c.s.gotPing.Add(1)
  962. var m derp.PingMessage
  963. if fl < uint32(len(m)) {
  964. return fmt.Errorf("short ping: %v", fl)
  965. }
  966. if fl > 1000 {
  967. // unreasonably extra large. We leave some extra
  968. // space for future extensibility, but not too much.
  969. return fmt.Errorf("ping body too large: %v", fl)
  970. }
  971. _, err := io.ReadFull(c.br, m[:])
  972. if err != nil {
  973. return err
  974. }
  975. if extra := int64(fl) - int64(len(m)); extra > 0 {
  976. _, err = io.CopyN(io.Discard, c.br, extra)
  977. }
  978. select {
  979. case c.sendPongCh <- [8]byte(m):
  980. default:
  981. // They're pinging too fast. Ignore.
  982. // TODO(bradfitz): add a rate limiter too.
  983. }
  984. return err
  985. }
  986. func (c *sclient) handleFrameClosePeer(ft derp.FrameType, fl uint32) error {
  987. if fl != derp.KeyLen {
  988. return fmt.Errorf("handleFrameClosePeer wrong size")
  989. }
  990. if !c.canMesh {
  991. return fmt.Errorf("insufficient permissions")
  992. }
  993. var targetKey key.NodePublic
  994. if err := targetKey.ReadRawWithoutAllocating(c.br); err != nil {
  995. return err
  996. }
  997. s := c.s
  998. s.mu.Lock()
  999. defer s.mu.Unlock()
  1000. if set, ok := s.clients[targetKey]; ok {
  1001. if set.Len() == 1 {
  1002. c.logf("frameClosePeer closing peer %x", targetKey)
  1003. } else {
  1004. c.logf("frameClosePeer closing peer %x (%d connections)", targetKey, set.Len())
  1005. }
  1006. set.ForeachClient(func(target *sclient) {
  1007. go target.nc.Close()
  1008. })
  1009. } else {
  1010. c.logf("frameClosePeer failed to find peer %x", targetKey)
  1011. }
  1012. return nil
  1013. }
  1014. // handleFrameForwardPacket reads a "forward packet" frame from the client
  1015. // (which must be a trusted client, a peer in our mesh).
  1016. func (c *sclient) handleFrameForwardPacket(ft derp.FrameType, fl uint32) error {
  1017. if !c.canMesh {
  1018. return fmt.Errorf("insufficient permissions")
  1019. }
  1020. s := c.s
  1021. srcKey, dstKey, contents, err := s.recvForwardPacket(c.br, fl)
  1022. if err != nil {
  1023. return fmt.Errorf("client %v: recvForwardPacket: %v", c.key, err)
  1024. }
  1025. s.packetsForwardedIn.Add(1)
  1026. var dstLen int
  1027. var dst *sclient
  1028. s.mu.Lock()
  1029. if set, ok := s.clients[dstKey]; ok {
  1030. dstLen = set.Len()
  1031. dst = set.activeClient.Load()
  1032. }
  1033. s.mu.Unlock()
  1034. if dst == nil {
  1035. reason := dropReasonUnknownDestOnFwd
  1036. if dstLen > 1 {
  1037. reason = dropReasonDupClient
  1038. } else {
  1039. c.requestPeerGoneWriteLimited(dstKey, contents, derp.PeerGoneReasonNotHere)
  1040. }
  1041. s.recordDrop(contents, srcKey, dstKey, reason)
  1042. return nil
  1043. }
  1044. dst.debugLogf("received forwarded packet from %s via %s", srcKey.ShortString(), c.key.ShortString())
  1045. return c.sendPkt(dst, pkt{
  1046. bs: contents,
  1047. enqueuedAt: c.s.clock.Now(),
  1048. src: srcKey,
  1049. })
  1050. }
  1051. // handleFrameSendPacket reads a "send packet" frame from the client.
  1052. func (c *sclient) handleFrameSendPacket(ft derp.FrameType, fl uint32) error {
  1053. s := c.s
  1054. dstKey, contents, err := s.recvPacket(c.br, fl)
  1055. if err != nil {
  1056. return fmt.Errorf("client %v: recvPacket: %v", c.key, err)
  1057. }
  1058. var fwd PacketForwarder
  1059. var dstLen int
  1060. var dst *sclient
  1061. s.mu.Lock()
  1062. if set, ok := s.clients[dstKey]; ok {
  1063. dstLen = set.Len()
  1064. dst = set.activeClient.Load()
  1065. }
  1066. if dst == nil && dstLen < 1 {
  1067. fwd = s.clientsMesh[dstKey]
  1068. }
  1069. s.mu.Unlock()
  1070. if dst == nil {
  1071. if fwd != nil {
  1072. s.packetsForwardedOut.Add(1)
  1073. err := fwd.ForwardPacket(c.key, dstKey, contents)
  1074. c.debugLogf("SendPacket for %s, forwarding via %s: %v", dstKey.ShortString(), fwd, err)
  1075. if err != nil {
  1076. // TODO:
  1077. return nil
  1078. }
  1079. return nil
  1080. }
  1081. reason := dropReasonUnknownDest
  1082. if dstLen > 1 {
  1083. reason = dropReasonDupClient
  1084. } else {
  1085. c.requestPeerGoneWriteLimited(dstKey, contents, derp.PeerGoneReasonNotHere)
  1086. }
  1087. s.recordDrop(contents, c.key, dstKey, reason)
  1088. c.debugLogf("SendPacket for %s, dropping with reason=%s", dstKey.ShortString(), reason)
  1089. return nil
  1090. }
  1091. c.debugLogf("SendPacket for %s, sending directly", dstKey.ShortString())
  1092. p := pkt{
  1093. bs: contents,
  1094. enqueuedAt: c.s.clock.Now(),
  1095. src: c.key,
  1096. }
  1097. return c.sendPkt(dst, p)
  1098. }
  1099. func (c *sclient) debugLogf(format string, v ...any) {
  1100. if c.debug {
  1101. c.logf(format, v...)
  1102. }
  1103. }
  1104. type dropReasonKindLabels struct {
  1105. Reason string // metric label corresponding to a given dropReason
  1106. Kind string // either `disco` or `other`
  1107. }
  1108. // dropReason is why we dropped a DERP frame.
  1109. type dropReason string
  1110. const (
  1111. dropReasonUnknownDest dropReason = "unknown_dest" // unknown destination pubkey
  1112. dropReasonUnknownDestOnFwd dropReason = "unknown_dest_on_fwd" // unknown destination pubkey on a derp-forwarded packet
  1113. dropReasonGoneDisconnected dropReason = "gone_disconnected" // destination tailscaled disconnected before we could send
  1114. dropReasonQueueHead dropReason = "queue_head" // destination queue is full, dropped packet at queue head
  1115. dropReasonQueueTail dropReason = "queue_tail" // destination queue is full, dropped packet at queue tail
  1116. dropReasonWriteError dropReason = "write_error" // OS write() failed
  1117. dropReasonDupClient dropReason = "dup_client" // the public key is connected 2+ times (active/active, fighting)
  1118. )
  1119. func (s *Server) recordDrop(packetBytes []byte, srcKey, dstKey key.NodePublic, reason dropReason) {
  1120. labels := dropReasonKindLabels{
  1121. Reason: string(reason),
  1122. }
  1123. looksDisco := disco.LooksLikeDiscoWrapper(packetBytes)
  1124. if looksDisco {
  1125. labels.Kind = string(packetKindDisco)
  1126. } else {
  1127. labels.Kind = string(packetKindOther)
  1128. }
  1129. packetsDropped.Add(labels, 1)
  1130. bytesDropped.Add(labels, int64(len(packetBytes)))
  1131. if verboseDropKeys[dstKey] {
  1132. // Preformat the log string prior to calling limitedLogf. The
  1133. // limiter acts based on the format string, and we want to
  1134. // rate-limit per src/dst keys, not on the generic "dropped
  1135. // stuff" message.
  1136. msg := fmt.Sprintf("drop (%s) %s -> %s", srcKey.ShortString(), reason, dstKey.ShortString())
  1137. s.limitedLogf(msg)
  1138. }
  1139. s.debugLogf("dropping packet reason=%s dst=%s disco=%v", reason, dstKey, looksDisco)
  1140. }
  1141. func (c *sclient) sendPkt(dst *sclient, p pkt) error {
  1142. s := c.s
  1143. dstKey := dst.key
  1144. // Attempt to queue for sending up to 3 times. On each attempt, if
  1145. // the queue is full, try to drop from queue head to prioritize
  1146. // fresher packets.
  1147. sendQueue := dst.sendQueue
  1148. if disco.LooksLikeDiscoWrapper(p.bs) {
  1149. sendQueue = dst.discoSendQueue
  1150. }
  1151. for attempt := 0; attempt < 3; attempt++ {
  1152. select {
  1153. case <-dst.done:
  1154. s.recordDrop(p.bs, c.key, dstKey, dropReasonGoneDisconnected)
  1155. dst.debugLogf("sendPkt attempt %d dropped, dst gone", attempt)
  1156. return nil
  1157. default:
  1158. }
  1159. select {
  1160. case sendQueue <- p:
  1161. dst.debugLogf("sendPkt attempt %d enqueued", attempt)
  1162. return nil
  1163. default:
  1164. }
  1165. select {
  1166. case pkt := <-sendQueue:
  1167. s.recordDrop(pkt.bs, c.key, dstKey, dropReasonQueueHead)
  1168. c.recordQueueTime(pkt.enqueuedAt)
  1169. default:
  1170. }
  1171. }
  1172. // Failed to make room for packet. This can happen in a heavily
  1173. // contended queue with racing writers. Give up and tail-drop in
  1174. // this case to keep reader unblocked.
  1175. s.recordDrop(p.bs, c.key, dstKey, dropReasonQueueTail)
  1176. dst.debugLogf("sendPkt attempt %d dropped, queue full")
  1177. return nil
  1178. }
  1179. // onPeerGoneFromRegion is the callback registered with the Server to be
  1180. // notified (in a new goroutine) whenever a peer has disconnected from all DERP
  1181. // nodes in the current region.
  1182. func (c *sclient) onPeerGoneFromRegion(peer key.NodePublic) {
  1183. c.requestPeerGoneWrite(peer, derp.PeerGoneReasonDisconnected)
  1184. }
  1185. // requestPeerGoneWrite sends a request to write a "peer gone" frame
  1186. // with an explanation of why it is gone. It blocks until either the
  1187. // write request is scheduled, or the client has closed.
  1188. func (c *sclient) requestPeerGoneWrite(peer key.NodePublic, reason derp.PeerGoneReasonType) {
  1189. select {
  1190. case c.peerGone <- peerGoneMsg{
  1191. peer: peer,
  1192. reason: reason,
  1193. }:
  1194. case <-c.done:
  1195. }
  1196. }
  1197. // requestMeshUpdate notes that a c's peerStateChange has been appended to and
  1198. // should now be written.
  1199. //
  1200. // It does not block. If a meshUpdate is already pending for this client, it
  1201. // does nothing.
  1202. func (c *sclient) requestMeshUpdate() {
  1203. if !c.canMesh {
  1204. panic("unexpected requestMeshUpdate")
  1205. }
  1206. select {
  1207. case c.meshUpdate <- struct{}{}:
  1208. default:
  1209. }
  1210. }
  1211. // isMeshPeer reports whether the client is a trusted mesh peer
  1212. // node in the DERP region.
  1213. func (s *Server) isMeshPeer(info *derp.ClientInfo) bool {
  1214. // Compare mesh keys in constant time to prevent timing attacks.
  1215. // Since mesh keys are a fixed length, we don’t need to be concerned
  1216. // about timing attacks on client mesh keys that are the wrong length.
  1217. // See https://github.com/tailscale/corp/issues/28720
  1218. if info == nil || info.MeshKey.IsZero() {
  1219. return false
  1220. }
  1221. return s.meshKey.Equal(info.MeshKey)
  1222. }
  1223. // verifyClient checks whether the client is allowed to connect to the derper,
  1224. // depending on how & whether the server's been configured to verify.
  1225. func (s *Server) verifyClient(ctx context.Context, clientKey key.NodePublic, info *derp.ClientInfo, clientIP netip.Addr) error {
  1226. if s.isMeshPeer(info) {
  1227. // Trusted mesh peer. No need to verify further. In fact, verifying
  1228. // further wouldn't work: it's not part of the tailnet so tailscaled and
  1229. // likely the admission control URL wouldn't know about it.
  1230. return nil
  1231. }
  1232. // tailscaled-based verification:
  1233. if s.verifyClientsLocalTailscaled {
  1234. _, err := s.localClient.WhoIsNodeKey(ctx, clientKey)
  1235. if err == local.ErrPeerNotFound {
  1236. return fmt.Errorf("peer %v not authorized (not found in local tailscaled)", clientKey)
  1237. }
  1238. if err != nil {
  1239. if strings.Contains(err.Error(), "invalid 'addr' parameter") {
  1240. // Issue 12617
  1241. return errors.New("tailscaled version is too old (out of sync with derper binary)")
  1242. }
  1243. return fmt.Errorf("failed to query local tailscaled status for %v: %w", clientKey, err)
  1244. }
  1245. }
  1246. // admission controller-based verification:
  1247. if s.verifyClientsURL != "" {
  1248. ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
  1249. defer cancel()
  1250. jreq, err := json.Marshal(&tailcfg.DERPAdmitClientRequest{
  1251. NodePublic: clientKey,
  1252. Source: clientIP,
  1253. })
  1254. if err != nil {
  1255. return err
  1256. }
  1257. req, err := http.NewRequestWithContext(ctx, "POST", s.verifyClientsURL, bytes.NewReader(jreq))
  1258. if err != nil {
  1259. return err
  1260. }
  1261. res, err := http.DefaultClient.Do(req)
  1262. if err != nil {
  1263. if s.verifyClientsURLFailOpen {
  1264. s.logf("admission controller unreachable; allowing client %v", clientKey)
  1265. return nil
  1266. }
  1267. return err
  1268. }
  1269. defer res.Body.Close()
  1270. if res.StatusCode != 200 {
  1271. return fmt.Errorf("admission controller: %v", res.Status)
  1272. }
  1273. var jres tailcfg.DERPAdmitClientResponse
  1274. if err := json.NewDecoder(io.LimitReader(res.Body, 4<<10)).Decode(&jres); err != nil {
  1275. return err
  1276. }
  1277. if !jres.Allow {
  1278. return fmt.Errorf("admission controller: %v/%v not allowed", clientKey, clientIP)
  1279. }
  1280. // TODO(bradfitz): add policy for configurable bandwidth rate per client?
  1281. }
  1282. return nil
  1283. }
  1284. func (s *Server) sendServerKey(lw *lazyBufioWriter) error {
  1285. buf := make([]byte, 0, len(derp.Magic)+key.NodePublicRawLen)
  1286. buf = append(buf, derp.Magic...)
  1287. buf = s.publicKey.AppendTo(buf)
  1288. err := derp.WriteFrame(lw.bw(), derp.FrameServerKey, buf)
  1289. lw.Flush() // redundant (no-op) flush to release bufio.Writer
  1290. return err
  1291. }
  1292. func (s *Server) noteClientActivity(c *sclient) {
  1293. if !c.isDup.Load() {
  1294. // Fast path for clients that aren't in a dup set.
  1295. return
  1296. }
  1297. if c.isDisabled.Load() {
  1298. // If they're already disabled, no point checking more.
  1299. return
  1300. }
  1301. s.mu.Lock()
  1302. defer s.mu.Unlock()
  1303. cs, ok := s.clients[c.key]
  1304. if !ok {
  1305. return
  1306. }
  1307. dup := cs.dup
  1308. if dup == nil {
  1309. // It became unduped in between the isDup fast path check above
  1310. // and the mutex check. Nothing to do.
  1311. return
  1312. }
  1313. if s.dupPolicy == lastWriterIsActive {
  1314. dup.last = c
  1315. cs.activeClient.Store(c)
  1316. } else if dup.last == nil {
  1317. // If we didn't have a primary, let the current
  1318. // speaker be the primary.
  1319. dup.last = c
  1320. cs.activeClient.Store(c)
  1321. }
  1322. if slicesx.LastEqual(dup.sendHistory, c) {
  1323. // The client c was the last client to make activity
  1324. // in this set and it was already recorded. Nothing to
  1325. // do.
  1326. return
  1327. }
  1328. // If we saw this connection send previously, then consider
  1329. // the group fighting and disable them all.
  1330. if s.dupPolicy == disableFighters {
  1331. for _, prior := range dup.sendHistory {
  1332. if prior == c {
  1333. cs.ForeachClient(func(c *sclient) {
  1334. c.isDisabled.Store(true)
  1335. if cs.activeClient.Load() == c {
  1336. cs.activeClient.Store(nil)
  1337. }
  1338. })
  1339. break
  1340. }
  1341. }
  1342. }
  1343. // Append this client to the list of clients who spoke last.
  1344. dup.sendHistory = append(dup.sendHistory, c)
  1345. }
  1346. type ServerInfo = derp.ServerInfo
  1347. func (s *Server) sendServerInfo(bw *lazyBufioWriter, clientKey key.NodePublic) error {
  1348. msg, err := json.Marshal(ServerInfo{Version: derp.ProtocolVersion})
  1349. if err != nil {
  1350. return err
  1351. }
  1352. msgbox := s.privateKey.SealTo(clientKey, msg)
  1353. if err := derp.WriteFrameHeader(bw.bw(), derp.FrameServerInfo, uint32(len(msgbox))); err != nil {
  1354. return err
  1355. }
  1356. if _, err := bw.Write(msgbox); err != nil {
  1357. return err
  1358. }
  1359. return bw.Flush()
  1360. }
  1361. // recvClientKey reads the frameClientInfo frame from the client (its
  1362. // proof of identity) upon its initial connection. It should be
  1363. // considered especially untrusted at this point.
  1364. func (s *Server) recvClientKey(br *bufio.Reader) (clientKey key.NodePublic, info *derp.ClientInfo, err error) {
  1365. fl, err := derp.ReadFrameTypeHeader(br, derp.FrameClientInfo)
  1366. if err != nil {
  1367. return zpub, nil, err
  1368. }
  1369. const minLen = derp.KeyLen + derp.NonceLen
  1370. if fl < minLen {
  1371. return zpub, nil, errors.New("short client info")
  1372. }
  1373. // We don't trust the client at all yet, so limit its input size to limit
  1374. // things like JSON resource exhausting (http://github.com/golang/go/issues/31789).
  1375. if fl > 256<<10 {
  1376. return zpub, nil, errors.New("long client info")
  1377. }
  1378. if err := clientKey.ReadRawWithoutAllocating(br); err != nil {
  1379. return zpub, nil, err
  1380. }
  1381. msgLen := int(fl - derp.KeyLen)
  1382. msgbox := make([]byte, msgLen)
  1383. if _, err := io.ReadFull(br, msgbox); err != nil {
  1384. return zpub, nil, fmt.Errorf("msgbox: %v", err)
  1385. }
  1386. msg, ok := s.privateKey.OpenFrom(clientKey, msgbox)
  1387. if !ok {
  1388. return zpub, nil, fmt.Errorf("msgbox: cannot open len=%d with client key %s", msgLen, clientKey)
  1389. }
  1390. info = new(derp.ClientInfo)
  1391. if err := json.Unmarshal(msg, info); err != nil {
  1392. return zpub, nil, fmt.Errorf("msg: %v", err)
  1393. }
  1394. return clientKey, info, nil
  1395. }
  1396. func (s *Server) recvPacket(br *bufio.Reader, frameLen uint32) (dstKey key.NodePublic, contents []byte, err error) {
  1397. if frameLen < derp.KeyLen {
  1398. return zpub, nil, errors.New("short send packet frame")
  1399. }
  1400. if err := dstKey.ReadRawWithoutAllocating(br); err != nil {
  1401. return zpub, nil, err
  1402. }
  1403. packetLen := frameLen - derp.KeyLen
  1404. if packetLen > derp.MaxPacketSize {
  1405. return zpub, nil, fmt.Errorf("data packet longer (%d) than max of %v", packetLen, derp.MaxPacketSize)
  1406. }
  1407. contents = make([]byte, packetLen)
  1408. if _, err := io.ReadFull(br, contents); err != nil {
  1409. return zpub, nil, err
  1410. }
  1411. s.packetsRecv.Add(1)
  1412. s.bytesRecv.Add(int64(len(contents)))
  1413. if disco.LooksLikeDiscoWrapper(contents) {
  1414. s.packetsRecvDisco.Add(1)
  1415. } else {
  1416. s.packetsRecvOther.Add(1)
  1417. }
  1418. return dstKey, contents, nil
  1419. }
  1420. // zpub is the key.NodePublic zero value.
  1421. var zpub key.NodePublic
  1422. func (s *Server) recvForwardPacket(br *bufio.Reader, frameLen uint32) (srcKey, dstKey key.NodePublic, contents []byte, err error) {
  1423. if frameLen < derp.KeyLen*2 {
  1424. return zpub, zpub, nil, errors.New("short send packet frame")
  1425. }
  1426. if err := srcKey.ReadRawWithoutAllocating(br); err != nil {
  1427. return zpub, zpub, nil, err
  1428. }
  1429. if err := dstKey.ReadRawWithoutAllocating(br); err != nil {
  1430. return zpub, zpub, nil, err
  1431. }
  1432. packetLen := frameLen - derp.KeyLen*2
  1433. if packetLen > derp.MaxPacketSize {
  1434. return zpub, zpub, nil, fmt.Errorf("data packet longer (%d) than max of %v", packetLen, derp.MaxPacketSize)
  1435. }
  1436. contents = make([]byte, packetLen)
  1437. if _, err := io.ReadFull(br, contents); err != nil {
  1438. return zpub, zpub, nil, err
  1439. }
  1440. // TODO: was s.packetsRecv.Add(1)
  1441. // TODO: was s.bytesRecv.Add(int64(len(contents)))
  1442. return srcKey, dstKey, contents, nil
  1443. }
  1444. // sclient is a client connection to the server.
  1445. //
  1446. // A node (a wireguard public key) can be connected multiple times to a DERP server
  1447. // and thus have multiple sclient instances. An sclient represents
  1448. // only one of these possibly multiple connections. See clientSet for the
  1449. // type that represents the set of all connections for a given key.
  1450. //
  1451. // (The "s" prefix is to more explicitly distinguish it from Client in derp_client.go)
  1452. type sclient struct {
  1453. // Static after construction.
  1454. connNum int64 // process-wide unique counter, incremented each Accept
  1455. s *Server
  1456. nc derp.Conn
  1457. key key.NodePublic
  1458. info derp.ClientInfo
  1459. logf logger.Logf
  1460. done <-chan struct{} // closed when connection closes
  1461. remoteIPPort netip.AddrPort // zero if remoteAddr is not ip:port.
  1462. sendQueue chan pkt // packets queued to this client; never closed
  1463. discoSendQueue chan pkt // important packets queued to this client; never closed
  1464. sendPongCh chan [8]byte // pong replies to send to the client; never closed
  1465. peerGone chan peerGoneMsg // write request that a peer is not at this server (not used by mesh peers)
  1466. meshUpdate chan struct{} // write request to write peerStateChange
  1467. canMesh bool // clientInfo had correct mesh token for inter-region routing
  1468. isNotIdealConn bool // client indicated it is not its ideal node in the region
  1469. isDup atomic.Bool // whether more than 1 sclient for key is connected
  1470. isDisabled atomic.Bool // whether sends to this peer are disabled due to active/active dups
  1471. debug bool // turn on for verbose logging
  1472. // Owned by run, not thread-safe.
  1473. br *bufio.Reader
  1474. connectedAt time.Time
  1475. preferred bool
  1476. // Owned by sendLoop, not thread-safe.
  1477. sawSrc map[key.NodePublic]set.Handle
  1478. bw *lazyBufioWriter
  1479. // Guarded by s.mu
  1480. //
  1481. // peerStateChange is used by mesh peers (a set of regional
  1482. // DERP servers) and contains records that need to be sent to
  1483. // the client for them to update their map of who's connected
  1484. // to this node.
  1485. peerStateChange []peerConnState
  1486. // peerGoneLimiter limits how often the server will inform a
  1487. // client that it's trying to establish a direct connection
  1488. // through us with a peer we have no record of.
  1489. peerGoneLim *rate.Limiter
  1490. }
  1491. func (c *sclient) presentFlags() derp.PeerPresentFlags {
  1492. var f derp.PeerPresentFlags
  1493. if c.info.IsProber {
  1494. f |= derp.PeerPresentIsProber
  1495. }
  1496. if c.canMesh {
  1497. f |= derp.PeerPresentIsMeshPeer
  1498. }
  1499. if c.isNotIdealConn {
  1500. f |= derp.PeerPresentNotIdeal
  1501. }
  1502. if f == 0 {
  1503. return derp.PeerPresentIsRegular
  1504. }
  1505. return f
  1506. }
  1507. // peerConnState represents whether a peer is connected to the server
  1508. // or not.
  1509. type peerConnState struct {
  1510. ipPort netip.AddrPort // if present, the peer's IP:port
  1511. peer key.NodePublic
  1512. flags derp.PeerPresentFlags
  1513. present bool
  1514. }
  1515. // pkt is a request to write a data frame to an sclient.
  1516. type pkt struct {
  1517. // enqueuedAt is when a packet was put onto a queue before it was sent,
  1518. // and is used for reporting metrics on the duration of packets in the queue.
  1519. enqueuedAt time.Time
  1520. // bs is the data packet bytes.
  1521. // The memory is owned by pkt.
  1522. bs []byte
  1523. // src is the who's the sender of the packet.
  1524. src key.NodePublic
  1525. }
  1526. // peerGoneMsg is a request to write a peerGone frame to an sclient
  1527. type peerGoneMsg struct {
  1528. peer key.NodePublic
  1529. reason derp.PeerGoneReasonType
  1530. }
  1531. func (c *sclient) setPreferred(v bool) {
  1532. if c.preferred == v {
  1533. return
  1534. }
  1535. c.preferred = v
  1536. var homeMove *expvar.Int
  1537. if v {
  1538. c.s.curHomeClients.Add(1)
  1539. homeMove = &c.s.homeMovesIn
  1540. } else {
  1541. c.s.curHomeClients.Add(-1)
  1542. homeMove = &c.s.homeMovesOut
  1543. }
  1544. // Keep track of varz for home serve moves in/out. But ignore
  1545. // the initial packet set when a client connects, which we
  1546. // assume happens within 5 seconds. In any case, just for
  1547. // graphs, so not important to miss a move. But it shouldn't:
  1548. // the netcheck/re-STUNs in magicsock only happen about every
  1549. // 30 seconds.
  1550. if c.s.clock.Since(c.connectedAt) > 5*time.Second {
  1551. homeMove.Add(1)
  1552. }
  1553. }
  1554. // expMovingAverage returns the new moving average given the previous average,
  1555. // a new value, and an alpha decay factor.
  1556. // https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
  1557. func expMovingAverage(prev, newValue, alpha float64) float64 {
  1558. return alpha*newValue + (1-alpha)*prev
  1559. }
  1560. // recordQueueTime updates the average queue duration metric after a packet has been sent.
  1561. func (c *sclient) recordQueueTime(enqueuedAt time.Time) {
  1562. elapsed := float64(c.s.clock.Since(enqueuedAt).Milliseconds())
  1563. for {
  1564. old := atomic.LoadUint64(c.s.avgQueueDuration)
  1565. newAvg := expMovingAverage(math.Float64frombits(old), elapsed, 0.1)
  1566. if atomic.CompareAndSwapUint64(c.s.avgQueueDuration, old, math.Float64bits(newAvg)) {
  1567. break
  1568. }
  1569. }
  1570. }
  1571. // onSendLoopDone is called when the send loop is done
  1572. // to clean up.
  1573. //
  1574. // It must only be called from the sendLoop goroutine.
  1575. func (c *sclient) onSendLoopDone() {
  1576. // If the sender shuts down unilaterally due to an error, close so
  1577. // that the receive loop unblocks and cleans up the rest.
  1578. c.nc.Close()
  1579. // Clean up watches.
  1580. for peer, h := range c.sawSrc {
  1581. c.s.removePeerGoneFromRegionWatcher(peer, h)
  1582. }
  1583. // Drain the send queue to count dropped packets
  1584. for {
  1585. select {
  1586. case pkt := <-c.sendQueue:
  1587. c.s.recordDrop(pkt.bs, pkt.src, c.key, dropReasonGoneDisconnected)
  1588. case pkt := <-c.discoSendQueue:
  1589. c.s.recordDrop(pkt.bs, pkt.src, c.key, dropReasonGoneDisconnected)
  1590. default:
  1591. return
  1592. }
  1593. }
  1594. }
  1595. func (c *sclient) sendLoop(ctx context.Context) error {
  1596. defer c.onSendLoopDone()
  1597. jitter := rand.N(5 * time.Second)
  1598. keepAliveTick, keepAliveTickChannel := c.s.clock.NewTicker(derp.KeepAlive + jitter)
  1599. defer keepAliveTick.Stop()
  1600. var werr error // last write error
  1601. inBatch := -1 // for bufferedWriteFrames
  1602. for {
  1603. if werr != nil {
  1604. return werr
  1605. }
  1606. inBatch++
  1607. // First, a non-blocking select (with a default) that
  1608. // does as many non-flushing writes as possible.
  1609. select {
  1610. case <-ctx.Done():
  1611. return nil
  1612. case msg := <-c.peerGone:
  1613. werr = c.sendPeerGone(msg.peer, msg.reason)
  1614. continue
  1615. case <-c.meshUpdate:
  1616. werr = c.sendMeshUpdates()
  1617. continue
  1618. case msg := <-c.sendQueue:
  1619. werr = c.sendPacket(msg.src, msg.bs)
  1620. c.recordQueueTime(msg.enqueuedAt)
  1621. continue
  1622. case msg := <-c.discoSendQueue:
  1623. werr = c.sendPacket(msg.src, msg.bs)
  1624. c.recordQueueTime(msg.enqueuedAt)
  1625. continue
  1626. case msg := <-c.sendPongCh:
  1627. werr = c.sendPong(msg)
  1628. continue
  1629. case <-keepAliveTickChannel:
  1630. werr = c.sendKeepAlive()
  1631. continue
  1632. default:
  1633. // Flush any writes from the 3 sends above, or from
  1634. // the blocking loop below.
  1635. if werr = c.bw.Flush(); werr != nil {
  1636. return werr
  1637. }
  1638. if inBatch != 0 { // the first loop will almost always hit default & be size zero
  1639. c.s.bufferedWriteFrames.Observe(float64(inBatch))
  1640. inBatch = 0
  1641. }
  1642. }
  1643. // Then a blocking select with same:
  1644. select {
  1645. case <-ctx.Done():
  1646. return nil
  1647. case msg := <-c.peerGone:
  1648. werr = c.sendPeerGone(msg.peer, msg.reason)
  1649. case <-c.meshUpdate:
  1650. werr = c.sendMeshUpdates()
  1651. case msg := <-c.sendQueue:
  1652. werr = c.sendPacket(msg.src, msg.bs)
  1653. c.recordQueueTime(msg.enqueuedAt)
  1654. case msg := <-c.discoSendQueue:
  1655. werr = c.sendPacket(msg.src, msg.bs)
  1656. c.recordQueueTime(msg.enqueuedAt)
  1657. case msg := <-c.sendPongCh:
  1658. werr = c.sendPong(msg)
  1659. case <-keepAliveTickChannel:
  1660. werr = c.sendKeepAlive()
  1661. }
  1662. }
  1663. }
  1664. func (c *sclient) setWriteDeadline() {
  1665. d := c.s.tcpWriteTimeout
  1666. if c.canMesh {
  1667. // Trusted peers get more tolerance.
  1668. //
  1669. // The "canMesh" is a bit of a misnomer; mesh peers typically run over a
  1670. // different interface for a per-region private VPC and are not
  1671. // throttled. But monitoring software elsewhere over the internet also
  1672. // use the private mesh key to subscribe to connect/disconnect events
  1673. // and might hit throttling and need more time to get the initial dump
  1674. // of connected peers.
  1675. d = privilegedWriteTimeout
  1676. }
  1677. if d == 0 {
  1678. // A zero value should disable the write deadline per
  1679. // --tcp-write-timeout docs. The flag should only be applicable for
  1680. // non-mesh connections, again per its docs. If mesh happened to use a
  1681. // zero value constant above it would be a bug, so we don't bother
  1682. // with a condition on c.canMesh.
  1683. return
  1684. }
  1685. // Ignore the error from setting the write deadline. In practice,
  1686. // setting the deadline will only fail if the connection is closed
  1687. // or closing, so the subsequent Write() will fail anyway.
  1688. _ = c.nc.SetWriteDeadline(time.Now().Add(d))
  1689. }
  1690. // sendKeepAlive sends a keep-alive frame, without flushing.
  1691. func (c *sclient) sendKeepAlive() error {
  1692. c.setWriteDeadline()
  1693. return derp.WriteFrameHeader(c.bw.bw(), derp.FrameKeepAlive, 0)
  1694. }
  1695. // sendPong sends a pong reply, without flushing.
  1696. func (c *sclient) sendPong(data [8]byte) error {
  1697. c.s.sentPong.Add(1)
  1698. c.setWriteDeadline()
  1699. if err := derp.WriteFrameHeader(c.bw.bw(), derp.FramePong, uint32(len(data))); err != nil {
  1700. return err
  1701. }
  1702. _, err := c.bw.Write(data[:])
  1703. return err
  1704. }
  1705. const (
  1706. peerGoneFrameLen = derp.KeyLen + 1
  1707. peerPresentFrameLen = derp.KeyLen + 16 + 2 + 1 // 16 byte IP + 2 byte port + 1 byte flags
  1708. )
  1709. // sendPeerGone sends a peerGone frame, without flushing.
  1710. func (c *sclient) sendPeerGone(peer key.NodePublic, reason derp.PeerGoneReasonType) error {
  1711. switch reason {
  1712. case derp.PeerGoneReasonDisconnected:
  1713. c.s.peerGoneDisconnectedFrames.Add(1)
  1714. case derp.PeerGoneReasonNotHere:
  1715. c.s.peerGoneNotHereFrames.Add(1)
  1716. }
  1717. c.setWriteDeadline()
  1718. data := make([]byte, 0, peerGoneFrameLen)
  1719. data = peer.AppendTo(data)
  1720. data = append(data, byte(reason))
  1721. if err := derp.WriteFrameHeader(c.bw.bw(), derp.FramePeerGone, uint32(len(data))); err != nil {
  1722. return err
  1723. }
  1724. _, err := c.bw.Write(data)
  1725. return err
  1726. }
  1727. // sendPeerPresent sends a peerPresent frame, without flushing.
  1728. func (c *sclient) sendPeerPresent(peer key.NodePublic, ipPort netip.AddrPort, flags derp.PeerPresentFlags) error {
  1729. c.setWriteDeadline()
  1730. if err := derp.WriteFrameHeader(c.bw.bw(), derp.FramePeerPresent, peerPresentFrameLen); err != nil {
  1731. return err
  1732. }
  1733. payload := make([]byte, peerPresentFrameLen)
  1734. _ = peer.AppendTo(payload[:0])
  1735. a16 := ipPort.Addr().As16()
  1736. copy(payload[derp.KeyLen:], a16[:])
  1737. binary.BigEndian.PutUint16(payload[derp.KeyLen+16:], ipPort.Port())
  1738. payload[derp.KeyLen+18] = byte(flags)
  1739. _, err := c.bw.Write(payload)
  1740. return err
  1741. }
  1742. // sendMeshUpdates drains all mesh peerStateChange entries into the write buffer
  1743. // without flushing.
  1744. func (c *sclient) sendMeshUpdates() error {
  1745. var lastBatch []peerConnState // memory to best effort reuse
  1746. // takeAll returns c.peerStateChange and empties it.
  1747. takeAll := func() []peerConnState {
  1748. c.s.mu.Lock()
  1749. defer c.s.mu.Unlock()
  1750. if len(c.peerStateChange) == 0 {
  1751. return nil
  1752. }
  1753. batch := c.peerStateChange
  1754. if cap(lastBatch) > 16 {
  1755. lastBatch = nil
  1756. }
  1757. c.peerStateChange = lastBatch[:0]
  1758. return batch
  1759. }
  1760. for loops := 0; ; loops++ {
  1761. batch := takeAll()
  1762. if len(batch) == 0 {
  1763. c.s.meshUpdateLoopCount.Observe(float64(loops))
  1764. return nil
  1765. }
  1766. c.s.meshUpdateBatchSize.Observe(float64(len(batch)))
  1767. for _, pcs := range batch {
  1768. var err error
  1769. if pcs.present {
  1770. err = c.sendPeerPresent(pcs.peer, pcs.ipPort, pcs.flags)
  1771. } else {
  1772. err = c.sendPeerGone(pcs.peer, derp.PeerGoneReasonDisconnected)
  1773. }
  1774. if err != nil {
  1775. return err
  1776. }
  1777. }
  1778. lastBatch = batch
  1779. }
  1780. }
  1781. // sendPacket writes contents to the client in a RecvPacket frame. If
  1782. // srcKey.IsZero, uses the old DERPv1 framing format, otherwise uses
  1783. // DERPv2. The bytes of contents are only valid until this function
  1784. // returns, do not retain slices.
  1785. // It does not flush its bufio.Writer.
  1786. func (c *sclient) sendPacket(srcKey key.NodePublic, contents []byte) (err error) {
  1787. defer func() {
  1788. // Stats update.
  1789. if err != nil {
  1790. c.s.recordDrop(contents, srcKey, c.key, dropReasonWriteError)
  1791. } else {
  1792. c.s.packetsSent.Add(1)
  1793. c.s.bytesSent.Add(int64(len(contents)))
  1794. }
  1795. c.debugLogf("sendPacket from %s: %v", srcKey.ShortString(), err)
  1796. }()
  1797. c.setWriteDeadline()
  1798. withKey := !srcKey.IsZero()
  1799. pktLen := len(contents)
  1800. if withKey {
  1801. pktLen += key.NodePublicRawLen
  1802. c.noteSendFromSrc(srcKey)
  1803. }
  1804. if err = derp.WriteFrameHeader(c.bw.bw(), derp.FrameRecvPacket, uint32(pktLen)); err != nil {
  1805. return err
  1806. }
  1807. if withKey {
  1808. if err := srcKey.WriteRawWithoutAllocating(c.bw.bw()); err != nil {
  1809. return err
  1810. }
  1811. }
  1812. _, err = c.bw.Write(contents)
  1813. return err
  1814. }
  1815. // noteSendFromSrc notes that we are about to write a packet
  1816. // from src to sclient.
  1817. //
  1818. // It must only be called from the sendLoop goroutine.
  1819. func (c *sclient) noteSendFromSrc(src key.NodePublic) {
  1820. if _, ok := c.sawSrc[src]; ok {
  1821. return
  1822. }
  1823. h := c.s.addPeerGoneFromRegionWatcher(src, c.onPeerGoneFromRegion)
  1824. mak.Set(&c.sawSrc, src, h)
  1825. }
  1826. // AddPacketForwarder registers fwd as a packet forwarder for dst.
  1827. // fwd must be comparable.
  1828. func (s *Server) AddPacketForwarder(dst key.NodePublic, fwd PacketForwarder) {
  1829. s.mu.Lock()
  1830. defer s.mu.Unlock()
  1831. if prev, ok := s.clientsMesh[dst]; ok {
  1832. if prev == fwd {
  1833. // Duplicate registration of same forwarder. Ignore.
  1834. return
  1835. }
  1836. if m, ok := prev.(*multiForwarder); ok {
  1837. if _, ok := m.all[fwd]; ok {
  1838. // Duplicate registration of same forwarder in set; ignore.
  1839. return
  1840. }
  1841. m.add(fwd)
  1842. return
  1843. }
  1844. if prev != nil {
  1845. // Otherwise, the existing value is not a set,
  1846. // not a dup, and not local-only (nil) so make
  1847. // it a set. `prev` existed first, so will have higher
  1848. // priority.
  1849. fwd = newMultiForwarder(prev, fwd)
  1850. s.multiForwarderCreated.Add(1)
  1851. }
  1852. }
  1853. s.clientsMesh[dst] = fwd
  1854. }
  1855. // RemovePacketForwarder removes fwd as a packet forwarder for dst.
  1856. // fwd must be comparable.
  1857. func (s *Server) RemovePacketForwarder(dst key.NodePublic, fwd PacketForwarder) {
  1858. s.mu.Lock()
  1859. defer s.mu.Unlock()
  1860. v, ok := s.clientsMesh[dst]
  1861. if !ok {
  1862. return
  1863. }
  1864. if m, ok := v.(*multiForwarder); ok {
  1865. if len(m.all) < 2 {
  1866. panic("unexpected")
  1867. }
  1868. if remain, isLast := m.deleteLocked(fwd); isLast {
  1869. // If fwd was in m and we no longer need to be a
  1870. // multiForwarder, replace the entry with the
  1871. // remaining PacketForwarder.
  1872. s.clientsMesh[dst] = remain
  1873. s.multiForwarderDeleted.Add(1)
  1874. }
  1875. return
  1876. }
  1877. if v != fwd {
  1878. s.removePktForwardOther.Add(1)
  1879. // Delete of an entry that wasn't in the
  1880. // map. Harmless, so ignore.
  1881. // (This might happen if a user is moving around
  1882. // between nodes and/or the server sent duplicate
  1883. // connection change broadcasts.)
  1884. return
  1885. }
  1886. if _, isLocal := s.clients[dst]; isLocal {
  1887. s.clientsMesh[dst] = nil
  1888. } else {
  1889. delete(s.clientsMesh, dst)
  1890. s.notePeerGoneFromRegionLocked(dst)
  1891. }
  1892. }
  1893. // multiForwarder is a PacketForwarder that represents a set of
  1894. // forwarding options. It's used in the rare cases that a client is
  1895. // connected to multiple DERP nodes in a region. That shouldn't really
  1896. // happen except for perhaps during brief moments while the client is
  1897. // reconfiguring, in which case we don't want to forget where the
  1898. // client is. The map value is unique connection number; the lowest
  1899. // one has been seen the longest. It's used to make sure we forward
  1900. // packets consistently to the same node and don't pick randomly.
  1901. type multiForwarder struct {
  1902. fwd syncs.AtomicValue[PacketForwarder] // preferred forwarder.
  1903. all map[PacketForwarder]uint8 // all forwarders, protected by s.mu.
  1904. }
  1905. // newMultiForwarder creates a new multiForwarder.
  1906. // The first PacketForwarder passed to this function will be the preferred one.
  1907. func newMultiForwarder(fwds ...PacketForwarder) *multiForwarder {
  1908. f := &multiForwarder{all: make(map[PacketForwarder]uint8)}
  1909. f.fwd.Store(fwds[0])
  1910. for idx, fwd := range fwds {
  1911. f.all[fwd] = uint8(idx)
  1912. }
  1913. return f
  1914. }
  1915. // add adds a new forwarder to the map with a connection number that
  1916. // is higher than the existing ones.
  1917. func (f *multiForwarder) add(fwd PacketForwarder) {
  1918. var max uint8
  1919. for _, v := range f.all {
  1920. if v > max {
  1921. max = v
  1922. }
  1923. }
  1924. f.all[fwd] = max + 1
  1925. }
  1926. // deleteLocked removes a packet forwarder from the map. It expects Server.mu to be held.
  1927. // If only one forwarder remains after the removal, it will be returned alongside a `true` boolean value.
  1928. func (f *multiForwarder) deleteLocked(fwd PacketForwarder) (_ PacketForwarder, isLast bool) {
  1929. delete(f.all, fwd)
  1930. if fwd == f.fwd.Load() {
  1931. // The preferred forwarder has been removed, choose a new one
  1932. // based on the lowest index.
  1933. var lowestfwd PacketForwarder
  1934. var lowest uint8
  1935. for k, v := range f.all {
  1936. if lowestfwd == nil || v < lowest {
  1937. lowestfwd = k
  1938. lowest = v
  1939. }
  1940. }
  1941. if lowestfwd != nil {
  1942. f.fwd.Store(lowestfwd)
  1943. }
  1944. }
  1945. if len(f.all) == 1 {
  1946. for k := range f.all {
  1947. return k, true
  1948. }
  1949. }
  1950. return nil, false
  1951. }
  1952. func (f *multiForwarder) ForwardPacket(src, dst key.NodePublic, payload []byte) error {
  1953. return f.fwd.Load().ForwardPacket(src, dst, payload)
  1954. }
  1955. func (f *multiForwarder) String() string {
  1956. return fmt.Sprintf("<MultiForwarder fwd=%s total=%d>", f.fwd.Load(), len(f.all))
  1957. }
  1958. func (s *Server) expVarFunc(f func() any) expvar.Func {
  1959. return expvar.Func(func() any {
  1960. s.mu.Lock()
  1961. defer s.mu.Unlock()
  1962. return f()
  1963. })
  1964. }
  1965. // ExpVar returns an expvar variable suitable for registering with expvar.Publish.
  1966. func (s *Server) ExpVar() expvar.Var {
  1967. m := new(metrics.Set)
  1968. m.Set("gauge_memstats_sys0", expvar.Func(func() any { return int64(s.memSys0) }))
  1969. m.Set("gauge_watchers", s.expVarFunc(func() any { return len(s.watchers) }))
  1970. m.Set("gauge_current_file_descriptors", expvar.Func(func() any { return metrics.CurrentFDs() }))
  1971. m.Set("gauge_current_connections", &s.curClients)
  1972. m.Set("gauge_current_home_connections", &s.curHomeClients)
  1973. m.Set("gauge_current_notideal_connections", &s.curClientsNotIdeal)
  1974. m.Set("gauge_clients_total", expvar.Func(func() any { return len(s.clientsMesh) }))
  1975. m.Set("gauge_clients_local", expvar.Func(func() any { return len(s.clients) }))
  1976. m.Set("gauge_clients_remote", expvar.Func(func() any { return len(s.clientsMesh) - len(s.clients) }))
  1977. m.Set("gauge_current_dup_client_keys", &s.dupClientKeys)
  1978. m.Set("gauge_current_dup_client_conns", &s.dupClientConns)
  1979. m.Set("counter_total_dup_client_conns", &s.dupClientConnTotal)
  1980. m.Set("accepts", &s.accepts)
  1981. m.Set("bytes_received", &s.bytesRecv)
  1982. m.Set("bytes_sent", &s.bytesSent)
  1983. m.Set("counter_packets_received_kind", &s.packetsRecvByKind)
  1984. m.Set("packets_sent", &s.packetsSent)
  1985. m.Set("packets_received", &s.packetsRecv)
  1986. m.Set("unknown_frames", &s.unknownFrames)
  1987. m.Set("home_moves_in", &s.homeMovesIn)
  1988. m.Set("home_moves_out", &s.homeMovesOut)
  1989. m.Set("got_ping", &s.gotPing)
  1990. m.Set("sent_pong", &s.sentPong)
  1991. m.Set("peer_gone_disconnected_frames", &s.peerGoneDisconnectedFrames)
  1992. m.Set("peer_gone_not_here_frames", &s.peerGoneNotHereFrames)
  1993. m.Set("packets_forwarded_out", &s.packetsForwardedOut)
  1994. m.Set("packets_forwarded_in", &s.packetsForwardedIn)
  1995. m.Set("multiforwarder_created", &s.multiForwarderCreated)
  1996. m.Set("multiforwarder_deleted", &s.multiForwarderDeleted)
  1997. m.Set("packet_forwarder_delete_other_value", &s.removePktForwardOther)
  1998. m.Set("sclient_write_timeouts", &s.sclientWriteTimeouts)
  1999. m.Set("average_queue_duration_ms", expvar.Func(func() any {
  2000. return math.Float64frombits(atomic.LoadUint64(s.avgQueueDuration))
  2001. }))
  2002. m.Set("counter_tcp_rtt", &s.tcpRtt)
  2003. m.Set("counter_mesh_update_batch_size", s.meshUpdateBatchSize)
  2004. m.Set("counter_mesh_update_loop_count", s.meshUpdateLoopCount)
  2005. m.Set("counter_buffered_write_frames", s.bufferedWriteFrames)
  2006. var expvarVersion expvar.String
  2007. expvarVersion.Set(version.Long())
  2008. m.Set("version", &expvarVersion)
  2009. return m
  2010. }
  2011. func (s *Server) ConsistencyCheck() error {
  2012. s.mu.Lock()
  2013. defer s.mu.Unlock()
  2014. var errs []string
  2015. var nilMeshNotInClient int
  2016. for k, f := range s.clientsMesh {
  2017. if f == nil {
  2018. if _, ok := s.clients[k]; !ok {
  2019. nilMeshNotInClient++
  2020. }
  2021. }
  2022. }
  2023. if nilMeshNotInClient != 0 {
  2024. errs = append(errs, fmt.Sprintf("%d s.clientsMesh keys not in s.clients", nilMeshNotInClient))
  2025. }
  2026. var clientNotInMesh int
  2027. for k := range s.clients {
  2028. if _, ok := s.clientsMesh[k]; !ok {
  2029. clientNotInMesh++
  2030. }
  2031. }
  2032. if clientNotInMesh != 0 {
  2033. errs = append(errs, fmt.Sprintf("%d s.clients keys not in s.clientsMesh", clientNotInMesh))
  2034. }
  2035. if s.curClients.Value() != int64(len(s.clients)) {
  2036. errs = append(errs, fmt.Sprintf("expvar connections = %d != clients map says of %d",
  2037. s.curClients.Value(),
  2038. len(s.clients)))
  2039. }
  2040. if s.verifyClientsLocalTailscaled {
  2041. if err := s.checkVerifyClientsLocalTailscaled(); err != nil {
  2042. errs = append(errs, err.Error())
  2043. }
  2044. }
  2045. if len(errs) == 0 {
  2046. return nil
  2047. }
  2048. return errors.New(strings.Join(errs, ", "))
  2049. }
  2050. // checkVerifyClientsLocalTailscaled checks that a verifyClients call can be made successfully for the derper hosts own node key.
  2051. func (s *Server) checkVerifyClientsLocalTailscaled() error {
  2052. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  2053. defer cancel()
  2054. status, err := s.localClient.StatusWithoutPeers(ctx)
  2055. if err != nil {
  2056. return fmt.Errorf("localClient.Status: %w", err)
  2057. }
  2058. info := &derp.ClientInfo{
  2059. IsProber: true,
  2060. }
  2061. clientIP := netip.IPv6Loopback()
  2062. if err := s.verifyClient(ctx, status.Self.PublicKey, info, clientIP); err != nil {
  2063. return fmt.Errorf("verifyClient for self nodekey: %w", err)
  2064. }
  2065. return nil
  2066. }
  2067. const minTimeBetweenLogs = 2 * time.Second
  2068. // BytesSentRecv records the number of bytes that have been sent since the last traffic check
  2069. // for a given process, as well as the public key of the process sending those bytes.
  2070. type BytesSentRecv struct {
  2071. Sent uint64
  2072. Recv uint64
  2073. // Key is the public key of the client which sent/received these bytes.
  2074. Key key.NodePublic
  2075. }
  2076. // parseSSOutput parses the output from the specific call to ss in ServeDebugTraffic.
  2077. // Separated out for ease of testing.
  2078. func parseSSOutput(raw string) map[netip.AddrPort]BytesSentRecv {
  2079. newState := map[netip.AddrPort]BytesSentRecv{}
  2080. // parse every 2 lines and get src and dst ips, and kv pairs
  2081. lines := strings.Split(raw, "\n")
  2082. for i := 0; i < len(lines); i += 2 {
  2083. ipInfo := strings.Fields(strings.TrimSpace(lines[i]))
  2084. if len(ipInfo) < 5 {
  2085. continue
  2086. }
  2087. src, err := netip.ParseAddrPort(ipInfo[4])
  2088. if err != nil {
  2089. continue
  2090. }
  2091. stats := strings.Fields(strings.TrimSpace(lines[i+1]))
  2092. stat := BytesSentRecv{}
  2093. for _, s := range stats {
  2094. if strings.Contains(s, "bytes_sent") {
  2095. sent, err := strconv.Atoi(s[strings.Index(s, ":")+1:])
  2096. if err == nil {
  2097. stat.Sent = uint64(sent)
  2098. }
  2099. } else if strings.Contains(s, "bytes_received") {
  2100. recv, err := strconv.Atoi(s[strings.Index(s, ":")+1:])
  2101. if err == nil {
  2102. stat.Recv = uint64(recv)
  2103. }
  2104. }
  2105. }
  2106. newState[src] = stat
  2107. }
  2108. return newState
  2109. }
  2110. func (s *Server) ServeDebugTraffic(w http.ResponseWriter, r *http.Request) {
  2111. prevState := map[netip.AddrPort]BytesSentRecv{}
  2112. enc := json.NewEncoder(w)
  2113. for r.Context().Err() == nil {
  2114. output, err := exec.Command("ss", "-i", "-H", "-t").Output()
  2115. if err != nil {
  2116. fmt.Fprintf(w, "ss failed: %v", err)
  2117. return
  2118. }
  2119. newState := parseSSOutput(string(output))
  2120. s.mu.Lock()
  2121. for k, next := range newState {
  2122. prev := prevState[k]
  2123. if prev.Sent < next.Sent || prev.Recv < next.Recv {
  2124. if pkey, ok := s.keyOfAddr[k]; ok {
  2125. next.Key = pkey
  2126. if err := enc.Encode(next); err != nil {
  2127. s.mu.Unlock()
  2128. return
  2129. }
  2130. }
  2131. }
  2132. }
  2133. s.mu.Unlock()
  2134. prevState = newState
  2135. if _, err := fmt.Fprintln(w); err != nil {
  2136. return
  2137. }
  2138. if f, ok := w.(http.Flusher); ok {
  2139. f.Flush()
  2140. }
  2141. time.Sleep(minTimeBetweenLogs)
  2142. }
  2143. }
  2144. var bufioWriterPool = &sync.Pool{
  2145. New: func() any {
  2146. return bufio.NewWriterSize(io.Discard, 2<<10)
  2147. },
  2148. }
  2149. // lazyBufioWriter is a bufio.Writer-like wrapping writer that lazily
  2150. // allocates its actual bufio.Writer from a sync.Pool, releasing it to
  2151. // the pool upon flush.
  2152. //
  2153. // We do this to reduce memory overhead; most DERP connections are
  2154. // idle and the idle bufio.Writers were 30% of overall memory usage.
  2155. type lazyBufioWriter struct {
  2156. w io.Writer // underlying
  2157. lbw *bufio.Writer // lazy; nil means it needs an associated buffer
  2158. }
  2159. func (w *lazyBufioWriter) bw() *bufio.Writer {
  2160. if w.lbw == nil {
  2161. w.lbw = bufioWriterPool.Get().(*bufio.Writer)
  2162. w.lbw.Reset(w.w)
  2163. }
  2164. return w.lbw
  2165. }
  2166. func (w *lazyBufioWriter) Available() int { return w.bw().Available() }
  2167. func (w *lazyBufioWriter) Write(p []byte) (int, error) { return w.bw().Write(p) }
  2168. func (w *lazyBufioWriter) Flush() error {
  2169. if w.lbw == nil {
  2170. return nil
  2171. }
  2172. err := w.lbw.Flush()
  2173. w.lbw.Reset(io.Discard)
  2174. bufioWriterPool.Put(w.lbw)
  2175. w.lbw = nil
  2176. return err
  2177. }