2021-10-21 17:12:51 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
//go:build linux || js
|
|
|
|
|
|
|
|
package derphttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"nhooyr.io/websocket"
|
2022-10-18 21:20:43 +00:00
|
|
|
"tailscale.com/net/wsconn"
|
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)
|
2022-10-18 21:20:43 +00:00
|
|
|
netConn := wsconn.NetConn(context.Background(), c, websocket.MessageBinary)
|
2022-06-06 23:13:15 +00:00
|
|
|
return netConn, nil
|
2021-10-21 17:12:51 +00:00
|
|
|
}
|