feat(backend): Optimize HTTPS certificate path (#265)
This commit is contained in:
@@ -23,9 +23,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
@@ -33,6 +31,7 @@ import (
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/imagex"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/storage"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/storage/proxy"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
@@ -211,27 +210,9 @@ func (m *minioClient) GetObjectUrl(ctx context.Context, objectKey string, opts .
|
||||
}
|
||||
|
||||
// logs.CtxDebugf(ctx, "[GetObjectUrl] origin presignedURL.String = %s", presignedURL.String())
|
||||
|
||||
proxyPort := os.Getenv(consts.MinIOProxyEndpoint) // :8889
|
||||
if len(proxyPort) > 0 {
|
||||
currentHost, ok := ctxcache.Get[string](ctx, consts.HostKeyInCtx)
|
||||
if !ok {
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
currentScheme, ok := ctxcache.Get[string](ctx, consts.RequestSchemeKeyInCtx)
|
||||
if !ok {
|
||||
return presignedURL.String(), nil
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(currentHost)
|
||||
if err != nil {
|
||||
host = currentHost
|
||||
}
|
||||
minioProxyHost := host + proxyPort
|
||||
presignedURL.Host = minioProxyHost
|
||||
presignedURL.Scheme = currentScheme
|
||||
// logs.CtxDebugf(ctx, "[GetObjectUrl] reset presignedURL.String = %s", presignedURL.String())
|
||||
ok, proxyURL := proxy.CheckIfNeedReplaceHost(ctx, presignedURL.String())
|
||||
if ok {
|
||||
return proxyURL, nil
|
||||
}
|
||||
|
||||
return presignedURL.String(), nil
|
||||
@@ -265,7 +246,6 @@ func (m *minioClient) GetUploadAuth(ctx context.Context, opt ...imagex.UploadAut
|
||||
}
|
||||
|
||||
func (m *minioClient) GetResourceURL(ctx context.Context, uri string, opts ...imagex.GetResourceOpt) (*imagex.ResourceURL, error) {
|
||||
|
||||
url, err := m.GetObjectUrl(ctx, uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -273,11 +253,12 @@ func (m *minioClient) GetResourceURL(ctx context.Context, uri string, opts ...im
|
||||
return &imagex.ResourceURL{
|
||||
URL: url,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (m *minioClient) Upload(ctx context.Context, data []byte, opts ...imagex.UploadAuthOpt) (*imagex.UploadResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *minioClient) GetUploadAuthWithExpire(ctx context.Context, expire time.Duration, opt ...imagex.UploadAuthOpt) (*imagex.SecurityToken, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
63
backend/infra/impl/storage/proxy/proxy.go
Normal file
63
backend/infra/impl/storage/proxy/proxy.go
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/logs"
|
||||
"github.com/coze-dev/coze-studio/backend/types/consts"
|
||||
)
|
||||
|
||||
func CheckIfNeedReplaceHost(ctx context.Context, originURLStr string) (ok bool, proxyURL string) {
|
||||
// url parse
|
||||
originURL, err := url.Parse(originURLStr)
|
||||
if err != nil {
|
||||
logs.CtxWarnf(ctx, "[CheckIfNeedReplaceHost] url parse failed, err: %v", err)
|
||||
return false, ""
|
||||
}
|
||||
|
||||
proxyPort := os.Getenv(consts.MinIOProxyEndpoint) // :8889
|
||||
if proxyPort == "" {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
currentHost, ok := ctxcache.Get[string](ctx, consts.HostKeyInCtx)
|
||||
if !ok {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
currentScheme, ok := ctxcache.Get[string](ctx, consts.RequestSchemeKeyInCtx)
|
||||
if !ok {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(currentHost)
|
||||
if err != nil {
|
||||
host = currentHost
|
||||
}
|
||||
|
||||
minioProxyHost := host + proxyPort
|
||||
originURL.Host = minioProxyHost
|
||||
originURL.Scheme = currentScheme
|
||||
logs.CtxDebugf(ctx, "[CheckIfNeedReplaceHost] reset originURL.String = %s", originURL.String())
|
||||
return true, originURL.String()
|
||||
}
|
||||
@@ -21,9 +21,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
@@ -33,6 +30,7 @@ import (
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/imagex"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/storage"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/storage/proxy"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/logs"
|
||||
@@ -219,34 +217,9 @@ func (t *s3Client) GetObjectUrl(ctx context.Context, objectKey string, opts ...s
|
||||
return "", fmt.Errorf("get object presigned url failed: %v", err)
|
||||
}
|
||||
|
||||
// url parse
|
||||
url, err := url.Parse(req.URL)
|
||||
if err != nil {
|
||||
logs.CtxWarnf(ctx, "[GetObjectUrl] url parse failed, err: %v", err)
|
||||
return req.URL, nil
|
||||
}
|
||||
|
||||
proxyPort := os.Getenv(consts.MinIOProxyEndpoint) // :8889
|
||||
if len(proxyPort) > 0 {
|
||||
currentHost, ok := ctxcache.Get[string](ctx, consts.HostKeyInCtx)
|
||||
if !ok {
|
||||
return req.URL, nil
|
||||
}
|
||||
|
||||
currentScheme, ok := ctxcache.Get[string](ctx, consts.RequestSchemeKeyInCtx)
|
||||
if !ok {
|
||||
return req.URL, nil
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(currentHost)
|
||||
if err != nil {
|
||||
host = currentHost
|
||||
}
|
||||
minioProxyHost := host + proxyPort
|
||||
url.Host = minioProxyHost
|
||||
url.Scheme = currentScheme
|
||||
logs.CtxInfof(ctx, "[GetObjectUrl] reset ORG.URL = %s TOS.URL = %s", req.URL, url.String())
|
||||
return url.String(), nil
|
||||
ok, proxyURL := proxy.CheckIfNeedReplaceHost(ctx, req.URL)
|
||||
if ok {
|
||||
return proxyURL, nil
|
||||
}
|
||||
|
||||
return req.URL, nil
|
||||
@@ -258,7 +231,6 @@ func (i *s3Client) GetUploadHost(ctx context.Context) string {
|
||||
return ""
|
||||
}
|
||||
return currentHost + consts.ApplyUploadActionURI
|
||||
|
||||
}
|
||||
|
||||
func (t *s3Client) GetServerID() string {
|
||||
|
||||
@@ -21,10 +21,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"
|
||||
@@ -32,6 +29,7 @@ import (
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/imagex"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/contract/storage"
|
||||
"github.com/coze-dev/coze-studio/backend/infra/impl/storage/proxy"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/conv"
|
||||
@@ -213,34 +211,9 @@ func (t *tosClient) GetObjectUrl(ctx context.Context, objectKey string, opts ...
|
||||
return "", err
|
||||
}
|
||||
|
||||
// url parse
|
||||
url, err := url.Parse(output.SignedUrl)
|
||||
if err != nil {
|
||||
logs.CtxWarnf(ctx, "[GetObjectUrl] url parse failed, err: %v", err)
|
||||
return output.SignedUrl, nil
|
||||
}
|
||||
|
||||
proxyPort := os.Getenv(consts.MinIOProxyEndpoint) // :8889
|
||||
if len(proxyPort) > 0 {
|
||||
currentHost, ok := ctxcache.Get[string](ctx, consts.HostKeyInCtx)
|
||||
if !ok {
|
||||
return output.SignedUrl, nil
|
||||
}
|
||||
|
||||
currentScheme, ok := ctxcache.Get[string](ctx, consts.RequestSchemeKeyInCtx)
|
||||
if !ok {
|
||||
return output.SignedUrl, nil
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(currentHost)
|
||||
if err != nil {
|
||||
host = currentHost
|
||||
}
|
||||
minioProxyHost := host + proxyPort
|
||||
url.Host = minioProxyHost
|
||||
url.Scheme = currentScheme
|
||||
// logs.CtxDebugf(ctx, "[GetObjectUrl] reset \n ORG.URL = %s \n TOS.URL = %s", output.SignedUrl, url.String())
|
||||
return url.String(), nil
|
||||
ok, proxyURL := proxy.CheckIfNeedReplaceHost(ctx, output.SignedUrl)
|
||||
if ok {
|
||||
return proxyURL, nil
|
||||
}
|
||||
|
||||
return output.SignedUrl, nil
|
||||
|
||||
Reference in New Issue
Block a user