diff --git a/README.md b/README.md index 99f1a65..e524255 100644 --- a/README.md +++ b/README.md @@ -186,10 +186,18 @@ OAuth routes follow separate rules: the selected OAuth tier owns the request, us | `performance.max_conns_per_host` | `0` | Connection cap; zero means unlimited. | | `performance.idle_conn_timeout_seconds` | `120` | Idle connection lifetime. | -`proxies` accepts `direct`, HTTP, HTTPS, SOCKS5, and SOCKS5H URLs. `proxyfile` adds one proxy per nonempty line, relative to the configuration directory; entries are deduplicated and an empty resolved list means direct access. Line comments start with `#`, `;`, or `//` at the beginning or after whitespace. Credentials embedded in proxy URLs are secrets. +`proxies` accepts `direct` (dual stack), `direct4` (IPv4 only), `direct6` (IPv6 only), HTTP, HTTPS, SOCKS5, and SOCKS5H URLs. Dual stack uses the normal Go address selection and Happy Eyeballs behavior, not alternating IPv4/IPv6 requests. Single-family direct routes never fall back to the other family. `proxyfile` adds one route per nonempty line, relative to the configuration directory; entries are deduplicated and an empty resolved list still means dual-stack `direct`. Line comments start with `#`, `;`, or `//` at the beginning or after whitespace. Credentials embedded in proxy URLs are secrets. Mark a provider-managed rotating endpoint with `pool+https://proxy.example:8443` or `https://proxy.example:8443#pool`. Each attempt then opens a fresh connection without keep-alive/HTTP/2 reuse; these entries do not use static-node cooldown or health eviction. The provider must actually rotate its exits. Static proxies retain connection reuse, health tracking, and key bindings. `proxy_bindings` can pin a key fingerprint to a configured proxy. +To choose a direct address family per upstream key, first include the routes in `proxies` or `proxyfile`, then bind the key fingerprints: + +```json +{"proxies":["direct4","direct6","direct"],"proxy_bindings":{"KEY_A_FINGERPRINT":"direct4","KEY_B_FINGERPRINT":"direct6","KEY_C_FINGERPRINT":"direct"}} +``` + +Both regular and Bun-shaped traffic use the chosen family, including manual-key model discovery and route health checks. Bound keys fail closed rather than migrating to another route or family; unbound keys retain normal failover. This is a per-key restriction: request policy may still select the anonymous lane or another key/tier with its own route. These routes do not configure host networking, select a particular source address, or guarantee extra upstream allowance. `#pool` is only for proxy URLs, not direct routes. + An explicit `x-session-id` provides conversation affinity. OpenCode session headers and supported conversation fields also work; otherwise the initial system/user content supplies a seed. Generated identities are scoped to the local API key and process lifetime. Valid official OpenCode session IDs are preserved. Separate independent conversations with explicit IDs and use separate keys where user isolation is required; identical starts under one key can share affinity. An optional `models.anonymous_backoff` object pauses the anonymous lane after repeated failures. Its defaults are threshold `6`, base `1` hour, maximum `24` hours. Omitting the object disables this lane-level breaker. diff --git a/README.zh-CN.md b/README.zh-CN.md index 61816f4..767964c 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -186,10 +186,18 @@ OAuth 另有规则:请求选择 OAuth 路由后,只使用该 Tier 已确认 | `performance.max_conns_per_host` | `0` | 连接数上限;零表示不限。 | | `performance.idle_conn_timeout_seconds` | `120` | 空闲连接寿命。 | -`proxies` 支持 `direct`、HTTP、HTTPS、SOCKS5、SOCKS5H URL。`proxyfile` 从配置目录相对路径读取,每个非空行一个代理,合并后去重;最终为空时使用直连。行首或空白后的 `#`、`;`、`//` 表示注释。代理 URL 中的账号密码属于敏感信息。 +`proxies` 支持 `direct`(双栈)、`direct4`(仅 IPv4)、`direct6`(仅 IPv6)、HTTP、HTTPS、SOCKS5、SOCKS5H URL。双栈沿用 Go 的地址选择与 Happy Eyeballs,不代表 IPv4/IPv6 轮流发送;单栈直连不会回退另一地址族。`proxyfile` 从配置目录相对路径读取,每个非空行一个路由,合并后去重;最终为空时仍使用双栈 `direct`。行首或空白后的 `#`、`;`、`//` 表示注释。代理 URL 中的账号密码属于敏感信息。 供应商负责轮换出口时,可写成 `pool+https://proxy.example:8443` 或 `https://proxy.example:8443#pool`。每次尝试新建连接,不复用 keep-alive/HTTP/2,也不采用静态节点的冷却和健康淘汰。实际出口轮换由供应商提供。静态代理保留连接复用、健康跟踪与 Key 绑定;`proxy_bindings` 可按 Key 指纹绑定已配置代理。 +若要为不同上游 Key 选择直连地址族,先在 `proxies` 或 `proxyfile` 声明路由,再按 Key 指纹绑定: + +```json +{"proxies":["direct4","direct6","direct"],"proxy_bindings":{"KEY_A_FINGERPRINT":"direct4","KEY_B_FINGERPRINT":"direct6","KEY_C_FINGERPRINT":"direct"}} +``` + +普通与 Bun 伪装传输都会遵守所选地址族,包括手动 Key 的模型目录查询和路由健康检查。已绑定 Key 失败时不会迁移到其他路由或地址族,未绑定 Key 保留原有故障转移行为。这是每 Key 的限制,请求策略仍可能选择匿名通道或使用其他路由的 Key/Tier。这不会配置主机网络、指定源地址或保证额外上游额度;`#pool` 只适用于代理 URL,不适用于直连路由。 + 建议为独立会话发送 `x-session-id`。同时支持 OpenCode 会话头和既有 conversation 字段;未指定时使用初始 system/user 内容生成种子。网关生成的身份按本地 API Key 与进程生命周期隔离,有效的官方 OpenCode 会话 ID 原样保留。同一 Key 下相同开头可能共享亲和性,需要用户隔离时使用独立 Key。 可选 `models.anonymous_backoff` 在连续失败后暂停整个匿名通道,默认阈值 `6` 次、基础 `1` 小时、最大 `24` 小时;不配置此对象则关闭该通道级熔断。 diff --git a/docs/management.md b/docs/management.md index d4bd640..b6b16f7 100644 --- a/docs/management.md +++ b/docs/management.md @@ -159,6 +159,8 @@ Generated keys contain 192 random bits. Custom keys accept 16–256 letters, dig Configuration supports bulk import of inline proxies, individual checks, and staged removal. Manage `proxyfile` entries in their source file; relative paths are resolved against the configuration directory. Removal remains a draft until the complete configuration is saved. +Direct routes are `direct` (dual stack), `direct4` (IPv4 only), and `direct6` (IPv6 only). Add one per line in the proxy form or `proxyfile`; checks use that route's address family. List a route before using it as a `proxy_bindings` target. Bound keys retain fail-closed routing if the selected family is unavailable; dual stack uses Go's normal address selection, not round-robin across families. Saves and reloads apply route changes to new requests without restarting. The existing WARP selection rules are unchanged. Go quota reads and OAuth device authorization/token refresh use separate control-plane clients and are not pinned by these routes. + `POST /api/proxies/check` requires a session and CSRF. Submit exactly one of these requests: ```json diff --git a/docs/management.zh-CN.md b/docs/management.zh-CN.md index 3453c89..d323ed5 100644 --- a/docs/management.zh-CN.md +++ b/docs/management.zh-CN.md @@ -159,6 +159,8 @@ Key、代理、上游、超时、模型、偏好、reasoning 和日志设置热 配置中心支持批量导入内联代理、逐项检查及暂存移除。代理文件 `proxyfile` 的内容应在来源文件管理;相对路径以配置目录为准。移除仅为草稿,保存完整配置后才生效。 +直连路由支持 `direct`(双栈)、`direct4`(仅 IPv4)、`direct6`(仅 IPv6),可在代理表单或 `proxyfile` 中每行填写一个,连通性检查也遵守所选地址族。作为 `proxy_bindings` 目标前须先声明该路由;已绑定 Key 在所选地址族不可用时保持 fail-closed,不会改走另一地址族。双栈使用 Go 的正常地址选择,不是轮流分配 IPv4/IPv6。保存或重载后新请求立即使用新路由,无需重启。既有 WARP 选择规则不变。Go 额度读取及 OAuth 设备授权、Token 刷新使用独立控制面客户端,不受这些路由绑定。 + `POST /api/proxies/check` 要求 Session、CSRF,以下两种请求只能选一种: ```json diff --git a/internal/admin/proxies.go b/internal/admin/proxies.go index 7c40f3c..966cf11 100644 --- a/internal/admin/proxies.go +++ b/internal/admin/proxies.go @@ -35,7 +35,7 @@ func (a *Server) handleCheckProxy(w http.ResponseWriter, r *http.Request) { } } if err := config.ValidateProxyEntry(value); err != nil { - writeAdminError(w, http.StatusBadRequest, "invalid_proxy", "proxy must be direct or an HTTP, HTTPS, SOCKS5 or SOCKS5H URL") + writeAdminError(w, http.StatusBadRequest, "invalid_proxy", "proxy must be direct (dual stack), direct4 (IPv4), direct6 (IPv6), or an HTTP, HTTPS, SOCKS5 or SOCKS5H URL") return } select { diff --git a/internal/config/proxy.go b/internal/config/proxy.go index 0dddcef..5bd0f24 100644 --- a/internal/config/proxy.go +++ b/internal/config/proxy.go @@ -14,7 +14,7 @@ import ( // dial URL alongside whether the entry is a rotating proxy pool (such as Resin). func ParseProxyEntry(raw string) (clean string, pool bool) { trimmed := strings.TrimSpace(raw) - if trimmed == "" || trimmed == "direct" { + if trimmed == "" || DirectNetwork(trimmed) != "" { return trimmed, false } lower := strings.ToLower(trimmed) @@ -35,8 +35,21 @@ func ParseProxyEntry(raw string) (clean string, pool bool) { return trimmed, pool } +func DirectNetwork(raw string) string { + switch strings.TrimSpace(raw) { + case "direct": + return "tcp" + case "direct4": + return "tcp4" + case "direct6": + return "tcp6" + default: + return "" + } +} + func ValidateProxyEntry(raw string) error { - if raw == "direct" { + if DirectNetwork(raw) != "" { return nil } clean, _ := ParseProxyEntry(raw) diff --git a/internal/config/secrets.go b/internal/config/secrets.go index f961d88..427bae1 100644 --- a/internal/config/secrets.go +++ b/internal/config/secrets.go @@ -73,7 +73,7 @@ func (r *Redactor) Replace(cfg Config) { continue } values = append(values, connection.Credential.AccessToken, connection.Credential.RefreshToken) - if connection.Proxy != "" { + if connection.Proxy != "" && DirectNetwork(connection.Proxy) == "" { values = append(values, connection.Proxy) } } @@ -89,7 +89,7 @@ func (r *Redactor) Replace(cfg Config) { } } for _, value := range cfg.RuntimeProxies() { - if value != "direct" { + if DirectNetwork(value) == "" { clean, _ := ParseProxyEntry(value) values = append(values, value, clean) if parsed, err := url.Parse(clean); err == nil && parsed.User != nil { diff --git a/internal/gateway/first_event.go b/internal/gateway/first_event.go index ea1b57a..101a51f 100644 --- a/internal/gateway/first_event.go +++ b/internal/gateway/first_event.go @@ -152,7 +152,7 @@ func (e *proxyUnavailableError) Unwrap() []error { return []error{errProxyUnavai // rather than the upstream. Direct transports are left untouched: there a dial // failure is an upstream reachability problem. func asProxyUnavailable(proxy *proxyTransport, err error) error { - if err == nil || proxy == nil || proxy.name == "direct" { + if err == nil || proxy == nil || config.DirectNetwork(proxy.name) != "" { return err } var pe *phasedError diff --git a/internal/gateway/pool.go b/internal/gateway/pool.go index 6b2a83e..b64d3e3 100644 --- a/internal/gateway/pool.go +++ b/internal/gateway/pool.go @@ -504,13 +504,19 @@ func transportPool(proxies []string, cfg config.PerformanceConfig, responseHeade transport.TLSClientConfig = tlsCfg } } - transport.DialContext = (&net.Dialer{ + dialer := &net.Dialer{ Timeout: time.Duration(cfg.ConnectTimeoutSeconds) * time.Second, KeepAlive: 30 * time.Second, - }).DialContext + } + transport.DialContext = dialer.DialContext var proxyURL *url.URL - if clean == "direct" { + var directDial func(context.Context, string, string) (net.Conn, error) + if network := config.DirectNetwork(clean); network != "" { transport.Proxy = nil + directDial = func(ctx context.Context, _ string, address string) (net.Conn, error) { + return dialer.DialContext(ctx, network, address) + } + transport.DialContext = directDial } else { u, err := url.Parse(clean) if err != nil { @@ -521,6 +527,7 @@ func transportPool(proxies []string, cfg config.PerformanceConfig, responseHeade } bun, err := httpx.NewBunTransport(httpx.BunOptions{ Proxy: proxyURL, + DialContext: directDial, ConnectTimeout: time.Duration(cfg.ConnectTimeoutSeconds) * time.Second, ResponseHeaderTimeout: responseHeaderTimeout, IdleConnTimeout: transport.IdleConnTimeout, diff --git a/internal/gateway/proxy_check.go b/internal/gateway/proxy_check.go index 23c746c..22a7a1a 100644 --- a/internal/gateway/proxy_check.go +++ b/internal/gateway/proxy_check.go @@ -118,7 +118,7 @@ func proxyCheckFailure(ctx context.Context, value string, err error) ProxyCheckR return result } clean, _ := config.ParseProxyEntry(value) - if clean == "direct" { + if config.DirectNetwork(clean) != "" { return result } var routeError *net.OpError diff --git a/webui/index.html b/webui/index.html index 0cf6b94..aaf641b 100644 --- a/webui/index.html +++ b/webui/index.html @@ -925,6 +925,7 @@
支持 direct(双栈)、direct4(仅 IPv4)、direct6(仅 IPv6)、HTTP(S) 和 SOCKS URL。代理 URL 可添加 #pool 标记为轮换代理池,每次尝试新建连接,不使用节点冷却或健康淘汰。
仅检查指定代理到 Zen 的无认证连通性,不发模型推理、不验证额度、不使用上游 Key,也不依赖匿名通道开关。401 表示已到达上游且需认证;上游拒绝或限流不代表代理永久损坏,只有确认代理故障才会进入「选中确认故障」。
检查与移除仅管理下列内联代理和新增草稿;proxyfile 条目请在文件中管理。移除需保存后生效,绑定验证失败时不会自动删除绑定。