Files
opencode2api/internal/config/secrets.go
T
Jason Xu 260211cd9e feat(proxy): support rotating proxy pools via #pool marker
An endpoint that already rotates exit IPs (Resin and similar gateways) was
handled like a static proxy, which defeated the rotation in three ways:

- the cloned transport kept HTTP/2 and keep-alive enabled, so every request
  reused one CONNECT tunnel and therefore one exit IP, drawing 429s;
- a single slow exit marked the whole entry unhealthy and keys were rebound,
  with scheduled recovery only every 15 minutes;
- any 429/5xx put the anonymous node into a 15s-120s cooldown, and that lane
  has no earliest-expiry fallback, so the request failed with
  "no healthy anonymous proxies available" (HTTP 502).

Mark an entry with a "#pool" fragment (or a "pool+" scheme prefix) to declare
it a rotating pool. Pool entries then open a fresh connection per attempt
(DisableKeepAlives, no HTTP/2, plus Connection: close on the request), are
excluded from cooldown, health eviction, and proxy rebinding, and may be
retried up to retry.max_attempts on the anonymous lane instead of once.

Config validation and redaction work on the clean URL, the marker stays
visible in the operator UI, and deduplication keeps a pool entry next to a
static proxy that dials the same endpoint since they are different routes.
2026-10-17 03:30:25 +08:00

104 lines
2.5 KiB
Go

package config
import (
"crypto/sha256"
"encoding/hex"
"net/url"
"sort"
"strings"
"sync/atomic"
)
func MaskValue(value string) string {
runes := []rune(value)
if len(runes) <= 5 {
return "••••"
}
return "••••" + string(runes[len(runes)-5:])
}
func RedactURL(raw string) string {
clean, pool := ParseProxyEntry(raw)
if clean != "" {
raw = clean
}
u, err := url.Parse(raw)
if err != nil {
return "<invalid>"
}
if u.User != nil {
u.User = url.User("***")
}
if pool && u.Fragment == "" {
u.Fragment = "pool"
}
return u.String()
}
type SecretRedactor struct {
values atomic.Value
}
func NewSecretRedactor() *SecretRedactor {
r := &SecretRedactor{}
r.values.Store([]string(nil))
return r
}
func (r *SecretRedactor) Replace(cfg Config) {
values := make([]string, 0, len(cfg.ServerKeys)+len(cfg.ZenKeys)+len(cfg.GoKeys)+2)
values = append(values, cfg.ServerKeys...)
values = append(values, cfg.ZenKeys...)
values = append(values, cfg.GoKeys...)
if cfg.WebUI.Password != "" {
values = append(values, cfg.WebUI.Password)
}
for _, value := range []string{cfg.Upstream.Zen, cfg.Upstream.Go, cfg.Upstream.Anonymous} {
if parsed, err := url.Parse(value); err == nil && parsed.User != nil {
values = append(values, value, parsed.User.Username())
if password, ok := parsed.User.Password(); ok {
values = append(values, password)
}
}
}
for _, value := range cfg.RuntimeProxies() {
if value != "direct" {
clean, _ := ParseProxyEntry(value)
values = append(values, value, clean)
if parsed, err := url.Parse(clean); err == nil && parsed.User != nil {
values = append(values, parsed.User.Username())
if password, ok := parsed.User.Password(); ok {
values = append(values, password)
}
}
}
}
sort.Slice(values, func(i, j int) bool { return len(values[i]) > len(values[j]) })
r.values.Store(values)
}
func (r *SecretRedactor) String(value string) string {
for _, secret := range r.values.Load().([]string) {
if len(secret) >= 4 {
value = strings.ReplaceAll(value, secret, "***")
}
}
return value
}
func Fingerprint(value string) string {
sum := sha256.Sum256([]byte(value))
return hex.EncodeToString(sum[:])[:10]
}
// keyDisplayID is intentionally separate from secretFingerprint. The latter
// is an internal stable identifier used by session/config bookkeeping; this
// value is safe for logs and the operator UI and shows only the key suffix.
func KeyDisplayID(value string) string {
runes := []rune(value)
if len(runes) <= 5 {
return string(runes)
}
return string(runes[len(runes)-5:])
}