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.
109 lines
3.7 KiB
Go
109 lines
3.7 KiB
Go
package console
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/gateway/internal/protocol"
|
|
)
|
|
|
|
const maxIntrospectionResponse = 64 << 10
|
|
|
|
type IntrospectionRequest struct {
|
|
Ticket string `json:"ticket"`
|
|
ConnectionID string `json:"connection_id,omitempty"`
|
|
Consume bool `json:"consume"`
|
|
}
|
|
|
|
type IntrospectionResult struct {
|
|
Active bool `json:"active"`
|
|
ConnectionID string `json:"connection_id,omitempty"`
|
|
AgentID string `json:"agent_id,omitempty"`
|
|
AgentTicket string `json:"agent_ticket,omitempty"`
|
|
AgentExpiresAt string `json:"agent_expires_at,omitempty"`
|
|
}
|
|
|
|
type SessionIntrospector interface {
|
|
Inspect(context.Context, IntrospectionRequest) (IntrospectionResult, error)
|
|
}
|
|
|
|
type HTTPIntrospector struct {
|
|
endpoint string
|
|
secret []byte
|
|
client *http.Client
|
|
now func() time.Time
|
|
}
|
|
|
|
func NewHTTPIntrospector(endpoint string, secret []byte, timeout time.Duration) (*HTTPIntrospector, error) {
|
|
if err := validateIntrospectionURL(endpoint); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(secret) < 32 || timeout <= 0 || timeout > 30*time.Second {
|
|
return nil, errors.New("invalid introspection client settings")
|
|
}
|
|
dialer := &net.Dialer{Timeout: timeout, KeepAlive: 30 * time.Second}
|
|
transport := &http.Transport{
|
|
Proxy: nil, DialContext: dialer.DialContext, ForceAttemptHTTP2: false,
|
|
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
|
|
TLSHandshakeTimeout: timeout,
|
|
}
|
|
return &HTTPIntrospector{endpoint: endpoint, secret: append([]byte(nil), secret...), now: time.Now,
|
|
client: &http.Client{Transport: transport, Timeout: timeout, CheckRedirect: rejectRedirect}}, nil
|
|
}
|
|
|
|
func (i *HTTPIntrospector) Inspect(ctx context.Context, input IntrospectionRequest) (IntrospectionResult, error) {
|
|
body, err := json.Marshal(input)
|
|
if err != nil {
|
|
return IntrospectionResult{}, errors.New("encode introspection request")
|
|
}
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodPost, i.endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return IntrospectionResult{}, errors.New("create introspection request")
|
|
}
|
|
request.Header.Set("Accept", "application/json")
|
|
request.Header.Set("Content-Type", "application/json")
|
|
nonce, err := protocol.RandomNonce()
|
|
if err != nil {
|
|
return IntrospectionResult{}, errors.New("create introspection request nonce")
|
|
}
|
|
if err := protocol.Sign(request, body, protocol.Actor{Service: "agent-gateway"}, i.secret, i.now(), nonce); err != nil {
|
|
return IntrospectionResult{}, errors.New("sign introspection request")
|
|
}
|
|
response, err := i.client.Do(request)
|
|
if err != nil {
|
|
return IntrospectionResult{}, errors.New("panel introspection unavailable")
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusOK {
|
|
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, maxIntrospectionResponse))
|
|
return IntrospectionResult{}, errors.New("panel introspection rejected")
|
|
}
|
|
reader := io.LimitReader(response.Body, maxIntrospectionResponse+1)
|
|
raw, err := io.ReadAll(reader)
|
|
if err != nil || len(raw) > maxIntrospectionResponse {
|
|
return IntrospectionResult{}, errors.New("panel introspection response is invalid")
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(raw))
|
|
decoder.DisallowUnknownFields()
|
|
var result IntrospectionResult
|
|
if err := decoder.Decode(&result); err != nil {
|
|
return IntrospectionResult{}, errors.New("panel introspection response is invalid")
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
|
return IntrospectionResult{}, errors.New("panel introspection response is invalid")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func rejectRedirect(_ *http.Request, _ []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
}
|