This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2023-02-13 15:34:47 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type hostInterceptor struct {
|
2023-02-14 13:31:04 +00:00
|
|
|
inner http.RoundTripper
|
|
|
|
host string
|
|
|
|
scheme string
|
2023-02-13 15:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t hostInterceptor) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
r.Host = t.host
|
2023-02-14 13:31:04 +00:00
|
|
|
r.Header.Set("X-Forwarded-Proto", t.scheme)
|
2023-02-13 15:34:47 +00:00
|
|
|
return t.inner.RoundTrip(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHostInterceptor(inner *http.Client, host string) *http.Client {
|
|
|
|
aku, err := url.Parse(host)
|
|
|
|
if err != nil {
|
|
|
|
log.WithField("host", host).WithError(err).Warn("failed to parse host")
|
|
|
|
}
|
|
|
|
return &http.Client{
|
|
|
|
Transport: hostInterceptor{
|
2023-02-14 13:31:04 +00:00
|
|
|
inner: inner.Transport,
|
|
|
|
host: aku.Host,
|
|
|
|
scheme: aku.Scheme,
|
2023-02-13 15:34:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|