fix basic auth
This commit is contained in:
@@ -23,7 +23,7 @@ type (
|
||||
Endpoint struct {
|
||||
To string
|
||||
Redir bool
|
||||
BasicAuth string
|
||||
BasicAuth string `yaml:"basicAuth"`
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -47,6 +47,17 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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)
|
||||
r.URL = u
|
||||
r.Host = u.Host
|
||||
@@ -54,6 +65,9 @@ func (c Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user