From 74392efbed7c701243e3b3a155d2ae95d4f5d99b Mon Sep 17 00:00:00 2001 From: wozulong <> Date: Tue, 28 May 2024 11:57:05 +0800 Subject: [PATCH] merge upstream Signed-off-by: wozulong <> --- common/constants.go | 1 + common/image.go | 7 ++-- common/utils.go | 58 +++++++++++++++++++++++++++++ model/option.go | 3 ++ relay/relay-mj.go | 2 +- web/src/components/SystemSetting.js | 21 +++++++++++ 6 files changed, 87 insertions(+), 5 deletions(-) diff --git a/common/constants.go b/common/constants.go index 4a75668..fb5b811 100644 --- a/common/constants.go +++ b/common/constants.go @@ -22,6 +22,7 @@ var StartTime = time.Now().Unix() // unit: second var Version = "v0.0.0" // this hard coding will be replaced automatically when building, no need to manually change var SystemName = "New API" var ServerAddress = "http://localhost:3000" +var OutProxyUrl = "" var Footer = "" var Logo = "" var TopUpLink = "" diff --git a/common/image.go b/common/image.go index 41ff51f..29c9f49 100644 --- a/common/image.go +++ b/common/image.go @@ -8,7 +8,6 @@ import ( "golang.org/x/image/webp" "image" "io" - "net/http" "strings" ) @@ -32,7 +31,7 @@ func DecodeBase64ImageData(base64String string) (image.Config, string, string, e } func IsImageUrl(url string) (bool, error) { - resp, err := http.Head(url) + resp, err := ProxiedHttpHead(url, OutProxyUrl) if err != nil { return false, err } @@ -48,7 +47,7 @@ func GetImageFromUrl(url string) (mimeType string, data string, err error) { if !isImage { return } - resp, err := http.Get(url) + resp, err := ProxiedHttpGet(url, OutProxyUrl) if err != nil { return } @@ -64,7 +63,7 @@ func GetImageFromUrl(url string) (mimeType string, data string, err error) { } func DecodeUrlImageData(imageUrl string) (image.Config, string, error) { - response, err := http.Get(imageUrl) + response, err := ProxiedHttpGet(imageUrl, OutProxyUrl) if err != nil { SysLog(fmt.Sprintf("fail to get image from url: %s", err.Error())) return image.Config{}, "", err diff --git a/common/utils.go b/common/utils.go index 6c89d41..8c4bffb 100644 --- a/common/utils.go +++ b/common/utils.go @@ -1,13 +1,18 @@ package common import ( + "context" "encoding/json" + "errors" "fmt" "github.com/google/uuid" + "golang.org/x/net/proxy" "html/template" "log" "math/rand" "net" + "net/http" + "net/url" "os" "os/exec" "runtime" @@ -267,3 +272,56 @@ func StrToMap(str string) map[string]interface{} { } return m } + +func GetProxiedHttpClient(proxyUrl string) (*http.Client, error) { + if "" == proxyUrl { + return &http.Client{}, nil + } + + u, err := url.Parse(proxyUrl) + if err != nil { + return nil, err + } + + if strings.HasPrefix(proxyUrl, "http") { + + return &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyURL(u), + }, + }, nil + } else if strings.HasPrefix(proxyUrl, "socks") { + dialer, err := proxy.FromURL(u, proxy.Direct) + if err != nil { + return nil, err + } + + return &http.Client{ + Transport: &http.Transport{ + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + return dialer.(proxy.ContextDialer).DialContext(ctx, network, addr) + }, + }, + }, nil + } + + return nil, errors.New("unsupported proxy type") +} + +func ProxiedHttpGet(url, proxyUrl string) (*http.Response, error) { + client, err := GetProxiedHttpClient(proxyUrl) + if err != nil { + return nil, err + } + + return client.Get(url) +} + +func ProxiedHttpHead(url, proxyUrl string) (*http.Response, error) { + client, err := GetProxiedHttpClient(proxyUrl) + if err != nil { + return nil, err + } + + return client.Head(url) +} diff --git a/model/option.go b/model/option.go index ec5ccbd..d8a3942 100644 --- a/model/option.go +++ b/model/option.go @@ -61,6 +61,7 @@ func InitOptionMap() { common.OptionMap["SystemName"] = common.SystemName common.OptionMap["Logo"] = common.Logo common.OptionMap["ServerAddress"] = "" + common.OptionMap["OutProxyUrl"] = "" common.OptionMap["StripeApiSecret"] = common.StripeApiSecret common.OptionMap["StripeWebhookSecret"] = common.StripeWebhookSecret common.OptionMap["StripePriceId"] = common.StripePriceId @@ -242,6 +243,8 @@ func updateOptionMap(key string, value string) (err error) { common.SMTPToken = value case "ServerAddress": common.ServerAddress = value + case "OutProxyUrl": + common.OutProxyUrl = value case "StripeApiSecret": common.StripeApiSecret = value case "StripeWebhookSecret": diff --git a/relay/relay-mj.go b/relay/relay-mj.go index b28f026..fec6161 100644 --- a/relay/relay-mj.go +++ b/relay/relay-mj.go @@ -30,7 +30,7 @@ func RelayMidjourneyImage(c *gin.Context) { }) return } - resp, err := http.Get(midjourneyTask.ImageUrl) + resp, err := common.ProxiedHttpGet(midjourneyTask.ImageUrl, common.OutProxyUrl) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": "http_get_image_failed", diff --git a/web/src/components/SystemSetting.js b/web/src/components/SystemSetting.js index 4b9ed91..1011dc3 100644 --- a/web/src/components/SystemSetting.js +++ b/web/src/components/SystemSetting.js @@ -31,6 +31,7 @@ const SystemSetting = () => { SMTPFrom: '', SMTPToken: '', ServerAddress: '', + OutProxyUrl: '', StripeApiSecret: '', StripeWebhookSecret: '', StripePriceId: '', @@ -150,6 +151,7 @@ const SystemSetting = () => { name === 'Notice' || (name.startsWith('SMTP') && name !== 'SMTPSSLEnabled') || name === 'ServerAddress' || + name === 'OutProxyUrl' || name === 'StripeApiSecret' || name === 'StripeWebhookSecret' || name === 'StripePriceId' || @@ -181,6 +183,11 @@ const SystemSetting = () => { await updateOption('ServerAddress', ServerAddress); }; + const submitOutProxyUrl = async () => { + let OutProxyUrl = removeTrailingSlash(inputs.OutProxyUrl); + await updateOption('OutProxyUrl', OutProxyUrl); + }; + const submitPaymentConfig = async () => { if (inputs.ServerAddress === '') { showError('请先填写服务器地址'); @@ -368,6 +375,20 @@ const SystemSetting = () => { 更新服务器地址 +
+ 代理设置 +
+ + + + 更新代理设置 +
支付设置(当前仅支持Stripe Checkout)