feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+16 -16
View File
@@ -211,8 +211,8 @@ func modelPricingSyncValues(data map[string]any, name string) map[string]any {
func FetchUpstreamRatios(c *gin.Context) {
var req dto.UpstreamRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.SysError("failed to bind upstream request: " + err.Error())
c.JSON(http.StatusBadRequest, gin.H{"success": false, "message": "请求参数格式错误"})
common.SysError(common.LogText("failed to bind upstream request: %s", err.Error()))
common.ApiErrorStatus(c, http.StatusBadRequest, common.NewMessage("Invalid request parameter format"))
return
}
@@ -239,8 +239,8 @@ func FetchUpstreamRatios(c *gin.Context) {
}
dbChannels, err := model.GetChannelsByIds(intIds)
if err != nil {
logger.LogError(c.Request.Context(), "failed to query channels: "+err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "message": "查询渠道失败"})
logger.LogError(c.Request.Context(), common.LogText("failed to query channels: %s", err.Error()))
common.ApiErrorStatus(c, http.StatusInternalServerError, common.NewMessage("Failed to query channels"))
return
}
for _, ch := range dbChannels {
@@ -256,7 +256,7 @@ func FetchUpstreamRatios(c *gin.Context) {
}
if len(upstreams) == 0 {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "无有效上游渠道"})
common.ApiErrorT(c, "No valid upstream channel")
return
}
@@ -268,7 +268,7 @@ func FetchUpstreamRatios(c *gin.Context) {
dialer := &net.Dialer{Timeout: 10 * time.Second}
transport := &http.Transport{MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, ResponseHeaderTimeout: 10 * time.Second}
if common.TLSInsecureSkipVerify {
transport.TLSClientConfig = common.InsecureTLSConfig
transport.TLSClientConfig = common.InsecureTLSConfig.Clone()
}
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
host, _, err := net.SplitHostPort(addr)
@@ -322,7 +322,7 @@ func FetchUpstreamRatios(c *gin.Context) {
httpReq, err := http.NewRequestWithContext(ctx, http.MethodGet, fullURL, nil)
if err != nil {
logger.LogWarn(c.Request.Context(), "build request failed: "+err.Error())
logger.LogWarn(c.Request.Context(), common.LogText("build request failed: %s", err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
return
}
@@ -360,25 +360,25 @@ func FetchUpstreamRatios(c *gin.Context) {
time.Sleep(time.Duration(200*(1<<attempt)) * time.Millisecond)
}
if lastErr != nil {
logger.LogWarn(c.Request.Context(), "http error on "+chItem.Name+": "+lastErr.Error())
logger.LogWarn(c.Request.Context(), common.LogText("http error on %s: %s", chItem.Name, lastErr.Error()))
ch <- upstreamResult{Name: uniqueName, Err: lastErr.Error()}
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logger.LogWarn(c.Request.Context(), "non-200 from "+chItem.Name+": "+resp.Status)
logger.LogWarn(c.Request.Context(), common.LogText("non-200 from %s: %s", chItem.Name, resp.Status))
ch <- upstreamResult{Name: uniqueName, Err: resp.Status}
return
}
// Content-Type 和响应体大小校验
if ct := resp.Header.Get("Content-Type"); ct != "" && !strings.Contains(strings.ToLower(ct), "application/json") {
logger.LogWarn(c.Request.Context(), "unexpected content-type from "+chItem.Name+": "+ct)
logger.LogWarn(c.Request.Context(), common.LogText("unexpected content-type from %s: %s", chItem.Name, ct))
}
limited := io.LimitReader(resp.Body, maxRatioConfigBytes)
bodyBytes, err := io.ReadAll(limited)
if err != nil {
logger.LogWarn(c.Request.Context(), "read response failed from "+chItem.Name+": "+err.Error())
logger.LogWarn(c.Request.Context(), common.LogText("read response failed from %s: %s", chItem.Name, err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
return
}
@@ -387,7 +387,7 @@ func FetchUpstreamRatios(c *gin.Context) {
if isOpenRouter {
converted, err := convertOpenRouterToRatioData(bytes.NewReader(bodyBytes))
if err != nil {
logger.LogWarn(c.Request.Context(), "OpenRouter parse failed from "+chItem.Name+": "+err.Error())
logger.LogWarn(c.Request.Context(), common.LogText("OpenRouter parse failed from %s: %s", chItem.Name, err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
return
}
@@ -399,7 +399,7 @@ func FetchUpstreamRatios(c *gin.Context) {
if isModelsDev {
converted, err := convertModelsDevToRatioData(bytes.NewReader(bodyBytes))
if err != nil {
logger.LogWarn(c.Request.Context(), "models.dev parse failed from "+chItem.Name+": "+err.Error())
logger.LogWarn(c.Request.Context(), common.LogText("models.dev parse failed from %s: %s", chItem.Name, err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
return
}
@@ -417,7 +417,7 @@ func FetchUpstreamRatios(c *gin.Context) {
}
if err := common.DecodeJson(bytes.NewReader(bodyBytes), &body); err != nil {
logger.LogWarn(c.Request.Context(), "json decode failed from "+chItem.Name+": "+err.Error())
logger.LogWarn(c.Request.Context(), common.LogText("json decode failed from %s: %s", chItem.Name, err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: err.Error()}
return
}
@@ -462,8 +462,8 @@ func FetchUpstreamRatios(c *gin.Context) {
BillingExpr string `json:"billing_expr"`
}
if err := common.Unmarshal(body.Data, &pricingItems); err != nil {
logger.LogWarn(c.Request.Context(), "unrecognized data format from "+chItem.Name+": "+err.Error())
ch <- upstreamResult{Name: uniqueName, Err: "无法解析上游返回数据"}
logger.LogWarn(c.Request.Context(), common.LogText("unrecognized data format from %s: %s", chItem.Name, err.Error()))
ch <- upstreamResult{Name: uniqueName, Err: "failed to parse the upstream response"}
return
}