-
Notifications
You must be signed in to change notification settings - Fork 4
wgengine/magicsock: allow passing region dialer to derp client #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,6 +349,9 @@ type Conn struct { | |
// headers that are passed to the DERP HTTP client | ||
derpHeader atomic.Pointer[http.Header] | ||
|
||
// derpRegionDialer is passed to the DERP client | ||
derpRegionDialer atomic.Pointer[func(ctx context.Context, region *tailcfg.DERPRegion) net.Conn] | ||
|
||
// stats maintains per-connection counters. | ||
stats atomic.Pointer[connstats.Statistics] | ||
|
||
|
@@ -1514,6 +1517,10 @@ func (c *Conn) derpWriteChanOfAddr(addr netip.AddrPort, peer key.NodePublic) cha | |
if header != nil { | ||
dc.Header = header.Clone() | ||
} | ||
dialer := c.derpRegionDialer.Load() | ||
if dialer != nil { | ||
dc.SetRegionDialer(*dialer) | ||
} | ||
|
||
ctx, cancel := context.WithCancel(c.connCtx) | ||
ch := make(chan derpWriteRequest, bufferedDerpWritesBeforeDrop) | ||
|
@@ -2367,6 +2374,15 @@ func (c *Conn) SetDERPHeader(header http.Header) { | |
c.derpHeader.Store(&header) | ||
} | ||
|
||
func (c *Conn) SetDERPRegionDialer(dialer func(ctx context.Context, region *tailcfg.DERPRegion) net.Conn) { | ||
c.derpRegionDialer.Store(&dialer) | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
for _, d := range c.activeDerp { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any chance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
d.c.SetRegionDialer(dialer) | ||
} | ||
} | ||
|
||
func (c *Conn) SetNetworkUp(up bool) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't do this below, so I don't think it's necessary here.
Somewhat related tailscale#7557
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason it's OK below is that a defer func is created that checks the return error and closes
tcpConn
if there was an error. Here, however, regionDialer could have allocated resources/goroutines that need to be torn down or otherwise they will leak. That's why we need the close here.Edit: Granted, I didn't verify that it's actually OK below. As long as the TLS client doesn't need to be torn down, should be fine.
As for the websocket dial, it's unclear if we should do a close there as well or if that'll be guaranteed by the context (it'd definitely be safer to close there as well).