2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-10-21 17:12:51 +00:00
|
|
|
|
2024-11-08 00:49:47 +00:00
|
|
|
//go:build js || ((linux || darwin) && ts_debug_websockets)
|
2021-10-21 17:12:51 +00:00
|
|
|
|
|
|
|
package derphttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
2024-08-09 19:32:24 +00:00
|
|
|
"github.com/coder/websocket"
|
2022-10-18 21:20:43 +00:00
|
|
|
"tailscale.com/net/wsconn"
|
2021-10-21 17:12:51 +00:00
|
|
|
)
|
|
|
|
|
2024-11-08 00:49:47 +00:00
|
|
|
const canWebsockets = true
|
|
|
|
|
2021-10-21 17:12:51 +00:00
|
|
|
func init() {
|
|
|
|
dialWebsocketFunc = dialWebsocket
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialWebsocket(ctx context.Context, urlStr string) (net.Conn, error) {
|
|
|
|
c, res, err := websocket.Dial(ctx, urlStr, &websocket.DialOptions{
|
|
|
|
Subprotocols: []string{"derp"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("websocket Dial: %v, %+v", err, res)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Printf("websocket: connected to %v", urlStr)
|
2023-08-29 23:27:30 +00:00
|
|
|
netConn := wsconn.NetConn(context.Background(), c, websocket.MessageBinary, urlStr)
|
2022-06-06 23:13:15 +00:00
|
|
|
return netConn, nil
|
2021-10-21 17:12:51 +00:00
|
|
|
}
|