2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-04-07 19:03:32 +00:00
|
|
|
|
|
|
|
package controlclient
|
|
|
|
|
|
|
|
import (
|
2022-10-30 04:30:03 +00:00
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-04-07 19:03:32 +00:00
|
|
|
"math"
|
2022-10-30 04:30:03 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-04-07 19:03:32 +00:00
|
|
|
"testing"
|
2022-10-30 15:42:54 +00:00
|
|
|
"time"
|
2022-04-07 19:03:32 +00:00
|
|
|
|
2022-10-30 04:30:03 +00:00
|
|
|
"golang.org/x/net/http2"
|
2024-11-07 23:59:19 +00:00
|
|
|
"tailscale.com/control/controlhttp/controlhttpserver"
|
2024-06-19 22:30:55 +00:00
|
|
|
"tailscale.com/internal/noiseconn"
|
2024-04-27 05:06:20 +00:00
|
|
|
"tailscale.com/net/netmon"
|
2022-10-30 04:30:03 +00:00
|
|
|
"tailscale.com/net/tsdial"
|
2022-04-07 19:03:32 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-10-30 04:30:03 +00:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/logger"
|
2022-04-07 19:03:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// maxAllowedNoiseVersion is the highest we expect the Tailscale
|
|
|
|
// capability version to ever get. It's a value close to 2^16, but
|
|
|
|
// with enough leeway that we get a very early warning that it's time
|
|
|
|
// to rework the wire protocol to allow larger versions, while still
|
|
|
|
// giving us headroom to bump this test and fix the build.
|
|
|
|
//
|
|
|
|
// Code elsewhere in the client will panic() if the tailcfg capability
|
|
|
|
// version exceeds 16 bits, so take a failure of this test seriously.
|
|
|
|
const maxAllowedNoiseVersion = math.MaxUint16 - 5000
|
|
|
|
|
|
|
|
func TestNoiseVersion(t *testing.T) {
|
|
|
|
if tailcfg.CurrentCapabilityVersion > maxAllowedNoiseVersion {
|
|
|
|
t.Fatalf("tailcfg.CurrentCapabilityVersion is %d, want <=%d", tailcfg.CurrentCapabilityVersion, maxAllowedNoiseVersion)
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 04:30:03 +00:00
|
|
|
|
2022-10-30 15:42:54 +00:00
|
|
|
type noiseClientTest struct {
|
|
|
|
sendEarlyPayload bool
|
|
|
|
}
|
|
|
|
|
2022-10-30 04:30:03 +00:00
|
|
|
func TestNoiseClientHTTP2Upgrade(t *testing.T) {
|
2022-10-30 15:42:54 +00:00
|
|
|
noiseClientTest{}.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoiseClientHTTP2Upgrade_earlyPayload(t *testing.T) {
|
|
|
|
noiseClientTest{
|
|
|
|
sendEarlyPayload: true,
|
|
|
|
}.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt noiseClientTest) run(t *testing.T) {
|
2022-10-30 04:30:03 +00:00
|
|
|
serverPrivate := key.NewMachine()
|
|
|
|
clientPrivate := key.NewMachine()
|
2022-10-30 15:42:54 +00:00
|
|
|
chalPrivate := key.NewChallenge()
|
2022-10-30 04:30:03 +00:00
|
|
|
|
|
|
|
const msg = "Hello, client"
|
|
|
|
h2 := &http2.Server{}
|
|
|
|
hs := httptest.NewServer(&Upgrader{
|
2022-10-30 15:42:54 +00:00
|
|
|
h2srv: h2,
|
|
|
|
noiseKeyPriv: serverPrivate,
|
|
|
|
sendEarlyPayload: tt.sendEarlyPayload,
|
|
|
|
challenge: chalPrivate,
|
2022-10-30 04:30:03 +00:00
|
|
|
httpBaseConfig: &http.Server{
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
io.WriteString(w, msg)
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
defer hs.Close()
|
|
|
|
|
2024-04-27 05:06:20 +00:00
|
|
|
dialer := tsdial.NewDialer(netmon.NewStatic())
|
2023-04-30 03:25:37 +00:00
|
|
|
nc, err := NewNoiseClient(NoiseOpts{
|
|
|
|
PrivKey: clientPrivate,
|
|
|
|
ServerPubKey: serverPrivate.Public(),
|
|
|
|
ServerURL: hs.URL,
|
|
|
|
Dialer: dialer,
|
|
|
|
})
|
2022-10-30 04:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-10-30 15:42:54 +00:00
|
|
|
|
|
|
|
// Get a conn and verify it read its early payload before the http/2
|
|
|
|
// handshake.
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
c, err := nc.getConn(ctx)
|
2022-10-30 04:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-06-19 22:30:55 +00:00
|
|
|
payload, err := c.GetEarlyPayload(ctx)
|
|
|
|
if err != nil {
|
2022-10-30 15:42:54 +00:00
|
|
|
t.Fatal("timed out waiting for didReadHeaderCh")
|
|
|
|
}
|
|
|
|
|
2024-06-19 22:30:55 +00:00
|
|
|
gotNonNil := payload != nil
|
|
|
|
if gotNonNil != tt.sendEarlyPayload {
|
|
|
|
t.Errorf("sendEarlyPayload = %v but got earlyPayload = %T", tt.sendEarlyPayload, payload)
|
|
|
|
}
|
|
|
|
if payload != nil {
|
|
|
|
if payload.NodeKeyChallenge != chalPrivate.Public() {
|
|
|
|
t.Errorf("earlyPayload.NodeKeyChallenge = %v; want %v", payload.NodeKeyChallenge, chalPrivate.Public())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-30 15:42:54 +00:00
|
|
|
checkRes := func(t *testing.T, res *http.Response) {
|
|
|
|
t.Helper()
|
|
|
|
defer res.Body.Close()
|
|
|
|
all, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(all) != msg {
|
|
|
|
t.Errorf("got response %q; want %q", all, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And verify we can do HTTP/2 against that conn.
|
|
|
|
res, err := (&http.Client{Transport: c}).Get("https://unused.example/")
|
2022-10-30 04:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-10-30 15:42:54 +00:00
|
|
|
checkRes(t, res)
|
2022-10-30 04:30:03 +00:00
|
|
|
|
2022-10-30 15:42:54 +00:00
|
|
|
// And try using the high-level nc.post API as well.
|
2024-03-20 13:41:56 +00:00
|
|
|
res, err = nc.post(context.Background(), "/", key.NodePublic{}, nil)
|
2022-10-30 15:42:54 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
checkRes(t, res)
|
2022-10-30 04:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upgrader is an http.Handler that hijacks and upgrades POST-with-Upgrade
|
|
|
|
// request to a Tailscale 2021 connection, then hands the resulting
|
|
|
|
// controlbase.Conn off to h2srv.
|
|
|
|
type Upgrader struct {
|
|
|
|
// h2srv is that will handle requests after the
|
|
|
|
// connection has been upgraded to HTTP/2-over-noise.
|
|
|
|
h2srv *http2.Server
|
|
|
|
|
|
|
|
// httpBaseConfig is the http1 server config that h2srv is
|
|
|
|
// associated with.
|
|
|
|
httpBaseConfig *http.Server
|
|
|
|
|
|
|
|
logf logger.Logf
|
|
|
|
|
|
|
|
noiseKeyPriv key.MachinePrivate
|
2022-10-30 15:42:54 +00:00
|
|
|
challenge key.ChallengePrivate
|
2022-10-30 04:30:03 +00:00
|
|
|
|
|
|
|
sendEarlyPayload bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (up *Upgrader) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if up == nil || up.h2srv == nil {
|
|
|
|
http.Error(w, "invalid server config", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r.URL.Path != "/ts2021" {
|
|
|
|
http.Error(w, "ts2021 upgrader installed at wrong path", http.StatusBadGateway)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if up.noiseKeyPriv.IsZero() {
|
|
|
|
http.Error(w, "keys not available", http.StatusServiceUnavailable)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
earlyWriteFn := func(protocolVersion int, w io.Writer) error {
|
|
|
|
if !up.sendEarlyPayload {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-30 15:42:54 +00:00
|
|
|
earlyJSON, err := json.Marshal(&tailcfg.EarlyNoise{
|
|
|
|
NodeKeyChallenge: up.challenge.Public(),
|
|
|
|
})
|
2022-10-30 04:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// 5 bytes that won't be mistaken for an HTTP/2 frame:
|
|
|
|
// https://httpwg.org/specs/rfc7540.html#rfc.section.4.1 (Especially not
|
|
|
|
// an HTTP/2 settings frame, which isn't of type 'T')
|
2022-10-30 15:42:54 +00:00
|
|
|
var notH2Frame [5]byte
|
2024-06-19 22:30:55 +00:00
|
|
|
copy(notH2Frame[:], noiseconn.EarlyPayloadMagic)
|
2022-10-30 04:30:03 +00:00
|
|
|
var lenBuf [4]byte
|
|
|
|
binary.BigEndian.PutUint32(lenBuf[:], uint32(len(earlyJSON)))
|
|
|
|
// These writes are all buffered by caller, so fine to do them
|
|
|
|
// separately:
|
|
|
|
if _, err := w.Write(notH2Frame[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.Write(lenBuf[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.Write(earlyJSON[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-07 23:59:19 +00:00
|
|
|
cbConn, err := controlhttpserver.AcceptHTTP(r.Context(), w, r, up.noiseKeyPriv, earlyWriteFn)
|
2022-10-30 04:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
up.logf("controlhttp: Accept: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cbConn.Close()
|
|
|
|
|
|
|
|
up.h2srv.ServeConn(cbConn, &http2.ServeConnOpts{
|
|
|
|
BaseConfig: up.httpBaseConfig,
|
|
|
|
})
|
|
|
|
}
|