From 45dcef5702d68d6ae47eb82181cab4e55b023118 Mon Sep 17 00:00:00 2001 From: liuyuanchuang Date: Fri, 6 Mar 2026 11:03:41 +0800 Subject: [PATCH] feat: add proxy --- config/config.go | 1 + config/config_dev.yaml | 7 ++++--- config/config_prod.yaml | 6 ++++++ internal/service/user_service.go | 19 ++++++++++++++++--- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 3fdd5b5..25201cc 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,7 @@ type GoogleOAuthConfig struct { ClientID string `mapstructure:"client_id"` ClientSecret string `mapstructure:"client_secret"` RedirectURI string `mapstructure:"redirect_uri"` + Proxy string `mapstructure:"proxy"` } type MathpixConfig struct { diff --git a/config/config_dev.yaml b/config/config_dev.yaml index ec788d0..8137e80 100644 --- a/config/config_dev.yaml +++ b/config/config_dev.yaml @@ -52,6 +52,7 @@ baidu_ocr: token: "e3a47bd2438f1f38840c203fc5939d17a54482d1" google: - client_id: "" - client_secret: "" - redirect_uri: "http://localhost:5173/auth/callback" + client_id: "404402221037-nqdsk11bkpk5a7oh396mrg1ieh28u6q1.apps.googleusercontent.com" + client_secret: "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" + redirect_uri: "https://app.cloud.texpixel.com:10443/auth/callback" + proxy: "http://localhost:7890" diff --git a/config/config_prod.yaml b/config/config_prod.yaml index c0d13cf..93342dd 100644 --- a/config/config_prod.yaml +++ b/config/config_prod.yaml @@ -50,3 +50,9 @@ mathpix: baidu_ocr: token: "e3a47bd2438f1f38840c203fc5939d17a54482d1" + +google: + client_id: "404402221037-nqdsk11bkpk5a7oh396mrg1ieh28u6q1.apps.googleusercontent.com" + client_secret: "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" + redirect_uri: "https://texpixel.com/auth/callback" + proxy: "http://100.115.184.74:7890" diff --git a/internal/service/user_service.go b/internal/service/user_service.go index 8c5ddd3..97929c9 100644 --- a/internal/service/user_service.go +++ b/internal/service/user_service.go @@ -9,7 +9,8 @@ import ( "net/http" "net/url" - "gitea.com/texpixel/document_ai/internal/model/user" + "gitea.com/texpixel/document_ai/config" + model "gitea.com/texpixel/document_ai/internal/model/user" "gitea.com/texpixel/document_ai/internal/storage/cache" "gitea.com/texpixel/document_ai/internal/storage/dao" "gitea.com/texpixel/document_ai/pkg/common" @@ -171,6 +172,17 @@ type googleTokenResponse struct { TokenType string `json:"token_type"` } +func (svc *UserService) googleHTTPClient() *http.Client { + if config.GlobalConfig.Google.Proxy == "" { + return &http.Client{} + } + proxyURL, err := url.Parse(config.GlobalConfig.Google.Proxy) + if err != nil { + return &http.Client{} + } + return &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}} +} + func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, clientID, clientSecret, code, redirectURI string) (*model.GoogleUserInfo, error) { tokenURL := "https://oauth2.googleapis.com/token" formData := url.Values{ @@ -181,7 +193,8 @@ func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, cl "redirect_uri": {redirectURI}, } - resp, err := http.PostForm(tokenURL, formData) + client := svc.googleHTTPClient() + resp, err := client.PostForm(tokenURL, formData) if err != nil { log.Error(ctx, "func", "ExchangeGoogleCodeAndGetUserInfo", "msg", "exchange code failed", "error", err) return nil, err @@ -219,7 +232,7 @@ func (svc *UserService) getGoogleUserInfo(ctx context.Context, accessToken strin } req.Header.Set("Authorization", "Bearer "+accessToken) - client := &http.Client{} + client := svc.googleHTTPClient() resp, err := client.Do(req) if err != nil { return nil, err