add allowed-redirect config for upstream
This commit is contained in:
parent
08eed185cf
commit
7341742feb
@ -13,6 +13,7 @@ type UpstreamMatch struct {
|
|||||||
type Upstream struct {
|
type Upstream struct {
|
||||||
Server string `yaml:"server"`
|
Server string `yaml:"server"`
|
||||||
Match UpstreamMatch `yaml:"match"`
|
Match UpstreamMatch `yaml:"match"`
|
||||||
|
AllowedRedirect *string `yaml:"allowed-redirect"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (upstream Upstream) GetPath(orig string) (string, bool, error) {
|
func (upstream Upstream) GetPath(orig string) (string, bool, error) {
|
||||||
|
@ -83,6 +83,7 @@ upstream:
|
|||||||
match:
|
match:
|
||||||
match: /chaotic-aur/(.*)
|
match: /chaotic-aur/(.*)
|
||||||
replace: /$1
|
replace: /$1
|
||||||
|
allowed-redirect: "^https?://cf-builds.garudalinux.org/.*/chaotic-aur/.*$"
|
||||||
|
|
||||||
misc:
|
misc:
|
||||||
first-chunk-bytes: 31457280 # 1024*1024*30, all upstreams with 200 response will wait for the first chunks to select a upstream by bandwidth, instead of by latency. defaults to 50M
|
first-chunk-bytes: 31457280 # 1024*1024*30, all upstreams with 200 response will wait for the first chunks to select a upstream by bandwidth, instead of by latency. defaults to 50M
|
||||||
|
23
server.go
23
server.go
@ -17,13 +17,27 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type reqCtxKey int
|
||||||
|
|
||||||
|
const (
|
||||||
|
reqCtxAllowedRedirect reqCtxKey = iota
|
||||||
|
)
|
||||||
|
|
||||||
var zeroTime time.Time
|
var zeroTime time.Time
|
||||||
|
|
||||||
var (
|
var (
|
||||||
httpClient = http.Client{
|
httpClient = http.Client{
|
||||||
|
// check allowed redirect
|
||||||
// No redirect!
|
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
lastRequest := via[len(via)-1]
|
||||||
|
if allowedRedirect, ok := lastRequest.Context().Value(reqCtxAllowedRedirect).(string); ok {
|
||||||
|
if matched, err := regexp.MatchString(allowedRedirect, req.URL.String()); err != nil {
|
||||||
|
return err
|
||||||
|
} else if !matched {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return http.ErrUseLastResponse
|
return http.ErrUseLastResponse
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -492,6 +506,11 @@ func (server *Server) tryUpstream(ctx context.Context, upstreamIdx int, r *http.
|
|||||||
if lastModified != zeroTime {
|
if lastModified != zeroTime {
|
||||||
method = http.MethodGet
|
method = http.MethodGet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if upstream.AllowedRedirect != nil {
|
||||||
|
ctx = context.WithValue(ctx, reqCtxAllowedRedirect, *upstream.AllowedRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
request, err := http.NewRequestWithContext(ctx, method, newurl, nil)
|
request, err := http.NewRequestWithContext(ctx, method, newurl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user