Преглед изворни кода

feat: 防止渠道地址传到用户端

CaIon пре 2 година
родитељ
комит
929cd79ee7
1 измењених фајлова са 11 додато и 4 уклоњено
  1. 11 4
      controller/relay-utils.go

+ 11 - 4
controller/relay-utils.go

@@ -8,6 +8,7 @@ import (
 	"io"
 	"io"
 	"net/http"
 	"net/http"
 	"one-api/common"
 	"one-api/common"
+	"regexp"
 	"strconv"
 	"strconv"
 )
 )
 
 
@@ -106,12 +107,18 @@ func countTokenText(text string, model string) int {
 }
 }
 
 
 func errorWrapper(err error, code string, statusCode int) *OpenAIErrorWithStatusCode {
 func errorWrapper(err error, code string, statusCode int) *OpenAIErrorWithStatusCode {
-	if statusCode == http.StatusInternalServerError {
-		//避免暴露内部错误
-		err = fmt.Errorf("internal server error")
+	text := err.Error()
+	// 定义一个正则表达式匹配URL
+	urlPattern := `http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+`
+
+	urlRegexp, reErr := regexp.Compile(urlPattern)
+	if reErr == nil {
+		text = urlRegexp.ReplaceAllString(text, "https://api.openai.com")
 	}
 	}
+	//避免暴露内部错误
+
 	openAIError := OpenAIError{
 	openAIError := OpenAIError{
-		Message: err.Error(),
+		Message: text,
 		Type:    "one_api_error",
 		Type:    "one_api_error",
 		Code:    code,
 		Code:    code,
 	}
 	}