feat: add proxy

This commit is contained in:
liuyuanchuang
2026-03-06 11:03:41 +08:00
parent ed7232e5c0
commit 45dcef5702
4 changed files with 27 additions and 6 deletions

View File

@@ -26,6 +26,7 @@ type GoogleOAuthConfig struct {
ClientID string `mapstructure:"client_id"` ClientID string `mapstructure:"client_id"`
ClientSecret string `mapstructure:"client_secret"` ClientSecret string `mapstructure:"client_secret"`
RedirectURI string `mapstructure:"redirect_uri"` RedirectURI string `mapstructure:"redirect_uri"`
Proxy string `mapstructure:"proxy"`
} }
type MathpixConfig struct { type MathpixConfig struct {

View File

@@ -52,6 +52,7 @@ baidu_ocr:
token: "e3a47bd2438f1f38840c203fc5939d17a54482d1" token: "e3a47bd2438f1f38840c203fc5939d17a54482d1"
google: google:
client_id: "" client_id: "404402221037-nqdsk11bkpk5a7oh396mrg1ieh28u6q1.apps.googleusercontent.com"
client_secret: "" client_secret: "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"
redirect_uri: "http://localhost:5173/auth/callback" redirect_uri: "https://app.cloud.texpixel.com:10443/auth/callback"
proxy: "http://localhost:7890"

View File

@@ -50,3 +50,9 @@ mathpix:
baidu_ocr: baidu_ocr:
token: "e3a47bd2438f1f38840c203fc5939d17a54482d1" 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"

View File

@@ -9,7 +9,8 @@ import (
"net/http" "net/http"
"net/url" "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/cache"
"gitea.com/texpixel/document_ai/internal/storage/dao" "gitea.com/texpixel/document_ai/internal/storage/dao"
"gitea.com/texpixel/document_ai/pkg/common" "gitea.com/texpixel/document_ai/pkg/common"
@@ -171,6 +172,17 @@ type googleTokenResponse struct {
TokenType string `json:"token_type"` 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) { func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, clientID, clientSecret, code, redirectURI string) (*model.GoogleUserInfo, error) {
tokenURL := "https://oauth2.googleapis.com/token" tokenURL := "https://oauth2.googleapis.com/token"
formData := url.Values{ formData := url.Values{
@@ -181,7 +193,8 @@ func (svc *UserService) ExchangeGoogleCodeAndGetUserInfo(ctx context.Context, cl
"redirect_uri": {redirectURI}, "redirect_uri": {redirectURI},
} }
resp, err := http.PostForm(tokenURL, formData) client := svc.googleHTTPClient()
resp, err := client.PostForm(tokenURL, formData)
if err != nil { if err != nil {
log.Error(ctx, "func", "ExchangeGoogleCodeAndGetUserInfo", "msg", "exchange code failed", "error", err) log.Error(ctx, "func", "ExchangeGoogleCodeAndGetUserInfo", "msg", "exchange code failed", "error", err)
return nil, err return nil, err
@@ -219,7 +232,7 @@ func (svc *UserService) getGoogleUserInfo(ctx context.Context, accessToken strin
} }
req.Header.Set("Authorization", "Bearer "+accessToken) req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{} client := svc.googleHTTPClient()
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err