prompt_outbound_security.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package securityaudit
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "time"
  9. infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
  10. )
  11. const maxGuardResponseBytes int64 = 256 * 1024
  12. func NormalizeBaseURL(raw string) (string, error) {
  13. raw = strings.TrimSpace(raw)
  14. parsed, err := url.Parse(raw)
  15. if err != nil || parsed.Scheme == "" || parsed.Host == "" {
  16. return "", infraerrors.BadRequest("prompt_audit_invalid_base_url", "审计节点地址无效")
  17. }
  18. parsed.Scheme = strings.ToLower(parsed.Scheme)
  19. if parsed.Scheme != "http" && parsed.Scheme != "https" {
  20. return "", infraerrors.BadRequest("prompt_audit_invalid_base_url_scheme", "审计节点仅支持 HTTP(S)")
  21. }
  22. if parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" {
  23. return "", infraerrors.BadRequest("prompt_audit_unsafe_base_url", "审计节点地址不能包含凭据、查询参数或片段")
  24. }
  25. host := strings.TrimSpace(parsed.Hostname())
  26. if host == "" {
  27. return "", infraerrors.BadRequest("prompt_audit_invalid_base_url", "审计节点地址无效")
  28. }
  29. path := strings.TrimRight(parsed.EscapedPath(), "/")
  30. if strings.EqualFold(path, "/v1") {
  31. path = ""
  32. }
  33. parsed.Path = path
  34. parsed.RawPath = ""
  35. return strings.TrimRight(parsed.String(), "/"), nil
  36. }
  37. func ChatCompletionsURL(base string) (string, error) {
  38. normalized, err := NormalizeBaseURL(base)
  39. if err != nil {
  40. return "", err
  41. }
  42. return normalized + "/v1/chat/completions", nil
  43. }
  44. func ModelsURL(base string) (string, error) {
  45. normalized, err := NormalizeBaseURL(base)
  46. if err != nil {
  47. return "", err
  48. }
  49. return normalized + "/v1/models", nil
  50. }
  51. func NewSecureHTTPClient(endpoint ActiveEndpoint) (*http.Client, error) {
  52. _, err := NormalizeBaseURL(endpoint.BaseURL)
  53. if err != nil {
  54. return nil, err
  55. }
  56. dialer := &net.Dialer{Timeout: 3 * time.Second, KeepAlive: 30 * time.Second}
  57. transport := &http.Transport{
  58. // Do not inherit HTTP(S)_PROXY. A proxy would move the actual destination
  59. // dial outside secureDialContext and bypass this module's DNS/IP validation.
  60. Proxy: nil,
  61. ForceAttemptHTTP2: true,
  62. MaxIdleConns: 64,
  63. MaxIdleConnsPerHost: 16,
  64. IdleConnTimeout: 90 * time.Second,
  65. TLSHandshakeTimeout: 5 * time.Second,
  66. ResponseHeaderTimeout: time.Duration(endpoint.TimeoutMS) * time.Millisecond,
  67. ExpectContinueTimeout: time.Second,
  68. TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12},
  69. }
  70. // Endpoint ownership and destination trust are administrator concerns.
  71. // Use the standard dialer so configured private, loopback, reserved, and
  72. // DNS-resolved addresses are all reachable from the service environment.
  73. transport.DialContext = dialer.DialContext
  74. timeout := time.Duration(endpoint.TimeoutMS) * time.Millisecond
  75. if timeout <= 0 {
  76. timeout = DefaultTimeoutMS * time.Millisecond
  77. }
  78. return &http.Client{
  79. Transport: transport,
  80. Timeout: timeout,
  81. }, nil
  82. }