- Modularized handlers into dedicated packages (`gemini`, `claude`, `cli`) for better structure. - Centralized `ErrorResponse` and `ErrorDetail` types under `handlers` package for reuse. - Updated all handlers to utilize the shared `ErrorResponse` model. - Introduced specialization of handler structs (`GeminiAPIHandlers`, `ClaudeCodeAPIHandlers`, `GeminiCLIAPIHandlers`) for improved clarity and separation of concerns. - Refactored `getClient` logic with additional properties and better state management. Refactor `translator` package by modularizing code for `claude` and `gemini` - Moved Claude-specific logic (`PrepareClaudeRequest`, `ConvertCliToClaude`) to `translator/claude/code`. - Moved Gemini-specific logic (`FixCLIToolResponse`) to `translator/gemini/cli` for better package structure. - Updated affected handler imports and method references. Add comprehensive package-level documentation across key modules - Introduced detailed package-level documentation for core modules: `auth`, `client`, `cmd`, `handlers`, `util`, `watcher`, `config`, `translator`, and `api`. - Enhanced code readability and maintainability by clarifying the purpose and functionality of each package. - Aligned documentation style and tone with existing codebase conventions. Refactor API handlers and translator modules for improved clarity and consistency - Standardized handler struct names (`GeminiAPIHandlers`, `ClaudeCodeAPIHandlers`, `GeminiCLIAPIHandlers`, `OpenAIAPIHandlers`) and updated related comments. - Fixed unnecessary `else` blocks in streaming logic for cleaner error handling. - Renamed variables for better readability (`responseIdResult` to `responseIDResult`, `activationUrl` to `activationURL`, etc.). - Addressed minor inconsistencies in API handler comments and SSE header initialization. - Improved modularization of `claude` and `gemini` translator components. Standardize configuration field naming for consistency across modules - Renamed `ProxyUrl` to `ProxyURL`, `ApiKeys` to `APIKeys`, and `ConfigQuotaExceeded` to `QuotaExceeded`. - Updated all relevant references and comments in `config`, `auth`, `api`, `util`, and `watcher`. - Ensured consistent casing for `GlAPIKey` debug logs.
44 lines
1.5 KiB
Go
44 lines
1.5 KiB
Go
// Package util provides utility functions for the CLI Proxy API server.
|
|
// It includes helper functions for proxy configuration, HTTP client setup,
|
|
// and other common operations used across the application.
|
|
package util
|
|
|
|
import (
|
|
"context"
|
|
"github.com/luispater/CLIProxyAPI/internal/config"
|
|
"golang.org/x/net/proxy"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
// 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.Config, httpClient *http.Client) (*http.Client, error) {
|
|
var transport *http.Transport
|
|
proxyURL, errParse := url.Parse(cfg.ProxyURL)
|
|
if errParse == nil {
|
|
if proxyURL.Scheme == "socks5" {
|
|
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 {
|
|
return nil, errSOCKS5
|
|
}
|
|
transport = &http.Transport{
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
return dialer.Dial(network, addr)
|
|
},
|
|
}
|
|
} else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
|
|
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
|
|
}
|
|
}
|
|
if transport != nil {
|
|
httpClient.Transport = transport
|
|
}
|
|
return httpClient, nil
|
|
}
|