fix basic auth
This commit is contained in:
@@ -23,7 +23,7 @@ type (
|
|||||||
Endpoint struct {
|
Endpoint struct {
|
||||||
To string
|
To string
|
||||||
Redir bool
|
Redir bool
|
||||||
BasicAuth string
|
BasicAuth string `yaml:"basicAuth"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -47,13 +47,27 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if endpoint.Redir {
|
if endpoint.Redir {
|
||||||
|
c.serveHTTPRedir(w, r, endpoint)
|
||||||
|
} else {
|
||||||
|
c.serveHTTPProxy(w, r, endpoint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) serveHTTPRedir(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||||
|
if !c.basicAuth(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
u, _ := url.Parse(endpoint.To)
|
u, _ := url.Parse(endpoint.To)
|
||||||
r.URL = u
|
r.URL = u
|
||||||
r.Host = u.Host
|
r.Host = u.Host
|
||||||
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var someDialer = &net.Dialer{}
|
||||||
|
|
||||||
|
func (c Config) serveHTTPProxy(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
|
||||||
cors(w)
|
cors(w)
|
||||||
if r.Method == http.MethodOptions {
|
if r.Method == http.MethodOptions {
|
||||||
w.Header().Set("Content-Length", "0")
|
w.Header().Set("Content-Length", "0")
|
||||||
@@ -86,7 +100,7 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if idx := strings.LastIndex(u.Host, ":"); idx > 0 {
|
if idx := strings.LastIndex(u.Host, ":"); idx > 0 {
|
||||||
ip = ip + u.Host[idx:]
|
ip = ip + u.Host[idx:]
|
||||||
}
|
}
|
||||||
return (&net.Dialer{}).DialContext(ctx, network, ip)
|
return someDialer.DialContext(ctx, network, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user