// Package proxy relays console TCP traffic between guacd and a managed server's
// agent. The agent dials only its own loopback address; the port is the single
// value it takes from the control plane.
package proxy

import (
	"crypto/rand"
	"encoding/hex"
	"errors"
)

var (
	ErrNotFound  = errors.New("proxy session not found")
	ErrForbidden = errors.New("proxy session belongs to another server")
)

// NewID returns a 32-byte random identifier as hex. It is the only credential
// tying an incoming ProxyStream to a pending console session.
func NewID() (string, error) {
	b := make([]byte, 32)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	return hex.EncodeToString(b), nil
}

// There is deliberately no in-process registry of pending sessions here any
// more. One existed, keyed by proxy_id, on the assumption that the pod which
// bound a listener was the pod that would receive the matching ProxyStream.
// That assumption holds only for a single replica: a ProxyStream is its own
// HTTP/2 request and an L7 proxy routes it independently of the agent's
// command stream, so with N replicas the lookup missed (N-1)/N of the time and
// the console failed with "proxy session not found".
//
// The pending record lives in Redis instead (bus.SetPendingProxy /
// ClaimPendingProxy), and the listener is bound by whichever pod the stream
// actually reaches — see services.ClaimProxyStream.
