-
Notifications
You must be signed in to change notification settings - Fork 936
feat: automatically use websockets if DERP upgrade is unavailable #6381
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package tailnet | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
|
||
"nhooyr.io/websocket" | ||
"tailscale.com/derp" | ||
"tailscale.com/net/wsconn" | ||
) | ||
|
||
// WithWebsocketSupport returns an http.Handler that upgrades | ||
// connections to the "derp" subprotocol to WebSockets and | ||
// passes them to the DERP server. | ||
// Taken from: https://github.com/tailscale/tailscale/blob/e3211ff88ba85435f70984cf67d9b353f3d650d8/cmd/derper/websocket.go#L21 | ||
func WithWebsocketSupport(s *derp.Server, base http.Handler) (http.Handler, func()) { | ||
var mu sync.Mutex | ||
var waitGroup sync.WaitGroup | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
up := strings.ToLower(r.Header.Get("Upgrade")) | ||
|
||
// Very early versions of Tailscale set "Upgrade: WebSocket" but didn't actually | ||
// speak WebSockets (they still assumed DERP's binary framing). So to distinguish | ||
// clients that actually want WebSockets, look for an explicit "derp" subprotocol. | ||
if up != "websocket" || !strings.Contains(r.Header.Get("Sec-Websocket-Protocol"), "derp") { | ||
base.ServeHTTP(w, r) | ||
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. Should we log here to better understand why it may not be working? (E.g. future bugs.) 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. I don't think so because this could be spammy. |
||
return | ||
} | ||
|
||
mu.Lock() | ||
kylecarbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ctx.Err() != nil { | ||
mu.Unlock() | ||
return | ||
} | ||
waitGroup.Add(1) | ||
mu.Unlock() | ||
defer waitGroup.Done() | ||
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{ | ||
Subprotocols: []string{"derp"}, | ||
OriginPatterns: []string{"*"}, | ||
// Disable compression because we transmit WireGuard messages that | ||
// are not compressible. | ||
// Additionally, Safari has a broken implementation of compression | ||
// (see https://github.com/nhooyr/websocket/issues/218) that makes | ||
// enabling it actively harmful. | ||
CompressionMode: websocket.CompressionDisabled, | ||
}) | ||
if err != nil { | ||
log.Printf("websocket.Accept: %v", err) | ||
return | ||
} | ||
defer c.Close(websocket.StatusInternalError, "closing") | ||
if c.Subprotocol() != "derp" { | ||
c.Close(websocket.StatusPolicyViolation, "client must speak the derp subprotocol") | ||
return | ||
} | ||
wc := wsconn.NetConn(ctx, c, websocket.MessageBinary) | ||
brw := bufio.NewReadWriter(bufio.NewReader(wc), bufio.NewWriter(wc)) | ||
s.Accept(ctx, wc, brw, r.RemoteAddr) | ||
}), func() { | ||
cancelFunc() | ||
mu.Lock() | ||
waitGroup.Wait() | ||
mu.Unlock() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.