From 35b2a887f3ee79c53e0e85d505f955ce92d7109d Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 7 Sep 2026 22:01:26 -0600 Subject: [PATCH] fix basic auth --- config.go | 2 +- main.go | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 09a78bd..81da588 100644 --- a/config.go +++ b/config.go @@ -23,7 +23,7 @@ type ( Endpoint struct { To string Redir bool - BasicAuth string + BasicAuth string `yaml:"basicAuth"` } ) diff --git a/main.go b/main.go index 607c5ab..3be8b65 100644 --- a/main.go +++ b/main.go @@ -47,13 +47,27 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if endpoint.Redir { - u, _ := url.Parse(endpoint.To) - r.URL = u - r.Host = u.Host - http.Redirect(w, r, r.URL.String(), http.StatusSeeOther) + 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) + 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) if r.Method == http.MethodOptions { 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 { ip = ip + u.Host[idx:] } - return (&net.Dialer{}).DialContext(ctx, network, ip) + return someDialer.DialContext(ctx, network, ip) } }