Skip to content

Commit

Permalink
allow overriding tailscaled transport
Browse files Browse the repository at this point in the history
This allows callers an alternative to overriding the tailcaled dialer.

Updates tailscale/caddy-tailscale#66

Signed-off-by: Will Norris <will@tailscale.com>
  • Loading branch information
willnorris committed Jun 7, 2024
1 parent bbccfbf commit 34704db
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions tscert.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var (
// TailscaledDialer is the DialContext func that connects to the local machine's
// tailscaled or equivalent.
TailscaledDialer = DialLocalAPI

// TailscaledTransport is the RoundTripper that sends LocalAPI requests
// to the local machine's tailscaled or equivalent.
// If nil, a default RoundTripper is used that uses TailscaledDialer.
TailscaledTransport http.RoundTripper
)

// DialLocalAPI connects to the LocalAPI server of the tailscaled instance on the machine.
Expand Down Expand Up @@ -79,11 +84,12 @@ var (
// DoLocalRequest may mutate the request to add Authorization headers.
func DoLocalRequest(req *http.Request) (*http.Response, error) {
tsClientOnce.Do(func() {
tsClient = &http.Client{
Transport: &http.Transport{
if TailscaledTransport == nil {
TailscaledTransport = &http.Transport{
DialContext: TailscaledDialer,
},
}
}
tsClient = &http.Client{Transport: TailscaledTransport}
})
if _, token, err := safesocket.LocalTCPPortAndToken(); err == nil {
req.SetBasicAuth("", token)
Expand Down

0 comments on commit 34704db

Please sign in to comment.