You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
345 lines
9.9 KiB
Go
345 lines
9.9 KiB
Go
package console
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
const (
|
|
BrowserProtocolPrefix = "cloud-ticket."
|
|
defaultMaximumFrame = 8 << 20
|
|
consoleHeartbeatInterval = 25 * time.Second
|
|
consoleHeartbeatTimeout = 10 * time.Second
|
|
)
|
|
|
|
var opaqueTokenPattern = regexp.MustCompile(`^[A-Za-z0-9_-]{32,256}$`)
|
|
var agentIDPattern = regexp.MustCompile(`^[1-9][0-9]{0,18}$`)
|
|
|
|
type ReverseConsoleConnector interface {
|
|
OpenConsole(context.Context, string, string, string) (*websocket.Conn, error)
|
|
}
|
|
|
|
type Relay struct {
|
|
introspector SessionIntrospector
|
|
connector ReverseConsoleConnector
|
|
recheckEvery time.Duration
|
|
heartbeatEvery time.Duration
|
|
heartbeatTimeout time.Duration
|
|
maxFrame int64
|
|
now func() time.Time
|
|
lifecycleMu sync.Mutex
|
|
draining bool
|
|
active sync.WaitGroup
|
|
next uint64
|
|
cancels map[uint64]context.CancelFunc
|
|
}
|
|
|
|
func NewRelay(introspector SessionIntrospector, connector ReverseConsoleConnector) (*Relay, error) {
|
|
if introspector == nil || connector == nil {
|
|
return nil, errors.New("invalid console relay settings")
|
|
}
|
|
return &Relay{
|
|
introspector: introspector,
|
|
connector: connector,
|
|
recheckEvery: 15 * time.Second,
|
|
heartbeatEvery: consoleHeartbeatInterval,
|
|
heartbeatTimeout: consoleHeartbeatTimeout,
|
|
maxFrame: defaultMaximumFrame,
|
|
now: time.Now,
|
|
cancels: make(map[uint64]context.CancelFunc),
|
|
}, nil
|
|
}
|
|
|
|
func (r *Relay) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
|
if request.Method != http.MethodGet || request.URL.Path != "/engine-console" || request.URL.RawQuery != "" {
|
|
http.Error(response, "not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if !validSameOrigin(request) {
|
|
http.Error(response, "origin rejected", http.StatusForbidden)
|
|
return
|
|
}
|
|
if !validWebSocketUpgrade(request) {
|
|
http.Error(response, "websocket upgrade required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
browserProtocol, browserTicket, ok := requestedProtocol(request.Header.Values("Sec-WebSocket-Protocol"), BrowserProtocolPrefix)
|
|
if !ok {
|
|
http.Error(response, "console ticket required", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
relayContext, finish, ok := r.begin(request.Context())
|
|
if !ok {
|
|
http.Error(response, "console gateway is draining", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
defer finish()
|
|
request = request.WithContext(relayContext)
|
|
initial, err := r.introspector.Inspect(request.Context(), IntrospectionRequest{Ticket: browserTicket, Consume: true})
|
|
if err != nil || validateInitialResult(initial, r.now().UTC()) != nil {
|
|
http.Error(response, "console authorization failed", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
agent, err := r.connector.OpenConsole(request.Context(), initial.AgentID,
|
|
initial.AgentTicket, initial.ConnectionID)
|
|
if err != nil || agent == nil {
|
|
if agent != nil {
|
|
_ = agent.CloseNow()
|
|
}
|
|
http.Error(response, "console host unavailable", http.StatusBadGateway)
|
|
return
|
|
}
|
|
agent.SetReadLimit(r.maxFrame)
|
|
browser, err := websocket.Accept(response, request, &websocket.AcceptOptions{
|
|
Subprotocols: []string{browserProtocol}, CompressionMode: websocket.CompressionDisabled,
|
|
})
|
|
if err != nil {
|
|
_ = agent.CloseNow()
|
|
return
|
|
}
|
|
browser.SetReadLimit(r.maxFrame)
|
|
r.relay(request.Context(), browser, agent, browserTicket, initial.ConnectionID)
|
|
}
|
|
|
|
func (r *Relay) begin(parent context.Context) (context.Context, func(), bool) {
|
|
r.lifecycleMu.Lock()
|
|
if r.draining {
|
|
r.lifecycleMu.Unlock()
|
|
return nil, nil, false
|
|
}
|
|
ctx, cancel := context.WithCancel(parent)
|
|
r.next++
|
|
id := r.next
|
|
r.cancels[id] = cancel
|
|
r.active.Add(1)
|
|
r.lifecycleMu.Unlock()
|
|
var once sync.Once
|
|
finish := func() {
|
|
once.Do(func() {
|
|
r.lifecycleMu.Lock()
|
|
delete(r.cancels, id)
|
|
r.lifecycleMu.Unlock()
|
|
cancel()
|
|
r.active.Done()
|
|
})
|
|
}
|
|
return ctx, finish, true
|
|
}
|
|
|
|
// BeginDrain rejects new browser console sessions while allowing already
|
|
// accepted relays to finish within the process shutdown grace period.
|
|
func (r *Relay) BeginDrain() {
|
|
r.lifecycleMu.Lock()
|
|
r.draining = true
|
|
r.lifecycleMu.Unlock()
|
|
}
|
|
|
|
// Wait blocks until every accepted console handler has returned. BeginDrain
|
|
// must be called before Wait so no new WaitGroup additions can race the wait.
|
|
func (r *Relay) Wait(ctx context.Context) error {
|
|
done := make(chan struct{})
|
|
go func() {
|
|
r.active.Wait()
|
|
close(done)
|
|
}()
|
|
select {
|
|
case <-done:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
// Close cancels remaining hijacked WebSocket relays after the graceful drain
|
|
// deadline. Each relay owns and closes its browser and Agent sockets.
|
|
func (r *Relay) Close() {
|
|
r.BeginDrain()
|
|
r.lifecycleMu.Lock()
|
|
cancels := make([]context.CancelFunc, 0, len(r.cancels))
|
|
for _, cancel := range r.cancels {
|
|
cancels = append(cancels, cancel)
|
|
}
|
|
r.lifecycleMu.Unlock()
|
|
for _, cancel := range cancels {
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
func validateInitialResult(result IntrospectionResult, now time.Time) error {
|
|
if !result.Active || !opaqueTokenPattern.MatchString(result.ConnectionID) ||
|
|
!agentIDPattern.MatchString(result.AgentID) ||
|
|
!opaqueTokenPattern.MatchString(result.AgentTicket) {
|
|
return errors.New("inactive or malformed console session")
|
|
}
|
|
expiresAt, err := time.Parse(time.RFC3339, result.AgentExpiresAt)
|
|
if err != nil || !expiresAt.After(now) || expiresAt.After(now.Add(2*time.Minute)) {
|
|
return errors.New("agent console ticket expiry is invalid")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Relay) relay(parent context.Context, browser, agent *websocket.Conn, ticket, connectionID string) {
|
|
ctx, cancel := context.WithCancel(parent)
|
|
errorsChannel := make(chan error, 5)
|
|
go func() { errorsChannel <- copyBinary(ctx, browser, agent) }()
|
|
go func() { errorsChannel <- copyBinary(ctx, agent, browser) }()
|
|
go func() { errorsChannel <- r.revalidate(ctx, ticket, connectionID) }()
|
|
go func() {
|
|
errorsChannel <- maintainConsoleHeartbeat(ctx, browser, r.heartbeatEvery, r.heartbeatTimeout)
|
|
}()
|
|
go func() {
|
|
errorsChannel <- maintainConsoleHeartbeat(ctx, agent, r.heartbeatEvery, r.heartbeatTimeout)
|
|
}()
|
|
err := <-errorsChannel
|
|
cancel()
|
|
_ = err
|
|
_ = browser.CloseNow()
|
|
_ = agent.CloseNow()
|
|
select {
|
|
case <-errorsChannel:
|
|
case <-time.After(time.Second):
|
|
}
|
|
}
|
|
|
|
type consolePinger interface {
|
|
Ping(context.Context) error
|
|
}
|
|
|
|
// maintainConsoleHeartbeat keeps an otherwise static console WebSocket alive
|
|
// through reverse proxies and detects a half-open peer. Ping and Pong are
|
|
// WebSocket control frames, so they never enter the binary-only VNC stream.
|
|
// Ping is intentionally run alongside copyBinary: coder/websocket requires an
|
|
// active Reader to process the peer's Pong and supports concurrent methods.
|
|
func maintainConsoleHeartbeat(
|
|
ctx context.Context,
|
|
connection consolePinger,
|
|
interval time.Duration,
|
|
timeout time.Duration,
|
|
) error {
|
|
if interval <= 0 || timeout <= 0 {
|
|
return errors.New("console heartbeat timing is invalid")
|
|
}
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-ticker.C:
|
|
pingCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
err := connection.Ping(pingCtx)
|
|
cancel()
|
|
if err != nil {
|
|
return errors.Join(errors.New("console heartbeat failed"), err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var errSessionRevoked = errors.New("console session revoked")
|
|
|
|
func (r *Relay) revalidate(ctx context.Context, ticket, connectionID string) error {
|
|
ticker := time.NewTicker(r.recheckEvery)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-ticker.C:
|
|
result, err := r.introspector.Inspect(ctx, IntrospectionRequest{Ticket: ticket, ConnectionID: connectionID, Consume: false})
|
|
if err != nil || !result.Active {
|
|
return errSessionRevoked
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func copyBinary(ctx context.Context, source, destination *websocket.Conn) error {
|
|
for {
|
|
messageType, reader, err := source.Reader(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if messageType != websocket.MessageBinary {
|
|
return errors.New("text console frames are forbidden")
|
|
}
|
|
writer, err := destination.Writer(ctx, websocket.MessageBinary)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, copyErr := io.Copy(writer, reader)
|
|
closeErr := writer.Close()
|
|
if copyErr != nil {
|
|
return copyErr
|
|
}
|
|
if closeErr != nil {
|
|
return closeErr
|
|
}
|
|
}
|
|
}
|
|
|
|
func requestedProtocol(values []string, prefix string) (string, string, bool) {
|
|
var protocols []string
|
|
for _, value := range values {
|
|
for _, part := range strings.Split(value, ",") {
|
|
if trimmed := strings.TrimSpace(part); trimmed != "" {
|
|
protocols = append(protocols, trimmed)
|
|
}
|
|
}
|
|
}
|
|
if len(protocols) != 1 || !strings.HasPrefix(protocols[0], prefix) {
|
|
return "", "", false
|
|
}
|
|
ticket := strings.TrimPrefix(protocols[0], prefix)
|
|
if !opaqueTokenPattern.MatchString(ticket) {
|
|
return "", "", false
|
|
}
|
|
return protocols[0], ticket, true
|
|
}
|
|
|
|
func validSameOrigin(request *http.Request) bool {
|
|
origins := request.Header.Values("Origin")
|
|
if len(origins) != 1 || strings.Contains(origins[0], ",") {
|
|
return false
|
|
}
|
|
origin, err := url.Parse(origins[0])
|
|
if err != nil || origin.User != nil || origin.RawQuery != "" || origin.Fragment != "" ||
|
|
(origin.Path != "" && origin.Path != "/") || origin.Host == "" {
|
|
return false
|
|
}
|
|
forwarded := request.Header.Values("X-Forwarded-Proto")
|
|
if len(forwarded) != 1 || (forwarded[0] != "https" && forwarded[0] != "http") || origin.Scheme != forwarded[0] {
|
|
return false
|
|
}
|
|
return strings.EqualFold(origin.Host, request.Host)
|
|
}
|
|
|
|
func validWebSocketUpgrade(request *http.Request) bool {
|
|
if !headerHasToken(request.Header.Values("Connection"), "upgrade") ||
|
|
!headerHasToken(request.Header.Values("Upgrade"), "websocket") ||
|
|
request.Header.Get("Sec-WebSocket-Version") != "13" || request.Header.Get("Sec-WebSocket-Key") == "" {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func headerHasToken(values []string, expected string) bool {
|
|
for _, value := range values {
|
|
for _, token := range strings.Split(value, ",") {
|
|
if strings.EqualFold(strings.TrimSpace(token), expected) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|