mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-08-05 20:58:28 +02:00

+ Added support for multiple upstreams + Added load balancer + Added upstream abstraction in endpoint + Added load balancer structure + Added breaking change auto-updater + Added uptime monitor proxy type definitions + Added upstream editor UI + Fixed charset bug in many snippets HTML files
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package dynamicproxy
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"imuslab.com/zoraxy/mod/auth"
|
|
)
|
|
|
|
/*
|
|
BasicAuth.go
|
|
|
|
This file handles the basic auth on proxy endpoints
|
|
if RequireBasicAuth is set to true
|
|
*/
|
|
|
|
func (h *ProxyHandler) handleBasicAuthRouting(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
|
|
err := handleBasicAuth(w, r, pe)
|
|
if err != nil {
|
|
h.Parent.logRequest(r, false, 401, "host", r.URL.Hostname())
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Handle basic auth logic
|
|
// do not write to http.ResponseWriter if err return is not nil (already handled by this function)
|
|
func handleBasicAuth(w http.ResponseWriter, r *http.Request, pe *ProxyEndpoint) error {
|
|
if len(pe.BasicAuthExceptionRules) > 0 {
|
|
//Check if the current path matches the exception rules
|
|
for _, exceptionRule := range pe.BasicAuthExceptionRules {
|
|
if strings.HasPrefix(r.RequestURI, exceptionRule.PathPrefix) {
|
|
//This path is excluded from basic auth
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
u, p, ok := r.BasicAuth()
|
|
if !ok {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
|
w.WriteHeader(401)
|
|
return errors.New("unauthorized")
|
|
}
|
|
|
|
//Check for the credentials to see if there is one matching
|
|
hashedPassword := auth.Hash(p)
|
|
matchingFound := false
|
|
for _, cred := range pe.BasicAuthCredentials {
|
|
if u == cred.Username && hashedPassword == cred.PasswordHash {
|
|
matchingFound = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !matchingFound {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
|
w.WriteHeader(401)
|
|
return errors.New("unauthorized")
|
|
}
|
|
|
|
return nil
|
|
}
|