2020-05-19 23:53:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-06 18:57:18 +02:00
|
|
|
"bytes"
|
2021-02-26 08:09:55 +01:00
|
|
|
"crypto/subtle"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
2022-09-06 18:57:18 +02:00
|
|
|
"fmt"
|
2021-02-26 08:09:55 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-09-06 18:57:18 +02:00
|
|
|
"sync"
|
2022-09-06 20:43:56 +02:00
|
|
|
"time"
|
2022-09-04 21:44:15 +02:00
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
"github.com/tg123/go-htpasswd"
|
2022-09-04 21:44:15 +02:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2020-05-19 23:53:32 +02:00
|
|
|
)
|
|
|
|
|
2020-05-24 20:32:40 +02:00
|
|
|
const AUTH_REQUIRED_MSG = "Proxy authentication required.\n"
|
2020-05-24 23:24:46 +02:00
|
|
|
const BAD_REQ_MSG = "Bad Request\n"
|
2020-05-28 16:53:04 +02:00
|
|
|
const AUTH_TRIGGERED_MSG = "Browser auth triggered!\n"
|
|
|
|
const EPOCH_EXPIRE = "Thu, 01 Jan 1970 00:00:01 GMT"
|
2020-05-19 23:53:32 +02:00
|
|
|
|
|
|
|
type Auth interface {
|
2022-09-14 20:52:19 +02:00
|
|
|
Validate(wr http.ResponseWriter, req *http.Request) (string, bool)
|
2022-09-06 20:43:56 +02:00
|
|
|
Stop()
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
func NewAuth(paramstr string, logger *CondLogger) (Auth, error) {
|
2021-02-26 08:09:55 +01:00
|
|
|
url, err := url.Parse(paramstr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(url.Scheme) {
|
|
|
|
case "static":
|
2022-09-06 18:57:18 +02:00
|
|
|
return NewStaticAuth(url, logger)
|
2021-02-26 08:09:55 +01:00
|
|
|
case "basicfile":
|
2022-09-06 18:57:18 +02:00
|
|
|
return NewBasicFileAuth(url, logger)
|
2021-02-26 08:09:55 +01:00
|
|
|
case "cert":
|
|
|
|
return CertAuth{}, nil
|
|
|
|
case "none":
|
|
|
|
return NoAuth{}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown auth scheme")
|
|
|
|
}
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
func NewStaticAuth(param_url *url.URL, logger *CondLogger) (*BasicAuth, error) {
|
2021-02-26 08:09:55 +01:00
|
|
|
values, err := url.ParseQuery(param_url.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
username := values.Get("username")
|
|
|
|
if username == "" {
|
|
|
|
return nil, errors.New("\"username\" parameter is missing from auth config URI")
|
|
|
|
}
|
|
|
|
password := values.Get("password")
|
|
|
|
if password == "" {
|
|
|
|
return nil, errors.New("\"password\" parameter is missing from auth config URI")
|
|
|
|
}
|
|
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-06 18:57:18 +02:00
|
|
|
buf := bytes.NewBufferString(username)
|
|
|
|
buf.WriteByte(':')
|
|
|
|
buf.Write(hashedPassword)
|
|
|
|
|
|
|
|
pwFile, err := htpasswd.NewFromReader(buf, htpasswd.DefaultSystems, func(parseError error) {
|
|
|
|
logger.Error("static auth: password entry parse error: %v", err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't instantiate pwFile: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-02-26 08:09:55 +01:00
|
|
|
return &BasicAuth{
|
|
|
|
hiddenDomain: strings.ToLower(values.Get("hidden_domain")),
|
2022-09-06 18:57:18 +02:00
|
|
|
logger: logger,
|
|
|
|
pwFile: pwFile,
|
2022-09-06 20:43:56 +02:00
|
|
|
stopChan: make(chan struct{}),
|
2021-02-26 08:09:55 +01:00
|
|
|
}, nil
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:26:23 +02:00
|
|
|
func requireBasicAuth(wr http.ResponseWriter, req *http.Request, hidden_domain string) {
|
2021-02-26 08:09:55 +01:00
|
|
|
if hidden_domain != "" &&
|
|
|
|
(subtle.ConstantTimeCompare([]byte(req.URL.Host), []byte(hidden_domain)) != 1 &&
|
|
|
|
subtle.ConstantTimeCompare([]byte(req.Host), []byte(hidden_domain)) != 1) {
|
|
|
|
http.Error(wr, BAD_REQ_MSG, http.StatusBadRequest)
|
|
|
|
} else {
|
|
|
|
wr.Header().Set("Proxy-Authenticate", `Basic realm="dumbproxy"`)
|
|
|
|
wr.Header().Set("Content-Length", strconv.Itoa(len([]byte(AUTH_REQUIRED_MSG))))
|
|
|
|
wr.WriteHeader(407)
|
|
|
|
wr.Write([]byte(AUTH_REQUIRED_MSG))
|
|
|
|
}
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 20:32:40 +02:00
|
|
|
type BasicAuth struct {
|
2022-09-06 18:57:18 +02:00
|
|
|
pwFilename string
|
|
|
|
pwFile *htpasswd.File
|
|
|
|
pwMux sync.RWMutex
|
|
|
|
logger *CondLogger
|
2021-02-26 08:09:55 +01:00
|
|
|
hiddenDomain string
|
2022-09-06 20:43:56 +02:00
|
|
|
stopOnce sync.Once
|
|
|
|
stopChan chan struct{}
|
|
|
|
lastReloaded time.Time
|
2020-05-24 20:32:40 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
func NewBasicFileAuth(param_url *url.URL, logger *CondLogger) (*BasicAuth, error) {
|
2021-02-26 08:09:55 +01:00
|
|
|
values, err := url.ParseQuery(param_url.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filename := values.Get("path")
|
|
|
|
if filename == "" {
|
|
|
|
return nil, errors.New("\"path\" parameter is missing from auth config URI")
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
auth := &BasicAuth{
|
|
|
|
hiddenDomain: strings.ToLower(values.Get("hidden_domain")),
|
|
|
|
pwFilename: filename,
|
|
|
|
logger: logger,
|
2022-09-06 20:43:56 +02:00
|
|
|
stopChan: make(chan struct{}),
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
if err := auth.reload(); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to load initial password list: %w", err)
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
2022-09-06 18:57:18 +02:00
|
|
|
|
2022-09-06 20:43:56 +02:00
|
|
|
reloadIntervalOption := values.Get("reload")
|
|
|
|
reloadInterval, err := time.ParseDuration(reloadIntervalOption)
|
|
|
|
if err != nil {
|
|
|
|
reloadInterval = 0
|
|
|
|
}
|
|
|
|
if reloadInterval == 0 {
|
|
|
|
reloadInterval = 15 * time.Second
|
|
|
|
}
|
|
|
|
if reloadInterval > 0 {
|
|
|
|
go auth.reloadLoop(reloadInterval)
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
return auth, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *BasicAuth) reload() error {
|
2022-09-06 20:43:56 +02:00
|
|
|
auth.logger.Info("reloading password file from %q...", auth.pwFilename)
|
2022-09-06 18:57:18 +02:00
|
|
|
newPwFile, err := htpasswd.New(auth.pwFilename, htpasswd.DefaultSystems, func(parseErr error) {
|
|
|
|
auth.logger.Error("failed to parse line in %q: %v", auth.pwFilename, parseErr)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
2022-09-06 18:57:18 +02:00
|
|
|
|
2022-09-06 20:43:56 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
auth.pwMux.Lock()
|
|
|
|
auth.pwFile = newPwFile
|
2022-09-06 20:43:56 +02:00
|
|
|
auth.lastReloaded = now
|
|
|
|
auth.pwMux.Unlock()
|
|
|
|
auth.logger.Info("password file reloaded.")
|
2022-09-06 18:57:18 +02:00
|
|
|
|
|
|
|
return nil
|
2020-05-24 20:32:40 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 20:43:56 +02:00
|
|
|
func (auth *BasicAuth) condReload() error {
|
|
|
|
reload := func() bool {
|
|
|
|
pwFileModTime, err := fileModTime(auth.pwFilename)
|
|
|
|
if err != nil {
|
|
|
|
auth.logger.Warning("can't get password file modtime: %v", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !pwFileModTime.Before(auth.lastReloaded)
|
|
|
|
}()
|
|
|
|
if reload {
|
|
|
|
return auth.reload()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *BasicAuth) reloadLoop(interval time.Duration) {
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-auth.stopChan:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
auth.condReload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 20:52:19 +02:00
|
|
|
func (auth *BasicAuth) Validate(wr http.ResponseWriter, req *http.Request) (string, bool) {
|
2021-02-26 08:09:55 +01:00
|
|
|
hdr := req.Header.Get("Proxy-Authorization")
|
|
|
|
if hdr == "" {
|
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
hdr_parts := strings.SplitN(hdr, " ", 2)
|
|
|
|
if len(hdr_parts) != 2 || strings.ToLower(hdr_parts[0]) != "basic" {
|
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
token := hdr_parts[1]
|
|
|
|
data, err := base64.StdEncoding.DecodeString(token)
|
|
|
|
if err != nil {
|
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pair := strings.SplitN(string(data), ":", 2)
|
|
|
|
if len(pair) != 2 {
|
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
login := pair[0]
|
|
|
|
password := pair[1]
|
2022-09-06 20:43:56 +02:00
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
auth.pwMux.RLock()
|
|
|
|
pwFile := auth.pwFile
|
|
|
|
auth.pwMux.RUnlock()
|
2021-02-26 08:09:55 +01:00
|
|
|
|
2022-09-06 18:57:18 +02:00
|
|
|
if pwFile.Match(login, password) {
|
2021-02-26 08:09:55 +01:00
|
|
|
if auth.hiddenDomain != "" &&
|
|
|
|
(req.Host == auth.hiddenDomain || req.URL.Host == auth.hiddenDomain) {
|
|
|
|
wr.Header().Set("Content-Length", strconv.Itoa(len([]byte(AUTH_TRIGGERED_MSG))))
|
|
|
|
wr.Header().Set("Pragma", "no-cache")
|
|
|
|
wr.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
|
|
|
wr.Header().Set("Expires", EPOCH_EXPIRE)
|
|
|
|
wr.Header()["Date"] = nil
|
|
|
|
wr.WriteHeader(http.StatusOK)
|
|
|
|
wr.Write([]byte(AUTH_TRIGGERED_MSG))
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
} else {
|
2022-09-14 20:52:19 +02:00
|
|
|
return login, true
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2020-05-24 20:32:40 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 20:43:56 +02:00
|
|
|
func (auth *BasicAuth) Stop() {
|
|
|
|
auth.stopOnce.Do(func() {
|
|
|
|
close(auth.stopChan)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-26 08:09:55 +01:00
|
|
|
type NoAuth struct{}
|
2020-05-19 23:53:32 +02:00
|
|
|
|
2022-09-14 20:52:19 +02:00
|
|
|
func (_ NoAuth) Validate(wr http.ResponseWriter, req *http.Request) (string, bool) {
|
|
|
|
return "", true
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 20:43:56 +02:00
|
|
|
func (_ NoAuth) Stop() {}
|
|
|
|
|
2021-02-26 08:09:55 +01:00
|
|
|
type CertAuth struct{}
|
2020-05-24 23:16:56 +02:00
|
|
|
|
2022-09-14 20:52:19 +02:00
|
|
|
func (_ CertAuth) Validate(wr http.ResponseWriter, req *http.Request) (string, bool) {
|
|
|
|
if req.TLS == nil || len(req.TLS.VerifiedChains) < 1 || len(req.TLS.VerifiedChains[0]) < 1 {
|
2021-02-26 08:09:55 +01:00
|
|
|
http.Error(wr, BAD_REQ_MSG, http.StatusBadRequest)
|
2022-09-14 20:52:19 +02:00
|
|
|
return "", false
|
2021-02-26 08:09:55 +01:00
|
|
|
} else {
|
2022-09-14 20:52:19 +02:00
|
|
|
return req.TLS.VerifiedChains[0][0].Subject.String(), true
|
2021-02-26 08:09:55 +01:00
|
|
|
}
|
2020-05-24 23:16:56 +02:00
|
|
|
}
|
2022-09-06 20:43:56 +02:00
|
|
|
|
|
|
|
func (_ CertAuth) Stop() {}
|