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.
127 lines
4.2 KiB
Go
127 lines
4.2 KiB
Go
package console
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
RequiredListenAddress = "127.0.0.1:8001"
|
|
RequiredIntrospectionPath = "/api/internal/cloud/session-introspection"
|
|
RequiredAgentAuthPath = "/api/internal/cloud/agent-auth"
|
|
RequiredAgentRequestTimeoutSeconds = 870
|
|
maxConfigBytes = 64 << 10
|
|
)
|
|
|
|
type Config struct {
|
|
ListenAddress string `json:"listen_address"`
|
|
PanelIntrospectionURL string `json:"panel_introspection_url"`
|
|
PanelAgentAuthURL string `json:"panel_agent_auth_url"`
|
|
PanelSharedSecret string `json:"panel_shared_secret"`
|
|
ConnectTimeoutSeconds int `json:"connect_timeout_seconds"`
|
|
AgentRequestTimeoutSeconds int `json:"agent_request_timeout_seconds"`
|
|
}
|
|
|
|
func LoadConfig(path string) (Config, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("open Agent gateway configuration: %w", err)
|
|
}
|
|
defer file.Close()
|
|
raw, err := io.ReadAll(io.LimitReader(file, maxConfigBytes+1))
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("read Agent gateway configuration: %w", err)
|
|
}
|
|
if len(raw) > maxConfigBytes {
|
|
return Config{}, errors.New("Agent gateway configuration exceeds 65536 bytes")
|
|
}
|
|
decoder := json.NewDecoder(bytes.NewReader(raw))
|
|
decoder.DisallowUnknownFields()
|
|
var config Config
|
|
if err := decoder.Decode(&config); err != nil {
|
|
return Config{}, fmt.Errorf("decode Agent gateway configuration: %w", err)
|
|
}
|
|
var extra any
|
|
if err := decoder.Decode(&extra); !errors.Is(err, io.EOF) {
|
|
return Config{}, errors.New("Agent gateway configuration contains trailing data")
|
|
}
|
|
if err := config.Validate(); err != nil {
|
|
return Config{}, err
|
|
}
|
|
return config, nil
|
|
}
|
|
|
|
func (c Config) Validate() error {
|
|
var problems []string
|
|
if c.ListenAddress != RequiredListenAddress {
|
|
problems = append(problems, "listen_address must be "+RequiredListenAddress)
|
|
}
|
|
if err := validateIntrospectionURL(c.PanelIntrospectionURL); err != nil {
|
|
problems = append(problems, err.Error())
|
|
}
|
|
if err := validatePanelURL(c.PanelAgentAuthURL, RequiredAgentAuthPath, "panel_agent_auth_url"); err != nil {
|
|
problems = append(problems, err.Error())
|
|
}
|
|
if len(c.PanelSharedSecret) < 32 || strings.ContainsAny(c.PanelSharedSecret, "\x00\r\n") {
|
|
problems = append(problems, "panel_shared_secret must contain at least 32 non-line-breaking bytes")
|
|
}
|
|
if c.ConnectTimeoutSeconds < 1 || c.ConnectTimeoutSeconds > 30 {
|
|
problems = append(problems, "connect_timeout_seconds must be between 1 and 30")
|
|
}
|
|
// Keep the production request envelope strictly between the Agent's 840s
|
|
// operation cap and PHP's 900s HTTP deadline.
|
|
if c.AgentRequestTimeoutSeconds != RequiredAgentRequestTimeoutSeconds {
|
|
problems = append(problems, "agent_request_timeout_seconds must be 870")
|
|
}
|
|
if len(problems) != 0 {
|
|
return errors.New(strings.Join(problems, "; "))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c Config) ConnectTimeout() time.Duration {
|
|
return time.Duration(c.ConnectTimeoutSeconds) * time.Second
|
|
}
|
|
|
|
func (c Config) AgentRequestTimeout() time.Duration {
|
|
return time.Duration(c.AgentRequestTimeoutSeconds) * time.Second
|
|
}
|
|
|
|
func validateIntrospectionURL(value string) error {
|
|
return validatePanelURL(value, RequiredIntrospectionPath, "panel_introspection_url")
|
|
}
|
|
|
|
func validatePanelURL(value, requiredPath, field string) error {
|
|
if value == "" || len(value) > 2048 || strings.ContainsAny(value, "\x00\r\n\\") {
|
|
return errors.New(field + " is invalid")
|
|
}
|
|
parsed, err := url.Parse(value)
|
|
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.User != nil ||
|
|
parsed.RawQuery != "" || parsed.ForceQuery || parsed.Fragment != "" || parsed.Path != requiredPath || parsed.RawPath != "" ||
|
|
strings.HasSuffix(parsed.Host, ":") {
|
|
return errors.New(field + " must be its fixed local endpoint")
|
|
}
|
|
host := parsed.Hostname()
|
|
ip := net.ParseIP(host)
|
|
if ip == nil || !ip.IsLoopback() {
|
|
return errors.New(field + " must use a literal loopback address")
|
|
}
|
|
port := parsed.Port()
|
|
if port != "" {
|
|
number, err := strconv.Atoi(port)
|
|
if err != nil || number < 1 || number > 65535 {
|
|
return errors.New(field + " has an invalid port")
|
|
}
|
|
}
|
|
return nil
|
|
}
|