mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
fix: direct embedded derp traffic directly to the server (#6595)
Prior to this change, DERP traffic would route from `coderd` to the `CODER_ACCESS_URL` to reach the internal DERP server, which may have resulted in slower connections due to proxying, or the failure of web traffic entirely. If your Coder deployment has a proxy in front of it, your traffic through web terminals, apps, and port-forwarding is about to get a lot faster!
This commit is contained in:
@ -258,7 +258,7 @@ func NewOptions(t *testing.T, options *Options) (func(http.Handler), context.Can
|
||||
stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{})
|
||||
t.Cleanup(stunCleanup)
|
||||
|
||||
derpServer := derp.NewServer(key.NewNode(), tailnet.Logger(slogtest.Make(t, nil).Named("derp")))
|
||||
derpServer := derp.NewServer(key.NewNode(), tailnet.Logger(slogtest.Make(t, nil).Named("derp").Leveled(slog.LevelDebug)))
|
||||
derpServer.SetMeshKey("test-key")
|
||||
|
||||
// match default with cli default
|
||||
|
@ -1,6 +1,7 @@
|
||||
package coderd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
@ -428,44 +429,9 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
|
||||
ctx := r.Context()
|
||||
clientConn, serverConn := net.Pipe()
|
||||
|
||||
derpMap := api.DERPMap.Clone()
|
||||
for _, region := range derpMap.Regions {
|
||||
if !region.EmbeddedRelay {
|
||||
continue
|
||||
}
|
||||
var node *tailcfg.DERPNode
|
||||
for _, n := range region.Nodes {
|
||||
if n.STUNOnly {
|
||||
continue
|
||||
}
|
||||
node = n
|
||||
break
|
||||
}
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
// TODO: This should dial directly to execute the
|
||||
// DERP server instead of contacting localhost.
|
||||
//
|
||||
// This requires modification of Tailscale internals
|
||||
// to pipe through a proxy function per-region, so
|
||||
// this is an easy and mostly reliable hack for now.
|
||||
cloned := node.Clone()
|
||||
// Add p for proxy.
|
||||
// This first node supports TLS.
|
||||
cloned.Name += "p"
|
||||
cloned.IPv4 = "127.0.0.1"
|
||||
cloned.InsecureForTests = true
|
||||
region.Nodes = append(region.Nodes, cloned.Clone())
|
||||
// This second node forces HTTP.
|
||||
cloned.Name += "-http"
|
||||
cloned.ForceHTTP = true
|
||||
region.Nodes = append(region.Nodes, cloned)
|
||||
}
|
||||
|
||||
conn, err := tailnet.NewConn(&tailnet.Options{
|
||||
Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(), 128)},
|
||||
DERPMap: derpMap,
|
||||
DERPMap: api.DERPMap,
|
||||
Logger: api.Logger.Named("tailnet"),
|
||||
})
|
||||
if err != nil {
|
||||
@ -473,6 +439,19 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (*
|
||||
_ = serverConn.Close()
|
||||
return nil, xerrors.Errorf("create tailnet conn: %w", err)
|
||||
}
|
||||
conn.SetDERPRegionDialer(func(_ context.Context, region *tailcfg.DERPRegion) net.Conn {
|
||||
if !region.EmbeddedRelay {
|
||||
return nil
|
||||
}
|
||||
left, right := net.Pipe()
|
||||
go func() {
|
||||
defer left.Close()
|
||||
defer right.Close()
|
||||
brw := bufio.NewReadWriter(bufio.NewReader(right), bufio.NewWriter(right))
|
||||
api.DERPServer.Accept(ctx, right, brw, r.RemoteAddr)
|
||||
}()
|
||||
return left
|
||||
})
|
||||
|
||||
sendNodes, _ := tailnet.ServeCoordinator(clientConn, func(node []*tailnet.Node) error {
|
||||
err = conn.UpdateNodes(node, true)
|
||||
|
Reference in New Issue
Block a user