Add SOCKS5 and HTTP/HTTPS proxy support

- Updated `GetAuthenticatedClient` to handle proxy configuration via `proxy-url`.
- Extended `Config` to include `proxy-url` property.
- Adjusted error handling and removed unused JSON error response logic for API handlers.
- Updated documentation and configuration examples to reflect new proxy settings.
This commit is contained in:
Luis Pater
2025-07-03 16:50:20 +08:00
parent 827bd6e356
commit d29245666e
7 changed files with 77 additions and 28 deletions

View File

@@ -429,12 +429,15 @@ func (h *APIHandlers) handleNonStreamingResponse(c *gin.Context, rawJson []byte)
}
case err, okError := <-errChan:
if okError {
c.JSON(http.StatusInternalServerError, ErrorResponse{
Error: ErrorDetail{
Message: err.Error(),
Type: "server_error",
},
})
c.Status(http.StatusInternalServerError)
_, _ = fmt.Fprint(c.Writer, err.Error())
flusher.Flush()
// c.JSON(http.StatusInternalServerError, ErrorResponse{
// Error: ErrorDetail{
// Message: err.Error(),
// Type: "server_error",
// },
// })
cliCancel()
return
}
@@ -523,12 +526,15 @@ func (h *APIHandlers) handleStreamingResponse(c *gin.Context, rawJson []byte) {
}
case err, okError := <-errChan:
if okError {
c.JSON(http.StatusInternalServerError, ErrorResponse{
Error: ErrorDetail{
Message: err.Error(),
Type: "server_error",
},
})
c.Status(http.StatusInternalServerError)
_, _ = fmt.Fprint(c.Writer, err.Error())
flusher.Flush()
// c.JSON(http.StatusInternalServerError, ErrorResponse{
// Error: ErrorDetail{
// Message: err.Error(),
// Type: "server_error",
// },
// })
cliCancel()
return
}

View File

@@ -5,10 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/luispater/CLIProxyAPI/internal/config"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"golang.org/x/net/proxy"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
@@ -39,7 +43,42 @@ type TokenStorage struct {
// GetAuthenticatedClient configures and returns an HTTP client with OAuth2 tokens.
// It handles the entire flow: loading, refreshing, and fetching new tokens.
func GetAuthenticatedClient(ctx context.Context, ts *TokenStorage, authDir string) (*http.Client, error) {
func GetAuthenticatedClient(ctx context.Context, ts *TokenStorage, cfg *config.Config) (*http.Client, error) {
proxyURL, err := url.Parse(cfg.ProxyUrl)
if err == nil {
if proxyURL.Scheme == "socks5" {
username := proxyURL.User.Username()
password, _ := proxyURL.User.Password()
auth := &proxy.Auth{
User: username,
Password: password,
}
dialer, errSOCKS5 := proxy.SOCKS5("tcp", proxyURL.Host, auth, proxy.Direct)
if errSOCKS5 != nil {
log.Fatalf("create SOCKS5 dialer failed: %v", errSOCKS5)
}
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (c net.Conn, err error) {
return dialer.Dial(network, addr)
},
}
proxyClient := &http.Client{
Transport: transport,
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, proxyClient)
} else if proxyURL.Scheme == "http" || proxyURL.Scheme == "https" {
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
proxyClient := &http.Client{
Transport: transport,
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, proxyClient)
}
}
conf := &oauth2.Config{
ClientID: oauthClientID,
ClientSecret: oauthClientSecret,
@@ -49,7 +88,6 @@ func GetAuthenticatedClient(ctx context.Context, ts *TokenStorage, authDir strin
}
var token *oauth2.Token
var err error
if ts.Token == nil {
log.Info("Could not load token from file, starting OAuth flow.")
@@ -57,7 +95,7 @@ func GetAuthenticatedClient(ctx context.Context, ts *TokenStorage, authDir strin
if err != nil {
return nil, fmt.Errorf("failed to get token from web: %w", err)
}
ts, err = saveTokenToFile(ctx, conf, token, ts.ProjectID, authDir)
ts, err = saveTokenToFile(ctx, conf, token, ts.ProjectID, cfg.AuthDir)
if err != nil {
// Log the error but proceed, as we have a valid token for the session.
log.Errorf("Warning: failed to save token to file: %v", err)

View File

@@ -284,7 +284,9 @@ func (c *Client) StreamAPIRequest(ctx context.Context, endpoint string, body int
_ = resp.Body.Close()
}()
bodyBytes, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("api streaming request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
return nil, fmt.Errorf(string(bodyBytes))
// return nil, fmt.Errorf("api streaming request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
}
return resp.Body, nil

View File

@@ -8,10 +8,11 @@ import (
// Config represents the application's configuration
type Config struct {
Port int `yaml:"port"`
AuthDir string `yaml:"auth_dir"`
Debug bool `yaml:"debug"`
ApiKeys []string `yaml:"api_keys"`
Port int `yaml:"port"`
AuthDir string `yaml:"auth_dir"`
Debug bool `yaml:"debug"`
ProxyUrl string `yaml:"proxy-url"`
ApiKeys []string `yaml:"api_keys"`
}
// / LoadConfig loads the configuration from the specified file