From 55769d9a400bf06ebd369ed4c686667aa14ca19d Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Tue, 29 Apr 2025 19:51:23 +0800 Subject: [PATCH] use custom headers for raw --- DEV-VERSION | 2 +- proxy/chunkreq.go | 2 +- proxy/gitreq.go | 4 ++-- proxy/reqheader.go | 31 ++++++++++++++++++++++++------- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/DEV-VERSION b/DEV-VERSION index ca629c1..f9c9056 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -25w32a \ No newline at end of file +25w33t-1 \ No newline at end of file diff --git a/proxy/chunkreq.go b/proxy/chunkreq.go index 13be45a..e766a06 100644 --- a/proxy/chunkreq.go +++ b/proxy/chunkreq.go @@ -51,7 +51,7 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c return } - setRequestHeaders(c, req) + setRequestHeaders(c, req, matcher) AuthPassThrough(c, cfg, req) resp, err = client.Do(req) diff --git a/proxy/gitreq.go b/proxy/gitreq.go index b8ba66d..2a4b57b 100644 --- a/proxy/gitreq.go +++ b/proxy/gitreq.go @@ -35,7 +35,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) return } - setRequestHeaders(c, req) + setRequestHeaders(c, req, "clone") //removeWSHeader(req) AuthPassThrough(c, cfg, req) @@ -50,7 +50,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) return } - setRequestHeaders(c, req) + setRequestHeaders(c, req, "clone") //removeWSHeader(req) AuthPassThrough(c, cfg, req) diff --git a/proxy/reqheader.go b/proxy/reqheader.go index 99bef3a..24baaae 100644 --- a/proxy/reqheader.go +++ b/proxy/reqheader.go @@ -6,12 +6,29 @@ import ( "github.com/cloudwego/hertz/pkg/app" ) -func setRequestHeaders(c *app.RequestContext, req *http.Request) { - c.Request.Header.VisitAll(func(key, value []byte) { - headerKey := string(key) - headerValue := string(value) - if _, shouldRemove := reqHeadersToRemove[headerKey]; !shouldRemove { - req.Header.Set(headerKey, headerValue) +// 预定义headers +var ( + defaultHeaders = map[string]string{ + "Accept": "*/*", + "Accept-Encoding": "gzip", + "Transfer-Encoding": "chunked", + "User-Agent": "GHProxy/1.0", + } +) + +func setRequestHeaders(c *app.RequestContext, req *http.Request, matcher string) { + if matcher == "raw" { + // 使用预定义Header + for key, value := range defaultHeaders { + req.Header.Set(key, value) } - }) + } else { + c.Request.Header.VisitAll(func(key, value []byte) { + headerKey := string(key) + headerValue := string(value) + if _, shouldRemove := reqHeadersToRemove[headerKey]; !shouldRemove { + req.Header.Set(headerKey, headerValue) + } + }) + } }