mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-03-29 16:54:41 +00:00
## 中文说明 ### 连接池优化 - 为 AMP 代理、SOCKS5 代理和 HTTP 代理配置优化的连接池参数 - MaxIdleConnsPerHost 从默认的 2 增加到 20,支持更多并发用户 - MaxConnsPerHost 设为 0(无限制),避免连接瓶颈 - 添加 IdleConnTimeout (90s) 和其他超时配置 ### Kiro 执行器增强 - 添加 Event Stream 消息解析的边界保护,防止越界访问 - 实现实时使用量估算(每 5000 字符或 15 秒发送 ping 事件) - 正确从上游事件中提取并传递 stop_reason - 改进输入 token 计算,优先使用 Claude 格式解析 - 添加 max_tokens 截断警告日志 ### Token 计算改进 - 添加 tokenizer 缓存(sync.Map)避免重复创建 - 为 Claude/Kiro/AmazonQ 模型添加 1.1 调整因子 - 新增 countClaudeChatTokens 函数支持 Claude API 格式 - 支持图像 token 估算(基于尺寸计算) ### 认证刷新优化 - RefreshLead 从 30 分钟改为 5 分钟,与 Antigravity 保持一致 - 修复 NextRefreshAfter 设置,防止频繁刷新检查 - refreshFailureBackoff 从 5 分钟改为 1 分钟,加快失败恢复 --- ## English Description ### Connection Pool Optimization - Configure optimized connection pool parameters for AMP proxy, SOCKS5 proxy, and HTTP proxy - Increase MaxIdleConnsPerHost from default 2 to 20 to support more concurrent users - Set MaxConnsPerHost to 0 (unlimited) to avoid connection bottlenecks - Add IdleConnTimeout (90s) and other timeout configurations ### Kiro Executor Enhancements - Add boundary protection for Event Stream message parsing to prevent out-of-bounds access - Implement real-time usage estimation (send ping events every 5000 chars or 15 seconds) - Correctly extract and pass stop_reason from upstream events - Improve input token calculation, prioritize Claude format parsing - Add max_tokens truncation warning logs ### Token Calculation Improvements - Add tokenizer cache (sync.Map) to avoid repeated creation - Add 1.1 adjustment factor for Claude/Kiro/AmazonQ models - Add countClaudeChatTokens function to support Claude API format - Support image token estimation (calculated based on dimensions) ### Authentication Refresh Optimization - Change RefreshLead from 30 minutes to 5 minutes, consistent with Antigravity - Fix NextRefreshAfter setting to prevent frequent refresh checks - Change refreshFailureBackoff from 5 minutes to 1 minute for faster failure recovery
67 lines
2.5 KiB
Go
67 lines
2.5 KiB
Go
// Package util provides utility functions for the CLI Proxy API server.
|
|
// It includes helper functions for proxy configuration, HTTP client setup,
|
|
// log level management, and other common operations used across the application.
|
|
package util
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/net/proxy"
|
|
)
|
|
|
|
// SetProxy configures the provided HTTP client with proxy settings from the configuration.
|
|
// It supports SOCKS5, HTTP, and HTTPS proxies. The function modifies the client's transport
|
|
// to route requests through the configured proxy server.
|
|
func SetProxy(cfg *config.SDKConfig, httpClient *http.Client) *http.Client {
|
|
var transport *http.Transport
|
|
// Attempt to parse the proxy URL from the configuration.
|
|
proxyURL, errParse := url.Parse(cfg.ProxyURL)
|
|
if errParse == nil {
|
|
// Handle different proxy schemes.
|
|
if proxyURL.Scheme == "socks5" {
|
|
// Configure SOCKS5 proxy with optional authentication.
|
|
var proxyAuth *proxy.Auth
|
|
if proxyURL.User != nil {
|
|
username := proxyURL.User.Username()
|
|
password, _ := proxyURL.User.Password()
|
|
proxyAuth = &proxy.Auth{User: username, Password: password}
|
|
}
|
|
dialer, errSOCKS5 := proxy.SOCKS5("tcp", proxyURL.Host, proxyAuth, proxy.Direct)
|
|
if errSOCKS5 != nil {
|
|
log.Errorf("create SOCKS5 dialer failed: %v", errSOCKS5)
|
|
return httpClient
|
|
}
|
|
// Set up a custom transport using the SOCKS5 dialer with optimized connection pooling
|
|
transport = &http.Transport{
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
return dialer.Dial(network, addr)
|
|
},
|
|
MaxIdleConns: 100,
|
|
MaxIdleConnsPerHost: 20, // Increased from default 2 to support more concurrent users
|
|
MaxConnsPerHost: 0, // No limit on max concurrent connections per host
|
|
IdleConnTimeout: 90 * time.Second,
|
|
}
|
|
} else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
|
|
// Configure HTTP or HTTPS proxy with optimized connection pooling
|
|
transport = &http.Transport{
|
|
Proxy: http.ProxyURL(proxyURL),
|
|
MaxIdleConns: 100,
|
|
MaxIdleConnsPerHost: 20, // Increased from default 2 to support more concurrent users
|
|
MaxConnsPerHost: 0, // No limit on max concurrent connections per host
|
|
IdleConnTimeout: 90 * time.Second,
|
|
}
|
|
}
|
|
}
|
|
// If a new transport was created, apply it to the HTTP client.
|
|
if transport != nil {
|
|
httpClient.Transport = transport
|
|
}
|
|
return httpClient
|
|
}
|