157 lines
4.0 KiB
Go
157 lines
4.0 KiB
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"ghproxy/config"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/WJQSERVER-STUDIO/go-utils/limitreader"
|
|
"github.com/cloudwego/hertz/pkg/app"
|
|
)
|
|
|
|
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
|
|
|
var (
|
|
req *http.Request
|
|
resp *http.Response
|
|
err error
|
|
)
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
if resp != nil && resp.Body != nil {
|
|
err := resp.Body.Close()
|
|
if err != nil {
|
|
logError("Failed to close response body: %v", err)
|
|
}
|
|
}
|
|
}()
|
|
|
|
rb := client.NewRequestBuilder(string(c.Request.Method()), u)
|
|
rb.NoDefaultHeaders()
|
|
//rb.SetBody(bytes.NewBuffer(c.Request.Body()))
|
|
rb.SetBody(c.RequestBodyStream())
|
|
rb.WithContext(ctx)
|
|
|
|
req, err = rb.Build()
|
|
if err != nil {
|
|
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
|
return
|
|
}
|
|
|
|
setRequestHeaders(c, req, cfg, matcher)
|
|
AuthPassThrough(c, cfg, req)
|
|
|
|
resp, err = client.Do(req)
|
|
if err != nil {
|
|
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
|
return
|
|
}
|
|
|
|
// 错误处理(404)
|
|
if resp.StatusCode == 404 {
|
|
ErrorPage(c, NewErrorWithStatusLookup(404, "Page Not Found (From Github)"))
|
|
return
|
|
}
|
|
|
|
// 处理302情况
|
|
if resp.StatusCode == 302 {
|
|
finalURL := resp.Header.Get("Location")
|
|
if finalURL != "" {
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
logError("Failed to close response body: %v", err)
|
|
}
|
|
c.Request.Header.Del("Referer")
|
|
logInfo("Internal Redirecting to %s", finalURL)
|
|
ChunkedProxyRequest(ctx, c, finalURL, cfg, matcher)
|
|
}
|
|
}
|
|
|
|
var (
|
|
bodySize int
|
|
contentLength string
|
|
sizelimit int
|
|
)
|
|
sizelimit = cfg.Server.SizeLimit * 1024 * 1024
|
|
contentLength = resp.Header.Get("Content-Length")
|
|
if contentLength != "" {
|
|
var err error
|
|
bodySize, err = strconv.Atoi(contentLength)
|
|
if err != nil {
|
|
logWarning("%s %s %s %s %s Content-Length header is not a valid integer: %v", c.ClientIP(), c.Method(), c.Path(), c.UserAgent(), c.Request.Header.GetProtocol(), err)
|
|
bodySize = -1
|
|
}
|
|
if err == nil && bodySize > sizelimit {
|
|
finalURL := resp.Request.URL.String()
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
logError("Failed to close response body: %v", err)
|
|
}
|
|
c.Redirect(301, []byte(finalURL))
|
|
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Method(), c.Path(), c.UserAgent(), c.Request.Header.GetProtocol(), finalURL, bodySize)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 复制响应头,排除需要移除的 header
|
|
for key, values := range resp.Header {
|
|
if _, shouldRemove := respHeadersToRemove[key]; !shouldRemove {
|
|
for _, value := range values {
|
|
c.Header(key, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
switch cfg.Server.Cors {
|
|
case "*":
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
case "":
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
case "nil":
|
|
c.Header("Access-Control-Allow-Origin", "")
|
|
default:
|
|
c.Header("Access-Control-Allow-Origin", cfg.Server.Cors)
|
|
}
|
|
|
|
c.Status(resp.StatusCode)
|
|
|
|
bodyReader := resp.Body
|
|
|
|
if cfg.RateLimit.BandwidthLimit.Enabled {
|
|
bodyReader = limitreader.NewRateLimitedReader(bodyReader, bandwidthLimit, int(bandwidthBurst), ctx)
|
|
}
|
|
|
|
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
|
|
// 判断body是不是gzip
|
|
var compress string
|
|
if resp.Header.Get("Content-Encoding") == "gzip" {
|
|
compress = "gzip"
|
|
}
|
|
|
|
logDebug("Use Shell Editor: %s %s %s %s %s", c.ClientIP(), c.Request.Method(), u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol())
|
|
c.Header("Content-Length", "")
|
|
|
|
var reader io.Reader
|
|
|
|
reader, _, err = processLinks(bodyReader, compress, string(c.Request.Host()), cfg)
|
|
c.SetBodyStream(reader, -1)
|
|
if err != nil {
|
|
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), c.Request.Method(), u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
|
ErrorPage(c, NewErrorWithStatusLookup(500, fmt.Sprintf("Failed to copy response body: %v", err)))
|
|
return
|
|
}
|
|
} else {
|
|
|
|
if contentLength != "" {
|
|
c.SetBodyStream(bodyReader, bodySize)
|
|
return
|
|
}
|
|
c.SetBodyStream(bodyReader, -1)
|
|
}
|
|
|
|
}
|