mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 16:21:11 +08:00
add docs
This commit is contained in:
parent
b02aaeba8a
commit
ff412f94ec
14 changed files with 386 additions and 104 deletions
|
|
@ -10,7 +10,6 @@ import (
|
|||
"strconv"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
//hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||
)
|
||||
|
||||
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
||||
|
|
@ -31,7 +30,6 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
|||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
}
|
||||
//defer headResp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
if err := Body.Close(); err != nil {
|
||||
logError("Failed to close response body: %v", err)
|
||||
|
|
@ -68,7 +66,6 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
|||
HandleError(c, fmt.Sprintf("Failed to send request: %v", err))
|
||||
return
|
||||
}
|
||||
//defer resp.Body.Close()
|
||||
|
||||
// 错误处理(404)
|
||||
if resp.StatusCode == 404 {
|
||||
|
|
@ -115,7 +112,6 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
|||
}
|
||||
|
||||
c.Status(resp.StatusCode)
|
||||
//c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||
|
||||
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
||||
// 判断body是不是gzip
|
||||
|
|
@ -127,7 +123,6 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
|||
logInfo("Is Shell: %s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol())
|
||||
c.Header("Content-Length", "")
|
||||
|
||||
//err := ProcessLinksAndWriteChunked(resp.Body, compress, string(c.Request.Host()), cfg, c)
|
||||
reader, _, err := processLinks(resp.Body, compress, string(c.Request.Host()), cfg)
|
||||
c.SetBodyStream(reader, -1)
|
||||
|
||||
|
|
@ -136,18 +131,6 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
|
|||
return
|
||||
}
|
||||
} else {
|
||||
//err = hwriter.Writer(resp.Body, c)
|
||||
//writer := c.Response.BodyWriter()
|
||||
|
||||
/*
|
||||
_, err := copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
||||
if err != nil {
|
||||
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
||||
return
|
||||
} else {
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
*/
|
||||
c.SetBodyStream(resp.Body, -1)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
|
|
@ -19,6 +18,6 @@ var (
|
|||
)
|
||||
|
||||
func HandleError(c *app.RequestContext, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
c.JSON(http.StatusInternalServerError, map[string]string{"error": message})
|
||||
logError(message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,6 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
|||
}
|
||||
setRequestHeaders(c, req)
|
||||
removeWSHeader(req)
|
||||
//reWriteEncodeHeader(req)
|
||||
AuthPassThrough(c, cfg, req)
|
||||
|
||||
resp, err = gitclient.Do(req)
|
||||
|
|
@ -60,7 +59,6 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
|||
}
|
||||
setRequestHeaders(c, req)
|
||||
removeWSHeader(req)
|
||||
//reWriteEncodeHeader(req)
|
||||
AuthPassThrough(c, cfg, req)
|
||||
|
||||
resp, err = client.Do(req)
|
||||
|
|
@ -69,14 +67,6 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
|||
return
|
||||
}
|
||||
}
|
||||
/*
|
||||
//defer resp.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
if err := Body.Close(); err != nil {
|
||||
logError("Failed to close response body: %v", err)
|
||||
}
|
||||
}(resp.Body)
|
||||
*/
|
||||
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
if contentLength != "" {
|
||||
|
|
@ -123,18 +113,6 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
|
|||
c.Response.Header.Set("Pragma", "no-cache")
|
||||
c.Response.Header.Set("Expires", "0")
|
||||
}
|
||||
|
||||
c.SetBodyStream(resp.Body, -1)
|
||||
//err = hwriter.Writer(resp.Body, c)
|
||||
/*
|
||||
_, err = copyb.Copy(c.Response.BodyWriter(), resp.Body)
|
||||
|
||||
if err != nil {
|
||||
logError("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), err)
|
||||
return
|
||||
} else {
|
||||
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,33 +17,3 @@ func removeWSHeader(req *http.Request) {
|
|||
req.Header.Del("Upgrade")
|
||||
req.Header.Del("Connection")
|
||||
}
|
||||
|
||||
/*
|
||||
func reWriteEncodeHeader(req *http.Request) {
|
||||
|
||||
if isGzipAccepted(req.Header) {
|
||||
req.Header.Set("Content-Encoding", "gzip")
|
||||
req.Header.Set("Accept-Encoding", "gzip")
|
||||
} else {
|
||||
req.Header.Del("Content-Encoding")
|
||||
req.Header.Del("Accept-Encoding")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// isGzipAccepted 检查 Accept-Encoding 头部中是否包含 gzip
|
||||
func isGzipAccepted(header http.Header) bool {
|
||||
// 获取 Accept-Encoding 的值
|
||||
encodings := header["Accept-Encoding"]
|
||||
for _, encoding := range encodings {
|
||||
// 将 encoding 字符串拆分为多个编码
|
||||
for _, enc := range strings.Split(encoding, ",") {
|
||||
// 去除空格并检查是否为 gzip
|
||||
if strings.TrimSpace(enc) == "gzip" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue