(dialCtx context.Context, agentID uuid.UUID, options *DialAgentOptions)
| 203 | } |
| 204 | |
| 205 | func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *DialAgentOptions) (agentConn AgentConn, err error) { |
| 206 | if options == nil { |
| 207 | options = &DialAgentOptions{} |
| 208 | } |
| 209 | |
| 210 | connInfo, err := c.AgentConnectionInfo(dialCtx, agentID) |
| 211 | if err != nil { |
| 212 | return nil, xerrors.Errorf("get connection info: %w", err) |
| 213 | } |
| 214 | if connInfo.DisableDirectConnections { |
| 215 | options.BlockEndpoints = true |
| 216 | } |
| 217 | |
| 218 | wsOptions := &websocket.DialOptions{ |
| 219 | HTTPClient: c.client.HTTPClient, |
| 220 | // Need to disable compression to avoid a data-race. |
| 221 | CompressionMode: websocket.CompressionDisabled, |
| 222 | } |
| 223 | c.client.SessionTokenProvider.SetDialOption(wsOptions) |
| 224 | |
| 225 | // New context, separate from dialCtx. We don't want to cancel the |
| 226 | // connection if dialCtx is canceled. |
| 227 | ctx, cancel := context.WithCancel(context.Background()) |
| 228 | defer func() { |
| 229 | if err != nil { |
| 230 | cancel() |
| 231 | } |
| 232 | }() |
| 233 | |
| 234 | coordinateURL, err := c.client.URL.Parse(fmt.Sprintf("/api/v2/workspaceagents/%s/coordinate", agentID)) |
| 235 | if err != nil { |
| 236 | return nil, xerrors.Errorf("parse url: %w", err) |
| 237 | } |
| 238 | |
| 239 | dialer := NewWebsocketDialer(options.Logger, coordinateURL, wsOptions) |
| 240 | clk := quartz.NewReal() |
| 241 | controller := tailnet.NewController(options.Logger, dialer) |
| 242 | controller.ResumeTokenCtrl = tailnet.NewBasicResumeTokenController(options.Logger, clk) |
| 243 | |
| 244 | ip := tailnet.TailscaleServicePrefix.RandomAddr() |
| 245 | var header http.Header |
| 246 | if headerTransport, ok := c.client.HTTPClient.Transport.(*codersdk.HeaderTransport); ok { |
| 247 | header = headerTransport.Header |
| 248 | } |
| 249 | var telemetrySink tailnet.TelemetrySink |
| 250 | if options.EnableTelemetry { |
| 251 | basicTel := tailnet.NewBasicTelemetryController(options.Logger) |
| 252 | telemetrySink = basicTel |
| 253 | controller.TelemetryCtrl = basicTel |
| 254 | } |
| 255 | |
| 256 | c.RewriteDERPMap(connInfo.DERPMap) |
| 257 | conn, err := tailnet.NewConn(&tailnet.Options{ |
| 258 | Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)}, |
| 259 | DERPMap: connInfo.DERPMap, |
| 260 | DERPHeader: &header, |
| 261 | DERPTLSConfig: c.client.DERPTLSConfig(), |
| 262 | DERPForceWebSockets: connInfo.DERPForceWebSockets, |
nothing calls this directly
no test coverage detected