@@ -23,23 +23,28 @@ import (
2323 "tailscale.com/tailcfg"
2424 "tailscale.com/types/key"
2525
26+ "github.com/coder/pretty"
2627 "github.com/coder/wush/cliui"
2728)
2829
29- func NewReceiveOverlay (logger * slog.Logger , dm * tailcfg.DERPMap ) * Receive {
30+ type Logf func (format string , args ... any )
31+
32+ func NewReceiveOverlay (logger * slog.Logger , hlog Logf , dm * tailcfg.DERPMap ) * Receive {
3033 return & Receive {
31- Logger : logger ,
32- DerpMap : dm ,
33- SelfPriv : key .NewNode (),
34- PeerPriv : key .NewNode (),
35- in : make (chan * tailcfg.Node , 8 ),
36- out : make (chan * tailcfg.Node , 8 ),
34+ Logger : logger ,
35+ HumanLogf : hlog ,
36+ DerpMap : dm ,
37+ SelfPriv : key .NewNode (),
38+ PeerPriv : key .NewNode (),
39+ in : make (chan * tailcfg.Node , 8 ),
40+ out : make (chan * tailcfg.Node , 8 ),
3741 }
3842}
3943
4044type Receive struct {
41- Logger * slog.Logger
42- DerpMap * tailcfg.DERPMap
45+ Logger * slog.Logger
46+ HumanLogf Logf
47+ DerpMap * tailcfg.DERPMap
4348 // SelfPriv is the private key that peers will encrypt overlay messages to.
4449 // The public key of this is sent in the auth key.
4550 SelfPriv key.NodePrivate
@@ -83,10 +88,10 @@ func (r *Receive) PickDERPHome(ctx context.Context) error {
8388 }
8489
8590 if report .PreferredDERP == 0 {
86- fmt . Println ("Failed to determine overlay DERP region, defaulting to" , cliui .Code ("NYC" ), "." )
91+ r . HumanLogf ("Failed to determine overlay DERP region, defaulting to %s. " , cliui .Code ("NYC" ))
8792 r .derpRegionID = 1
8893 } else {
89- fmt . Println ("Picked DERP region" , cliui .Code (r .DerpMap .Regions [report .PreferredDERP ].RegionName ), "as overlay home" )
94+ r . HumanLogf ("Picked DERP region %s as overlay home " , cliui .Code (r .DerpMap .Regions [report .PreferredDERP ].RegionName ))
9095 r .derpRegionID = uint16 (report .PreferredDERP )
9196 }
9297
@@ -139,7 +144,7 @@ func (r *Receive) ListenOverlaySTUN(ctx context.Context) (<-chan struct{}, error
139144 case <- restun .C :
140145 _ , err = conn .WriteToUDP (m .Raw , srvAddr )
141146 if err != nil {
142- fmt . Println ( cliui . Timestamp ( time . Now ()), " Failed to write STUN request on overlay:" , err )
147+ r . HumanLogf ( "%s Failed to write STUN request on overlay: %s" , cliui . Timestamp ( time . Now ()) , err )
143148 }
144149 restun .Reset (30 * time .Second )
145150 }
@@ -169,7 +174,7 @@ func (r *Receive) ListenOverlaySTUN(ctx context.Context) (<-chan struct{}, error
169174 peers .Range (func (_ key.NodePublic , addr netip.AddrPort ) bool {
170175 _ , err := conn .WriteToUDPAddrPort (sealed , addr )
171176 if err != nil {
172- fmt . Println ( " send response over udp:" , err )
177+ r . HumanLogf ( "%s Failed to send updated node over udp: %s" , cliui . Timestamp ( time . Now ()) , err )
173178 return false
174179 }
175180 return true
@@ -216,14 +221,11 @@ func (r *Receive) ListenOverlaySTUN(ctx context.Context) (<-chan struct{}, error
216221
217222 // our first STUN response
218223 if ! r .stunIP .IsValid () {
219- fmt . Println ( cliui . Timestamp ( time . Now ()), "STUN address is" , cliui .Code (stunAddrPort .String ()))
224+ r . HumanLogf ( "STUN address is %s " , cliui .Code (stunAddrPort .String ()))
220225 }
221226
222227 if r .stunIP .IsValid () && r .stunIP .Compare (stunAddrPort ) != 0 {
223- r .Logger .Warn ("STUN address changed, this may cause issues" ,
224- "old_ip" , r .stunIP .String (),
225- "new_ip" , stunAddrPort .String (),
226- )
228+ r .HumanLogf (pretty .Sprintf (cliui .DefaultStyles .Warn , "STUN address changed, this may cause issues; %s->%s" , r .stunIP .String (), stunAddrPort .String ()))
227229 }
228230 r .stunIP = stunAddrPort
229231 closeIPChanOnce .Do (func () {
@@ -234,7 +236,7 @@ func (r *Receive) ListenOverlaySTUN(ctx context.Context) (<-chan struct{}, error
234236
235237 res , key , err := r .handleNextMessage (buf , "STUN" )
236238 if err != nil {
237- fmt . Println ( cliui . Timestamp ( time . Now ()), "Failed to handle overlay message:" , err .Error ())
239+ r . HumanLogf ( "Failed to handle overlay message: %s " , err .Error ())
238240 continue
239241 }
240242
@@ -243,7 +245,7 @@ func (r *Receive) ListenOverlaySTUN(ctx context.Context) (<-chan struct{}, error
243245 if res != nil {
244246 _ , err = conn .WriteToUDPAddrPort (res , addr )
245247 if err != nil {
246- fmt . Println ( cliui . Timestamp ( time . Now ()), "Failed to send overlay response over STUN:" , err .Error ())
248+ r . HumanLogf ( "Failed to send overlay response over STUN: %s " , err .Error ())
247249 return
248250 }
249251 }
@@ -285,7 +287,7 @@ func (r *Receive) ListenOverlayDERP(ctx context.Context) error {
285287 peers .Range (func (_ , derpKey key.NodePublic ) bool {
286288 err = c .Send (derpKey , sealed )
287289 if err != nil {
288- fmt . Println ( "send response over derp: " , err )
290+ r . HumanLogf ( "Send updated node over DERP: %s " , err )
289291 return false
290292 }
291293 return true
@@ -304,7 +306,7 @@ func (r *Receive) ListenOverlayDERP(ctx context.Context) error {
304306 case derp.ReceivedPacket :
305307 res , key , err := r .handleNextMessage (msg .Data , "DERP" )
306308 if err != nil {
307- fmt . Println ( cliui . Timestamp ( time . Now ()), "Failed to handle overlay message:" , err .Error ())
309+ r . HumanLogf ( "Failed to handle overlay message: %s " , err .Error ())
308310 continue
309311 }
310312
@@ -313,7 +315,7 @@ func (r *Receive) ListenOverlayDERP(ctx context.Context) error {
313315 if res != nil {
314316 err = c .Send (msg .Source , res )
315317 if err != nil {
316- fmt . Println ( cliui . Timestamp ( time . Now ()), "Failed to send overlay response over derp:" , err .Error ())
318+ r . HumanLogf ( "Failed to send overlay response over derp: %s " , err .Error ())
317319 return err
318320 }
319321 }
@@ -350,9 +352,9 @@ func (r *Receive) handleNextMessage(msg []byte, system string) (resRaw []byte, n
350352 if h := ovMsg .HostInfo .Hostname ; h != "" {
351353 hostname = h
352354 }
353- fmt . Println ( cliui . Timestamp ( time . Now ()), " Received connection request over" , system , "from" , cliui .Keyword (fmt .Sprintf ("%s@%s" , username , hostname )))
355+ r . HumanLogf ( "%s Received connection request over %s from %s " , cliui . Timestamp ( time . Now ()), system , cliui .Keyword (fmt .Sprintf ("%s@%s" , username , hostname )))
354356 case messageTypeNodeUpdate :
355- fmt . Println ( cliui .Timestamp (time .Now ()), "Received updated node from" , cliui .Code (ovMsg .Node .Key .String ()))
357+ r . HumanLogf ( "%s Received updated node from %s" , cliui .Timestamp (time .Now ()), cliui .Code (ovMsg .Node .Key .String ()))
356358 r .in <- & ovMsg .Node
357359 res .Typ = messageTypeNodeUpdate
358360 if lastNode := r .lastNode .Load (); lastNode != nil {
0 commit comments