Quentin McGaw 3c8e80a1a4
chore(lint): upgrade linter from v1.56.2 to v1.61.0
- Remove no longer needed exclude rules
- Add new exclude rules for printf govet errors
- Remove deprecated linters `execinquery` and `exportloopref`
- Rename linter `goerr113` to `err113`
- Rename linter `gomnd` to `mnd`
2024-10-11 18:05:54 +00:00

45 lines
1.6 KiB
Go

package httpproxy
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
)
func (h *handler) isAuthorized(responseWriter http.ResponseWriter, request *http.Request) (authorized bool) {
if h.username == "" || (request.Method != http.MethodConnect && !request.URL.IsAbs()) {
return true
}
basicAuth := request.Header.Get("Proxy-Authorization")
if basicAuth == "" {
h.logger.Info("Proxy-Authorization header not found from " + request.RemoteAddr)
responseWriter.Header().Set("Proxy-Authenticate", `Basic realm="Access to Gluetun over HTTP"`)
responseWriter.WriteHeader(http.StatusProxyAuthRequired)
return false
}
b64UsernamePassword := strings.TrimPrefix(basicAuth, "Basic ")
b, err := base64.StdEncoding.DecodeString(b64UsernamePassword)
if err != nil {
h.logger.Info("Cannot decode Proxy-Authorization header value from " +
request.RemoteAddr + ": " + err.Error())
responseWriter.WriteHeader(http.StatusUnauthorized)
return false
}
usernamePassword := strings.Split(string(b), ":")
const expectedFields = 2
if len(usernamePassword) != expectedFields {
responseWriter.WriteHeader(http.StatusBadRequest)
return false
}
if h.username != usernamePassword[0] || h.password != usernamePassword[1] {
h.logger.Info(fmt.Sprintf("Username (%q) or password (%q) mismatch from %s",
usernamePassword[0], usernamePassword[1], request.RemoteAddr))
h.logger.Debug("username provided \"" + usernamePassword[0] +
"\" and password provided \"" + usernamePassword[1] + "\"")
responseWriter.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}