|
1 | 1 | package coderd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "database/sql" |
6 | 7 | "encoding/json" |
@@ -428,51 +429,29 @@ func (api *API) dialWorkspaceAgentTailnet(r *http.Request, agentID uuid.UUID) (* |
428 | 429 | ctx := r.Context() |
429 | 430 | clientConn, serverConn := net.Pipe() |
430 | 431 |
|
431 | | - derpMap := api.DERPMap.Clone() |
432 | | - for _, region := range derpMap.Regions { |
433 | | - if !region.EmbeddedRelay { |
434 | | - continue |
435 | | - } |
436 | | - var node *tailcfg.DERPNode |
437 | | - for _, n := range region.Nodes { |
438 | | - if n.STUNOnly { |
439 | | - continue |
440 | | - } |
441 | | - node = n |
442 | | - break |
443 | | - } |
444 | | - if node == nil { |
445 | | - continue |
446 | | - } |
447 | | - // TODO: This should dial directly to execute the |
448 | | - // DERP server instead of contacting localhost. |
449 | | - // |
450 | | - // This requires modification of Tailscale internals |
451 | | - // to pipe through a proxy function per-region, so |
452 | | - // this is an easy and mostly reliable hack for now. |
453 | | - cloned := node.Clone() |
454 | | - // Add p for proxy. |
455 | | - // This first node supports TLS. |
456 | | - cloned.Name += "p" |
457 | | - cloned.IPv4 = "127.0.0.1" |
458 | | - cloned.InsecureForTests = true |
459 | | - region.Nodes = append(region.Nodes, cloned.Clone()) |
460 | | - // This second node forces HTTP. |
461 | | - cloned.Name += "-http" |
462 | | - cloned.ForceHTTP = true |
463 | | - region.Nodes = append(region.Nodes, cloned) |
464 | | - } |
465 | | - |
466 | 432 | conn, err := tailnet.NewConn(&tailnet.Options{ |
467 | 433 | Addresses: []netip.Prefix{netip.PrefixFrom(tailnet.IP(), 128)}, |
468 | | - DERPMap: derpMap, |
| 434 | + DERPMap: api.DERPMap, |
469 | 435 | Logger: api.Logger.Named("tailnet"), |
470 | 436 | }) |
471 | 437 | if err != nil { |
472 | 438 | _ = clientConn.Close() |
473 | 439 | _ = serverConn.Close() |
474 | 440 | return nil, xerrors.Errorf("create tailnet conn: %w", err) |
475 | 441 | } |
| 442 | + conn.SetDERPRegionDialer(func(_ context.Context, region *tailcfg.DERPRegion) net.Conn { |
| 443 | + if !region.EmbeddedRelay { |
| 444 | + return nil |
| 445 | + } |
| 446 | + left, right := net.Pipe() |
| 447 | + go func() { |
| 448 | + defer left.Close() |
| 449 | + defer right.Close() |
| 450 | + brw := bufio.NewReadWriter(bufio.NewReader(right), bufio.NewWriter(right)) |
| 451 | + api.DERPServer.Accept(ctx, right, brw, r.RemoteAddr) |
| 452 | + }() |
| 453 | + return left |
| 454 | + }) |
476 | 455 |
|
477 | 456 | sendNodes, _ := tailnet.ServeCoordinator(clientConn, func(node []*tailnet.Node) error { |
478 | 457 | err = conn.UpdateNodes(node, true) |
|
0 commit comments