debian-mirror-gitlab/workhorse/internal/proxy/proxy.go

107 lines
2.7 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package proxy
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"time"
2021-10-27 15:23:28 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper"
2023-03-04 22:38:38 +05:30
"gitlab.com/gitlab-org/gitlab/workhorse/internal/helper/nginx"
2021-02-22 17:27:13 +05:30
)
var (
defaultTarget = helper.URLMustParse("http://localhost")
)
type Proxy struct {
Version string
reverseProxy *httputil.ReverseProxy
AllowResponseBuffering bool
2022-03-02 08:16:31 +05:30
customHeaders map[string]string
2022-06-21 17:19:12 +05:30
forceTargetHostHeader bool
2021-02-22 17:27:13 +05:30
}
2022-03-02 08:16:31 +05:30
func WithCustomHeaders(customHeaders map[string]string) func(*Proxy) {
return func(proxy *Proxy) {
proxy.customHeaders = customHeaders
}
}
2022-06-21 17:19:12 +05:30
func WithForcedTargetHostHeader() func(*Proxy) {
return func(proxy *Proxy) {
proxy.forceTargetHostHeader = true
}
}
2022-03-02 08:16:31 +05:30
func NewProxy(myURL *url.URL, version string, roundTripper http.RoundTripper, options ...func(*Proxy)) *Proxy {
p := Proxy{Version: version, AllowResponseBuffering: true, customHeaders: make(map[string]string)}
2021-02-22 17:27:13 +05:30
if myURL == nil {
myURL = defaultTarget
}
u := *myURL // Make a copy of p.URL
u.Path = ""
p.reverseProxy = httputil.NewSingleHostReverseProxy(&u)
p.reverseProxy.Transport = roundTripper
2023-03-04 22:38:38 +05:30
chainDirector(p.reverseProxy, func(r *http.Request) {
r.Header.Set("Gitlab-Workhorse", p.Version)
r.Header.Set("Gitlab-Workhorse-Proxy-Start", fmt.Sprintf("%d", time.Now().UnixNano()))
for k, v := range p.customHeaders {
r.Header.Set(k, v)
}
})
2022-03-02 08:16:31 +05:30
for _, option := range options {
option(&p)
}
2022-06-21 17:19:12 +05:30
if p.forceTargetHostHeader {
// because of https://github.com/golang/go/issues/28168, the
// upstream won't receive the expected Host header unless this
// is forced in the Director func here
2023-03-04 22:38:38 +05:30
chainDirector(p.reverseProxy, func(request *http.Request) {
2022-06-21 17:19:12 +05:30
// send original host along for the upstream
// to know it's being proxied under a different Host
// (for redirects and other stuff that depends on this)
request.Header.Set("X-Forwarded-Host", request.Host)
request.Header.Set("Forwarded", fmt.Sprintf("host=%s", request.Host))
// override the Host with the target
request.Host = request.URL.Host
2023-03-04 22:38:38 +05:30
})
2022-06-21 17:19:12 +05:30
}
2021-02-22 17:27:13 +05:30
return &p
}
2023-03-04 22:38:38 +05:30
func chainDirector(rp *httputil.ReverseProxy, nextDirector func(*http.Request)) {
previous := rp.Director
rp.Director = func(r *http.Request) {
previous(r)
nextDirector(r)
2022-03-02 08:16:31 +05:30
}
2023-03-04 22:38:38 +05:30
}
2022-03-02 08:16:31 +05:30
2023-03-04 22:38:38 +05:30
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2021-02-22 17:27:13 +05:30
if p.AllowResponseBuffering {
2023-03-04 22:38:38 +05:30
nginx.AllowResponseBuffering(w)
2021-02-22 17:27:13 +05:30
}
// If the ultimate client disconnects when the response isn't fully written
// to them yet, httputil.ReverseProxy panics with a net/http.ErrAbortHandler
// error. We can catch and discard this to keep the error log clean
defer func() {
if err := recover(); err != nil {
if err != http.ErrAbortHandler {
panic(err)
}
}
}()
2023-03-04 22:38:38 +05:30
p.reverseProxy.ServeHTTP(w, r)
2021-02-22 17:27:13 +05:30
}