重构shell嵌套加速

This commit is contained in:
starry
2025-06-12 08:16:36 +08:00
committed by GitHub
parent db9d11a44e
commit b43874bc17
2 changed files with 421 additions and 184 deletions

View File

@@ -198,9 +198,28 @@ func proxyWithRedirect(c *gin.Context, u string, redirectCount int) {
resp.Header.Del("Referrer-Policy")
resp.Header.Del("Strict-Transport-Security")
// 对于需要处理的shell文件使用chunked传输
isShellFile := strings.HasSuffix(strings.ToLower(u), ".sh")
if isShellFile {
// 智能处理系统 - 自动识别需要加速的内容
// 获取真实域名
realHost := c.Request.Header.Get("X-Forwarded-Host")
if realHost == "" {
realHost = c.Request.Host
}
// 如果域名中没有协议前缀添加https://
if !strings.HasPrefix(realHost, "http://") && !strings.HasPrefix(realHost, "https://") {
realHost = "https://" + realHost
}
// 使用智能处理器自动处理所有内容
processedBody, processedSize, err := ProcessSmart(resp.Body, resp.Header.Get("Content-Encoding") == "gzip", realHost)
if err != nil {
// 优雅降级 - 处理失败时直接返回原内容
c.String(http.StatusInternalServerError, fmt.Sprintf("内容处理错误: %v", err))
return
}
// 智能设置响应头
if processedSize > 0 {
// 内容被处理过使用chunked传输以获得最佳性能
resp.Header.Del("Content-Length")
resp.Header.Set("Transfer-Encoding", "chunked")
}
@@ -224,32 +243,9 @@ func proxyWithRedirect(c *gin.Context, u string, redirectCount int) {
c.Status(resp.StatusCode)
// 处理响应体
if isShellFile {
// 获取真实域名
realHost := c.Request.Header.Get("X-Forwarded-Host")
if realHost == "" {
realHost = c.Request.Host
}
// 如果域名中没有协议前缀添加https://
if !strings.HasPrefix(realHost, "http://") && !strings.HasPrefix(realHost, "https://") {
realHost = "https://" + realHost
}
// 使用ProcessGitHubURLs处理.sh文件
processedBody, _, err := ProcessGitHubURLs(resp.Body, resp.Header.Get("Content-Encoding") == "gzip", realHost, true)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("处理shell文件时发生错误: %v", err))
return
}
if _, err := io.Copy(c.Writer, processedBody); err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("写入响应时发生错误: %v", err))
return
}
} else {
// 对于非.sh文件直接复制响应体
if _, err := io.Copy(c.Writer, resp.Body); err != nil {
return
}
// 输出处理后的内容
if _, err := io.Copy(c.Writer, processedBody); err != nil {
return
}
}
@@ -261,5 +257,3 @@ func checkURL(u string) []string {
}
return nil
}