mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
use touka uni httpc
This commit is contained in:
parent
781e175721
commit
904a800eea
11 changed files with 51 additions and 128 deletions
|
|
@ -119,6 +119,7 @@ func GhcrRequest(ctx context.Context, c *touka.Context, u string, image *imageIn
|
|||
}()
|
||||
|
||||
method = c.Request.Method
|
||||
ghcrclient := c.GetHTTPC()
|
||||
|
||||
rb := ghcrclient.NewRequestBuilder(method, u)
|
||||
rb.NoDefaultHeaders()
|
||||
|
|
@ -267,6 +268,7 @@ func ChallengeReq(target string, image *imageInfo, ctx context.Context, c *touka
|
|||
var resp401 *http.Response
|
||||
var req401 *http.Request
|
||||
var err error
|
||||
ghcrclient := c.GetHTTPC()
|
||||
|
||||
rb401 := ghcrclient.NewRequestBuilder("GET", "https://"+target+"/v2/")
|
||||
rb401.NoDefaultHeaders()
|
||||
|
|
|
|||
|
|
@ -17,23 +17,12 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
|
|||
resp *http.Response
|
||||
)
|
||||
|
||||
/*
|
||||
fullBody, err := c.GetReqBodyFull()
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
reqBodyReader := bytes.NewBuffer(fullBody)
|
||||
*/
|
||||
|
||||
reqBodyReader, err := c.GetReqBodyBuffer()
|
||||
if err != nil {
|
||||
HandleError(c, fmt.Sprintf("Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
//bodyReader := c.Request.BodyStream() // 不可替换为此实现
|
||||
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
userPath, repoPath, remainingPath, queryParams, err := extractParts(u)
|
||||
if err != nil {
|
||||
|
|
@ -103,14 +92,6 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
for key, values := range resp.Header {
|
||||
for _, value := range values {
|
||||
c.Response.Header.Add(key, value)
|
||||
}
|
||||
}
|
||||
*/
|
||||
//copyHeader( resp.Header)
|
||||
c.SetHeaders(resp.Header)
|
||||
|
||||
headersToRemove := map[string]struct{}{
|
||||
|
|
@ -143,10 +124,6 @@ func GitReq(ctx context.Context, c *touka.Context, u string, cfg *config.Config,
|
|||
|
||||
bodyReader := resp.Body
|
||||
|
||||
// 读取body内容
|
||||
//bodyContent, _ := io.ReadAll(bodyReader)
|
||||
// c.Infof("%s", bodyContent)
|
||||
|
||||
if cfg.RateLimit.BandwidthLimit.Enabled {
|
||||
bodyReader = limitreader.NewRateLimitedReader(bodyReader, bandwidthLimit, int(bandwidthBurst), ctx)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,10 +15,6 @@ func NoRouteHandler(cfg *config.Config) touka.HandlerFunc {
|
|||
return func(c *touka.Context) {
|
||||
var ctx = c.Request.Context()
|
||||
var shoudBreak bool
|
||||
// shoudBreak = rateCheck(cfg, c, limiter, iplimiter)
|
||||
// if shoudBreak {
|
||||
// return
|
||||
// }
|
||||
|
||||
var (
|
||||
rawPath string
|
||||
|
|
|
|||
122
proxy/httpc.go
122
proxy/httpc.go
|
|
@ -1,7 +1,6 @@
|
|||
package proxy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ghproxy/config"
|
||||
"net/http"
|
||||
"time"
|
||||
|
|
@ -12,42 +11,40 @@ import (
|
|||
var BufferSize int = 32 * 1024 // 32KB
|
||||
|
||||
var (
|
||||
tr *http.Transport
|
||||
gittr *http.Transport
|
||||
client *httpc.Client
|
||||
gitclient *httpc.Client
|
||||
ghcrtr *http.Transport
|
||||
ghcrclient *httpc.Client
|
||||
tr *http.Transport
|
||||
gittr *http.Transport
|
||||
client *httpc.Client
|
||||
gitclient *httpc.Client
|
||||
)
|
||||
|
||||
func InitReq(cfg *config.Config) error {
|
||||
initHTTPClient(cfg)
|
||||
func InitReq(cfg *config.Config) (*httpc.Client, error) {
|
||||
client := initHTTPClient(cfg)
|
||||
if cfg.GitClone.Mode == "cache" {
|
||||
initGitHTTPClient(cfg)
|
||||
}
|
||||
initGhcrHTTPClient(cfg)
|
||||
err := SetGlobalRateLimit(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
return client, nil
|
||||
|
||||
}
|
||||
|
||||
func initHTTPClient(cfg *config.Config) {
|
||||
func initHTTPClient(cfg *config.Config) *httpc.Client {
|
||||
var proTolcols = new(http.Protocols)
|
||||
proTolcols.SetHTTP1(true)
|
||||
proTolcols.SetHTTP2(true)
|
||||
proTolcols.SetUnencryptedHTTP2(true)
|
||||
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
|
||||
|
||||
switch cfg.Httpc.Mode {
|
||||
case "auto", "":
|
||||
tr = &http.Transport{
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
Protocols: proTolcols,
|
||||
}
|
||||
} else if cfg.Httpc.Mode == "advanced" {
|
||||
case "advanced":
|
||||
tr = &http.Transport{
|
||||
MaxIdleConns: cfg.Httpc.MaxIdleConns,
|
||||
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
|
||||
|
|
@ -56,9 +53,10 @@ func initHTTPClient(cfg *config.Config) {
|
|||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
Protocols: proTolcols,
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
panic("unknown httpc mode: " + cfg.Httpc.Mode)
|
||||
}
|
||||
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, tr)
|
||||
}
|
||||
|
|
@ -72,18 +70,18 @@ func initHTTPClient(cfg *config.Config) {
|
|||
httpc.WithTransport(tr),
|
||||
)
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func initGitHTTPClient(cfg *config.Config) {
|
||||
|
||||
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
|
||||
switch cfg.Httpc.Mode {
|
||||
case "auto", "":
|
||||
gittr = &http.Transport{
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
}
|
||||
} else if cfg.Httpc.Mode == "advanced" {
|
||||
case "advanced":
|
||||
gittr = &http.Transport{
|
||||
MaxIdleConns: cfg.Httpc.MaxIdleConns,
|
||||
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
|
||||
|
|
@ -91,84 +89,30 @@ func initGitHTTPClient(cfg *config.Config) {
|
|||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
panic("unknown httpc mode: " + cfg.Httpc.Mode)
|
||||
}
|
||||
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, gittr)
|
||||
}
|
||||
if cfg.Server.Debug && cfg.GitClone.ForceH2C {
|
||||
gitclient = httpc.New(
|
||||
httpc.WithTransport(gittr),
|
||||
httpc.WithDumpLog(),
|
||||
httpc.WithProtocols(httpc.ProtocolsConfig{
|
||||
ForceH2C: true,
|
||||
}),
|
||||
)
|
||||
} else if !cfg.Server.Debug && cfg.GitClone.ForceH2C {
|
||||
gitclient = httpc.New(
|
||||
httpc.WithTransport(gittr),
|
||||
httpc.WithProtocols(httpc.ProtocolsConfig{
|
||||
ForceH2C: true,
|
||||
}),
|
||||
)
|
||||
} else if cfg.Server.Debug && !cfg.GitClone.ForceH2C {
|
||||
gitclient = httpc.New(
|
||||
httpc.WithTransport(gittr),
|
||||
httpc.WithDumpLog(),
|
||||
httpc.WithProtocols(httpc.ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true,
|
||||
Http2_Cleartext: true,
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
gitclient = httpc.New(
|
||||
httpc.WithTransport(gittr),
|
||||
httpc.WithProtocols(httpc.ProtocolsConfig{
|
||||
Http1: true,
|
||||
Http2: true,
|
||||
Http2_Cleartext: true,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func initGhcrHTTPClient(cfg *config.Config) {
|
||||
var proTolcols = new(http.Protocols)
|
||||
proTolcols.SetHTTP1(true)
|
||||
proTolcols.SetHTTP2(true)
|
||||
if cfg.Httpc.Mode == "auto" || cfg.Httpc.Mode == "" {
|
||||
var opts []httpc.Option // 使用切片来收集选项
|
||||
opts = append(opts, httpc.WithTransport(gittr))
|
||||
var protocolsConfig httpc.ProtocolsConfig
|
||||
|
||||
ghcrtr = &http.Transport{
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
Protocols: proTolcols,
|
||||
}
|
||||
} else if cfg.Httpc.Mode == "advanced" {
|
||||
ghcrtr = &http.Transport{
|
||||
MaxIdleConns: cfg.Httpc.MaxIdleConns,
|
||||
MaxConnsPerHost: cfg.Httpc.MaxConnsPerHost,
|
||||
MaxIdleConnsPerHost: cfg.Httpc.MaxIdleConnsPerHost,
|
||||
WriteBufferSize: 32 * 1024, // 32KB
|
||||
ReadBufferSize: 32 * 1024, // 32KB
|
||||
Protocols: proTolcols,
|
||||
}
|
||||
if cfg.GitClone.ForceH2C {
|
||||
protocolsConfig.ForceH2C = true
|
||||
} else {
|
||||
panic(fmt.Sprintf("unknown httpc mode: %s", cfg.Httpc.Mode))
|
||||
}
|
||||
if cfg.Outbound.Enabled {
|
||||
initTransport(cfg, ghcrtr)
|
||||
protocolsConfig.Http1 = true
|
||||
protocolsConfig.Http2 = true
|
||||
protocolsConfig.Http2_Cleartext = true
|
||||
}
|
||||
opts = append(opts, httpc.WithProtocols(protocolsConfig))
|
||||
|
||||
if cfg.Server.Debug {
|
||||
ghcrclient = httpc.New(
|
||||
httpc.WithTransport(ghcrtr),
|
||||
httpc.WithDumpLog(),
|
||||
)
|
||||
} else {
|
||||
ghcrclient = httpc.New(
|
||||
httpc.WithTransport(ghcrtr),
|
||||
)
|
||||
opts = append(opts, httpc.WithDumpLog())
|
||||
}
|
||||
|
||||
gitclient = httpc.New(opts...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,11 +12,6 @@ func RoutingHandler(cfg *config.Config) touka.HandlerFunc {
|
|||
|
||||
var shoudBreak bool
|
||||
|
||||
// shoudBreak = rateCheck(cfg, c, limiter, iplimiter)
|
||||
// if shoudBreak {
|
||||
// return
|
||||
//}
|
||||
|
||||
var (
|
||||
rawPath string
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue