mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 00:01:10 +08:00
25w20a
This commit is contained in:
parent
ac7e1e43b5
commit
a92bbb7fb6
22 changed files with 685 additions and 316 deletions
|
|
@ -4,20 +4,21 @@ import (
|
|||
"ghproxy/config"
|
||||
"net/http"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func AuthPassThrough(c *gin.Context, cfg *config.Config, req *http.Request) {
|
||||
func AuthPassThrough(c *app.RequestContext, cfg *config.Config, req *http.Request) {
|
||||
if cfg.Auth.PassThrough {
|
||||
token := c.Query("token")
|
||||
if token != "" {
|
||||
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, token)
|
||||
logDebug("%s %s %s %s %s Auth-PassThrough: token %s", c.ClientIP(), c.Request.Method, string(c.Path()), c.GetHeader, c.Request.Header.GetProtocol(), token)
|
||||
switch cfg.Auth.AuthMethod {
|
||||
case "parameters":
|
||||
if !cfg.Auth.Enabled {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
} else {
|
||||
logWarning("%s %s %s %s %s Auth-Error: Conflict Auth Method", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s Auth-Error: Conflict Auth Method", c.ClientIP(), c.Request.Method, string(c.Path()), c.GetHeader, c.Request.Header.GetProtocol())
|
||||
// 500 Internal Server Error
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Conflict Auth Method"})
|
||||
return
|
||||
|
|
@ -27,7 +28,7 @@ func AuthPassThrough(c *gin.Context, cfg *config.Config, req *http.Request) {
|
|||
req.Header.Set("Authorization", "token "+token)
|
||||
}
|
||||
default:
|
||||
logWarning("%s %s %s %s %s Invalid Auth Method / Auth Method is not be set", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s Invalid Auth Method / Auth Method is not be set", c.ClientIP(), c.Request.Method, string(c.Path()), c.GetHeader, c.Request.Header.GetProtocol())
|
||||
// 500 Internal Server Error
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid Auth Method / Auth Method is not be set"})
|
||||
return
|
||||
|
|
|
|||
|
|
@ -2,17 +2,21 @@ package proxy
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
//hclient "github.com/cloudwego/hertz/pkg/app/client"
|
||||
//"github.com/cloudwego/hertz/pkg/protocol"
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/hwriter"
|
||||
hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||
)
|
||||
|
||||
func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher string) {
|
||||
func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, matcher string) {
|
||||
method := c.Request.Method
|
||||
|
||||
// 发送HEAD请求, 预获取Content-Length
|
||||
|
|
@ -44,21 +48,17 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher s
|
|||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := headResp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
c.Redirect(http.StatusMovedPermanently, []byte(finalURL))
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
body, err := readRequestBody(c)
|
||||
if err != nil {
|
||||
HandleError(c, err.Error())
|
||||
return
|
||||
}
|
||||
body := c.Request.Body()
|
||||
|
||||
bodyReader := bytes.NewBuffer(body)
|
||||
|
||||
req, err := client.NewRequest(method, u, bodyReader)
|
||||
req, err := client.NewRequest(string(method()), u, bodyReader)
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
|
||||
return
|
||||
|
|
@ -86,8 +86,8 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher s
|
|||
size, err := strconv.Atoi(contentLength)
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
c.Redirect(http.StatusMovedPermanently, []byte(finalURL))
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -108,17 +108,6 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher s
|
|||
resp.Header.Del(header)
|
||||
}
|
||||
|
||||
//c.Header("Accept-Encoding", "gzip")
|
||||
//c.Header("Content-Encoding", "gzip")
|
||||
|
||||
/*
|
||||
if cfg.CORS.Enabled {
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
} else {
|
||||
c.Header("Access-Control-Allow-Origin", "")
|
||||
}
|
||||
*/
|
||||
|
||||
switch cfg.Server.Cors {
|
||||
case "*":
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
|
|
@ -131,6 +120,7 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher s
|
|||
}
|
||||
|
||||
c.Status(resp.StatusCode)
|
||||
c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||
|
||||
if MatcherShell(u) && matchString(matcher, matchedMatchers) && cfg.Shell.Editor {
|
||||
// 判断body是不是gzip
|
||||
|
|
@ -139,23 +129,36 @@ func ChunkedProxyRequest(c *gin.Context, u string, cfg *config.Config, matcher s
|
|||
compress = "gzip"
|
||||
}
|
||||
|
||||
logInfo("Is Shell: %s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
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 = processLinks(resp.Body, c.Writer, compress, c.Request.Host, cfg)
|
||||
|
||||
ProcessLinksAndWriteChunked(resp.Body, compress, string(c.Request.Host()), cfg, c)
|
||||
|
||||
/*
|
||||
presp, err := processLinks(resp.Body, compress, string(c.Request.Host()), cfg)
|
||||
if err != nil {
|
||||
logError("Failed to process links: %v", err)
|
||||
WriteChunkedBody(resp.Body, c)
|
||||
return
|
||||
}
|
||||
defer presp.Close()
|
||||
WriteChunkedBody(presp, c)
|
||||
*/
|
||||
|
||||
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.Proto, err)
|
||||
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.Writer.Flush() // 确保刷入
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
} else {
|
||||
//_, err = io.CopyBuffer(c.Writer, resp.Body, nil)
|
||||
_, err = copyb.CopyBuffer(c.Writer, resp.Body, nil)
|
||||
//WriteChunkedBody(resp.Body, c)
|
||||
err = hwriter.Writer(resp.Body, c)
|
||||
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.Proto, err)
|
||||
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.Writer.Flush() // 确保刷入
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package proxy
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
|
|
@ -10,13 +11,12 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/copyb"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/hwriter"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode string) {
|
||||
method := c.Request.Method
|
||||
logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, mode string, runMode string) {
|
||||
method := string(c.Request.Method())
|
||||
|
||||
logDump("Url Before FMT:%s", u)
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
|
|
@ -35,11 +35,7 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||
err error
|
||||
)
|
||||
|
||||
body, err := readRequestBody(c)
|
||||
if err != nil {
|
||||
HandleError(c, err.Error())
|
||||
return
|
||||
}
|
||||
body := c.Request.Body()
|
||||
|
||||
bodyReader := bytes.NewBuffer(body)
|
||||
// 创建请求
|
||||
|
|
@ -85,9 +81,9 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||
size, err := strconv.Atoi(contentLength)
|
||||
sizelimit := cfg.Server.SizeLimit * 1024 * 1024
|
||||
if err == nil && size > sizelimit {
|
||||
finalURL := resp.Request.URL.String()
|
||||
finalURL := []byte(resp.Request.URL.String())
|
||||
c.Redirect(http.StatusMovedPermanently, finalURL)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Request.URL.String(), c.Request.Header.Get("User-Agent"), c.Request.Proto, finalURL, size)
|
||||
logWarning("%s %s %s %s %s Final-URL: %s Size-Limit-Exceeded: %d", c.ClientIP(), c.Request.Method, c.Path(), c.Request.Header.Get("User-Agent"), c.Request.Header.GetProtocol(), finalURL, size)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
@ -127,21 +123,22 @@ func GitReq(c *gin.Context, u string, cfg *config.Config, mode string, runMode s
|
|||
|
||||
_, err = io.CopyBuffer(c.Writer, resp.Body, buffer)
|
||||
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.Proto, err)
|
||||
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.Writer.Flush() // 确保刷入
|
||||
}
|
||||
*/
|
||||
|
||||
_, err = copyb.CopyBuffer(c.Writer, resp.Body, nil)
|
||||
//_, err = copyb.CopyBuffer(c, resp.Body, nil)
|
||||
err = hwriter.Writer(resp.Body, c)
|
||||
|
||||
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.Proto, err)
|
||||
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.Writer.Flush() // 确保刷入
|
||||
c.Flush() // 确保刷入
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"ghproxy/auth"
|
||||
|
|
@ -10,23 +11,14 @@ import (
|
|||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var re = regexp.MustCompile(`^(http:|https:)?/?/?(.*)`) // 匹配http://或https://开头的路径
|
||||
/*
|
||||
var exps = []*regexp.Regexp{
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:releases|archive)/.*`), // 匹配 GitHub Releases 或 Archive 链接
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:blob|raw)/.*`), // 匹配 GitHub Blob 或 Raw 链接
|
||||
regexp.MustCompile(`^(?:https?://)?github\.com/([^/]+)/([^/]+)/(?:info|git-).*`), // 匹配 GitHub Info 或 Git 相关链接 (例如 .gitattributes, .gitignore)
|
||||
regexp.MustCompile(`^(?:https?://)?raw\.github(?:usercontent|)\.com/([^/]+)/([^/]+)/.+?/.+`), // 匹配 raw.githubusercontent.com 链接
|
||||
regexp.MustCompile(`^(?:https?://)?gist\.github(?:usercontent|)\.com/([^/]+)/.+?/.+`), // 匹配 gist.githubusercontent.com 链接
|
||||
regexp.MustCompile(`^(?:https?://)?api\.github\.com/repos/([^/]+)/([^/]+)/.*`), // 匹配 api.github.com/repos 链接 (GitHub API)
|
||||
}
|
||||
*/
|
||||
|
||||
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *rate.IPRateLimiter, runMode string) app.HandlerFunc {
|
||||
return func(ctx context.Context, c *app.RequestContext) {
|
||||
|
||||
// 限制访问频率
|
||||
if cfg.RateLimit.Enabled {
|
||||
|
|
@ -45,19 +37,19 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||
|
||||
if !allowed {
|
||||
c.JSON(http.StatusTooManyRequests, gin.H{"error": "Too Many Requests"})
|
||||
logWarning("%s %s %s %s %s 429-TooManyRequests", c.ClientIP(), c.Request.Method, c.Request.URL.RequestURI(), c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s 429-TooManyRequests", c.ClientIP(), c.Request.Method, c.Request.RequestURI(), c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
//rawPath := strings.TrimPrefix(c.Request.URL.Path, "/") // 去掉前缀/
|
||||
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") // 去掉前缀/
|
||||
matches := re.FindStringSubmatch(rawPath) // 匹配路径
|
||||
rawPath := strings.TrimPrefix(string(c.Request.RequestURI()), "/") // 去掉前缀/
|
||||
matches := re.FindStringSubmatch(rawPath) // 匹配路径
|
||||
logInfo("Matches: %v", matches)
|
||||
|
||||
// 匹配路径错误处理
|
||||
if len(matches) < 3 {
|
||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
logWarning(errMsg)
|
||||
c.String(http.StatusForbidden, "Invalid URL Format. Path: %s", rawPath)
|
||||
return
|
||||
|
|
@ -81,16 +73,16 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||
}
|
||||
username := user
|
||||
|
||||
logInfo("%s %s %s %s %s Matched-Username: %s, Matched-Repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, username, repo)
|
||||
// dump log 记录详细信息 c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, full Header
|
||||
logDump("%s %s %s %s %s %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, c.Request.Header)
|
||||
logInfo("%s %s %s %s %s Matched-Username: %s, Matched-Repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), username, repo)
|
||||
// dump log 记录详细信息 c.ClientIP(), c.Request.Method, rawPath,c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), full Header
|
||||
logDump("%s %s %s %s %s %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), c.Request.Header)
|
||||
repouser := fmt.Sprintf("%s/%s", username, repo)
|
||||
|
||||
// 白名单检查
|
||||
if cfg.Whitelist.Enabled {
|
||||
whitelist := auth.CheckWhitelist(username, repo)
|
||||
if !whitelist {
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Whitelist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, repouser)
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Whitelist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), repouser)
|
||||
errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", repouser)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logWarning(logErrMsg)
|
||||
|
|
@ -102,7 +94,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||
if cfg.Blacklist.Enabled {
|
||||
blacklist := auth.CheckBlacklist(username, repo)
|
||||
if blacklist {
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Blacklist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, repouser)
|
||||
logErrMsg := fmt.Sprintf("%s %s %s %s %s Blacklist Blocked repo: %s", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), repouser)
|
||||
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", repouser)
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
|
||||
logWarning(logErrMsg)
|
||||
|
|
@ -114,7 +106,7 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||
matches = CheckURL(rawPath, c)
|
||||
if matches == nil {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
logWarning("%s %s %s %s %s 404-NOMATCH", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
logWarning("%s %s %s %s %s 404-NOMATCH", c.ClientIP(), c.Request.Method, rawPath,c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
|
@ -128,22 +120,22 @@ func NoRouteHandler(cfg *config.Config, limiter *rate.RateLimiter, iplimiter *ra
|
|||
|
||||
// 鉴权
|
||||
var authcheck bool
|
||||
authcheck, err = auth.AuthHandler(c, cfg)
|
||||
authcheck, err = auth.AuthHandler(ctx, c, cfg)
|
||||
if !authcheck {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
|
||||
logWarning("%s %s %s %s %s Auth-Error: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
|
||||
logWarning("%s %s %s %s %s Auth-Error: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), err)
|
||||
return
|
||||
}
|
||||
|
||||
// IP METHOD URL USERAGENT PROTO MATCHES
|
||||
logDebug("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.Get("User-Agent"), c.Request.Proto, matches)
|
||||
logDebug("%s %s %s %s %s Matches: %v", c.ClientIP(), c.Request.Method, rawPath, c.Request.Header.UserAgent(), c.Request.Header.GetProtocol(), matches)
|
||||
|
||||
switch matcher {
|
||||
case "releases", "blob", "raw", "gist", "api":
|
||||
ChunkedProxyRequest(c, rawPath, cfg, matcher)
|
||||
ChunkedProxyRequest(ctx, c, rawPath, cfg, matcher)
|
||||
case "clone":
|
||||
//ProxyRequest(c, rawPath, cfg, "git", runMode)
|
||||
GitReq(c, rawPath, cfg, "git", runMode)
|
||||
GitReq(ctx, c, rawPath, cfg, "git", runMode)
|
||||
default:
|
||||
c.String(http.StatusForbidden, "Invalid input.")
|
||||
fmt.Println("Invalid input.")
|
||||
|
|
@ -159,7 +151,7 @@ func CheckURL(u string, c *gin.Context) []string {
|
|||
return matches[1:]
|
||||
}
|
||||
}
|
||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
|
||||
errMsg := fmt.Sprintf("%s %s %s %s %s Invalid URL", c.ClientIP(), c.Request.Method, u,c.Request.Header.UserAgent(), c.Request.Header.GetProtocol())
|
||||
logError(errMsg)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,18 @@ package proxy
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
hresp "github.com/cloudwego/hertz/pkg/protocol/http1/resp"
|
||||
"github.com/valyala/bytebufferpool"
|
||||
)
|
||||
|
||||
// 定义错误类型, error承载描述, 便于处理
|
||||
|
|
@ -205,15 +211,136 @@ func matchString(target string, stringsToMatch []string) bool {
|
|||
return exists
|
||||
}
|
||||
|
||||
// processLinks 处理链接并将结果写入输出流
|
||||
func processLinks(input io.Reader, output io.Writer, compress string, host string, cfg *config.Config) (written int64, err error) {
|
||||
// processLinks 处理链接并返回一个 io.ReadCloser
|
||||
func processLinks(input io.Reader, compress string, host string, cfg *config.Config) (io.ReadCloser, error) {
|
||||
var reader *bufio.Reader
|
||||
|
||||
if compress == "gzip" {
|
||||
// 解压gzip
|
||||
// 解压 gzip
|
||||
gzipReader, err := gzip.NewReader(input)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("gzip解压错误: %v", err)
|
||||
return nil, fmt.Errorf("gzip 解压错误: %w", err)
|
||||
}
|
||||
reader = bufio.NewReader(gzipReader)
|
||||
} else {
|
||||
reader = bufio.NewReader(input)
|
||||
}
|
||||
|
||||
// 创建一个缓冲区用于存储输出
|
||||
var outputBuffer io.Writer
|
||||
var gzipWriter *gzip.Writer
|
||||
var output io.ReadCloser
|
||||
var buf bytes.Buffer
|
||||
|
||||
if compress == "gzip" {
|
||||
// 创建一个管道来连接 gzipWriter 和 output
|
||||
pipeReader, pipeWriter := io.Pipe() // 创建一个管道
|
||||
output = pipeReader // 将管道的读取端作为输出
|
||||
outputBuffer = pipeWriter // 将管道的写入端作为 outputBuffer
|
||||
gzipWriter = gzip.NewWriter(outputBuffer)
|
||||
go func() {
|
||||
defer pipeWriter.Close() // 确保在 goroutine 结束时关闭 pipeWriter
|
||||
writer := bufio.NewWriter(gzipWriter)
|
||||
defer func() {
|
||||
if err := writer.Flush(); err != nil {
|
||||
logError("gzip writer 刷新失败: %v", err)
|
||||
}
|
||||
if err := gzipWriter.Close(); err != nil {
|
||||
logError("gzipWriter 关闭失败: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||
return modifyURL(originalURL, host, cfg)
|
||||
})
|
||||
if _, err := writer.WriteString(modifiedLine + "\n"); err != nil {
|
||||
logError("写入 gzipWriter 失败: %v", err)
|
||||
return // 在发生错误时退出 goroutine
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
logError("读取输入错误: %v", err)
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
outputBuffer = &buf
|
||||
writer := bufio.NewWriter(outputBuffer)
|
||||
defer func() {
|
||||
if err := writer.Flush(); err != nil {
|
||||
logError("writer 刷新失败: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||
return modifyURL(originalURL, host, cfg)
|
||||
})
|
||||
if _, err := writer.WriteString(modifiedLine + "\n"); err != nil {
|
||||
return nil, fmt.Errorf("写入文件错误: %w", err) // 传递错误
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("读取行错误: %w", err) // 传递错误
|
||||
}
|
||||
output = io.NopCloser(&buf)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func WriteChunkedBody(resp io.ReadCloser, c *app.RequestContext) {
|
||||
defer resp.Close()
|
||||
|
||||
c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||
|
||||
bufWrapper := bytebufferpool.Get()
|
||||
buf := bufWrapper.B
|
||||
size := 32768 // 32KB
|
||||
buf = buf[:cap(buf)]
|
||||
if len(buf) < size {
|
||||
buf = append(buf, make([]byte, size-len(buf))...)
|
||||
}
|
||||
buf = buf[:size] // 将缓冲区限制为 'size'
|
||||
defer bytebufferpool.Put(bufWrapper)
|
||||
|
||||
for {
|
||||
n, err := resp.Read(buf)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break // 读取到文件末尾
|
||||
}
|
||||
fmt.Println("读取错误:", err)
|
||||
c.String(http.StatusInternalServerError, "读取错误")
|
||||
return
|
||||
}
|
||||
|
||||
_, err = c.Write(buf[:n]) // 写入 chunk
|
||||
if err != nil {
|
||||
fmt.Println("写入 chunk 错误:", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Flush() // 刷新 chunk 到客户端
|
||||
}
|
||||
}
|
||||
|
||||
// processLinksAndWriteChunked 处理链接并将结果以 chunked 方式写入响应
|
||||
func ProcessLinksAndWriteChunked(input io.Reader, compress string, host string, cfg *config.Config, c *app.RequestContext) {
|
||||
var reader *bufio.Reader
|
||||
|
||||
if compress == "gzip" {
|
||||
// 解压 gzip
|
||||
gzipReader, err := gzip.NewReader(input)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("gzip 解压错误: %v", err))
|
||||
return
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
reader = bufio.NewReader(gzipReader)
|
||||
|
|
@ -221,36 +348,108 @@ func processLinks(input io.Reader, output io.Writer, compress string, host strin
|
|||
reader = bufio.NewReader(input)
|
||||
}
|
||||
|
||||
var writer *bufio.Writer
|
||||
// 获取 chunked body writer
|
||||
chunkedWriter := hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter())
|
||||
|
||||
var writer io.Writer = chunkedWriter
|
||||
var gzipWriter *gzip.Writer
|
||||
|
||||
// 根据是否gzip确定 writer 的创建
|
||||
if compress == "gzip" {
|
||||
gzipWriter = gzip.NewWriter(output)
|
||||
writer = bufio.NewWriterSize(gzipWriter, 4096) //设置缓冲区大小
|
||||
} else {
|
||||
writer = bufio.NewWriterSize(output, 4096)
|
||||
gzipWriter = gzip.NewWriter(writer)
|
||||
writer = gzipWriter
|
||||
defer func() {
|
||||
if err := gzipWriter.Close(); err != nil {
|
||||
logError("gzipWriter close failed: %v", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
//确保writer关闭
|
||||
defer func() {
|
||||
var closeErr error // 局部变量,用于保存defer中可能发生的错误
|
||||
bufWrapper := bytebufferpool.Get()
|
||||
buf := bufWrapper.B
|
||||
size := 32768 // 32KB
|
||||
buf = buf[:cap(buf)]
|
||||
if len(buf) < size {
|
||||
buf = append(buf, make([]byte, size-len(buf))...)
|
||||
}
|
||||
buf = buf[:size] // 将缓冲区限制为 'size'
|
||||
defer bytebufferpool.Put(bufWrapper)
|
||||
|
||||
if gzipWriter != nil {
|
||||
if closeErr = gzipWriter.Close(); closeErr != nil {
|
||||
logError("gzipWriter close failed %v", closeErr)
|
||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
urlPattern := regexp.MustCompile(`https?://[^\s'"]+`)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
modifiedLine := urlPattern.ReplaceAllStringFunc(line, func(originalURL string) string {
|
||||
return modifyURL(originalURL, host, cfg)
|
||||
})
|
||||
modifiedLineWithNewline := modifiedLine + "\n"
|
||||
|
||||
_, err := writer.Write([]byte(modifiedLineWithNewline))
|
||||
if err != nil {
|
||||
logError("写入 chunk 错误: %v", err)
|
||||
return // 发生错误时退出
|
||||
}
|
||||
|
||||
if compress != "gzip" {
|
||||
if fErr := chunkedWriter.Flush(); fErr != nil {
|
||||
logError("chunkedWriter flush failed: %v", fErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
logError("读取输入错误: %v", err)
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("读取输入错误: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
// 对于 gzip,chunkedWriter 的关闭会触发最后的 chunk
|
||||
if compress != "gzip" {
|
||||
if fErr := chunkedWriter.Flush(); fErr != nil {
|
||||
logError("final chunkedWriter flush failed: %v", fErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ProcessAndWriteChunkedBody(input io.Reader, compress string, host string, cfg *config.Config, c *app.RequestContext) error {
|
||||
var reader *bufio.Reader
|
||||
|
||||
if compress == "gzip" {
|
||||
// 解压gzip
|
||||
gzipReader, err := gzip.NewReader(input)
|
||||
if err != nil {
|
||||
return fmt.Errorf("gzip解压错误: %v", err)
|
||||
}
|
||||
defer gzipReader.Close()
|
||||
reader = bufio.NewReader(gzipReader)
|
||||
} else {
|
||||
reader = bufio.NewReader(input)
|
||||
}
|
||||
|
||||
// 创建一个缓冲区用于存储输出
|
||||
var outputBuffer io.Writer
|
||||
var gzipWriter *gzip.Writer
|
||||
var buf bytes.Buffer
|
||||
|
||||
if compress == "gzip" {
|
||||
// 创建一个缓冲区
|
||||
outputBuffer = &buf
|
||||
gzipWriter = gzip.NewWriter(outputBuffer)
|
||||
defer func() {
|
||||
if gzipWriter != nil {
|
||||
if closeErr := gzipWriter.Close(); closeErr != nil {
|
||||
logError("gzipWriter close failed %v", closeErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
} else {
|
||||
outputBuffer = &buf
|
||||
}
|
||||
|
||||
writer := bufio.NewWriter(outputBuffer)
|
||||
defer func() {
|
||||
if flushErr := writer.Flush(); flushErr != nil {
|
||||
logError("writer flush failed %v", flushErr)
|
||||
// 如果已经存在错误,则保留。否则,记录此错误。
|
||||
if err == nil {
|
||||
err = flushErr
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -262,7 +461,7 @@ func processLinks(input io.Reader, output io.Writer, compress string, host strin
|
|||
if err == io.EOF {
|
||||
break // 文件结束
|
||||
}
|
||||
return written, fmt.Errorf("读取行错误: %v", err) // 传递错误
|
||||
return fmt.Errorf("读取行错误: %v", err) // 传递错误
|
||||
}
|
||||
|
||||
// 替换所有匹配的 URL
|
||||
|
|
@ -270,17 +469,56 @@ func processLinks(input io.Reader, output io.Writer, compress string, host strin
|
|||
return modifyURL(originalURL, host, cfg)
|
||||
})
|
||||
|
||||
n, werr := writer.WriteString(modifiedLine)
|
||||
written += int64(n) // 更新写入的字节数
|
||||
_, werr := writer.WriteString(modifiedLine)
|
||||
if werr != nil {
|
||||
return written, fmt.Errorf("写入文件错误: %v", werr) // 传递错误
|
||||
return fmt.Errorf("写入文件错误: %v", werr) // 传递错误
|
||||
}
|
||||
}
|
||||
|
||||
// 在返回之前,再刷新一次
|
||||
if fErr := writer.Flush(); fErr != nil {
|
||||
return written, fErr
|
||||
return fErr
|
||||
}
|
||||
|
||||
return written, nil
|
||||
if compress == "gzip" {
|
||||
if err := gzipWriter.Close(); err != nil {
|
||||
return fmt.Errorf("gzipWriter close failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 将处理后的内容以分块的方式写入响应
|
||||
c.Response.HijackWriter(hresp.NewChunkedBodyWriter(&c.Response, c.GetWriter()))
|
||||
|
||||
bufWrapper := bytebufferpool.Get()
|
||||
bbuf := bufWrapper.B
|
||||
size := 32768 // 32KB
|
||||
if cap(bbuf) < size {
|
||||
bbuf = make([]byte, size)
|
||||
} else {
|
||||
bbuf = bbuf[:size]
|
||||
}
|
||||
defer bytebufferpool.Put(bufWrapper)
|
||||
|
||||
// 将缓冲区内容写入响应
|
||||
for {
|
||||
n, err := buf.Read(bbuf)
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
fmt.Println("读取错误:", err)
|
||||
c.String(http.StatusInternalServerError, "读取错误")
|
||||
return err
|
||||
}
|
||||
break // 读取到文件末尾
|
||||
}
|
||||
|
||||
_, err = c.Write(bbuf[:n]) // 写入 chunk
|
||||
if err != nil {
|
||||
fmt.Println("写入 chunk 错误:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.Flush() // 刷新 chunk 到客户端
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@ package proxy
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/WJQSERVER-STUDIO/go-utils/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
// 日志模块
|
||||
|
|
@ -19,18 +18,7 @@ var (
|
|||
logError = logger.LogError
|
||||
)
|
||||
|
||||
// 读取请求体
|
||||
func readRequestBody(c *gin.Context) ([]byte, error) {
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
logError("failed to read request body: %v", err)
|
||||
return nil, fmt.Errorf("failed to read request body: %v", err)
|
||||
}
|
||||
defer c.Request.Body.Close()
|
||||
return body, nil
|
||||
}
|
||||
|
||||
func HandleError(c *gin.Context, message string) {
|
||||
func HandleError(c *app.RequestContext, message string) {
|
||||
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
|
||||
logError(message)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,14 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
)
|
||||
|
||||
// 设置请求头
|
||||
func setRequestHeaders(c *gin.Context, req *http.Request) {
|
||||
for key, values := range c.Request.Header {
|
||||
for _, value := range values {
|
||||
req.Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
func setRequestHeaders(c *app.RequestContext, req *http.Request) {
|
||||
c.Request.Header.VisitAll(func(key, value []byte) {
|
||||
req.Header.Set(string(key), string(value))
|
||||
})
|
||||
}
|
||||
|
||||
func removeWSHeader(req *http.Request) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue