fix basic auth

This commit is contained in:
bel
2026-09-07 22:01:26 -06:00
parent e4acfff5a2
commit 35b2a887f3
2 changed files with 20 additions and 6 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ type (
Endpoint struct { Endpoint struct {
To string To string
Redir bool Redir bool
BasicAuth string BasicAuth string `yaml:"basicAuth"`
} }
) )
+19 -5
View File
@@ -47,13 +47,27 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if endpoint.Redir { if endpoint.Redir {
u, _ := url.Parse(endpoint.To) c.serveHTTPRedir(w, r, endpoint)
r.URL = u } else {
r.Host = u.Host c.serveHTTPProxy(w, r, endpoint)
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther) }
}
func (c Config) serveHTTPRedir(w http.ResponseWriter, r *http.Request, endpoint Endpoint) {
if !c.basicAuth(w, r) {
return return
} }
u, _ := url.Parse(endpoint.To)
r.URL = u
r.Host = u.Host
http.Redirect(w, r, r.URL.String(), http.StatusSeeOther)
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)
} }
} }