2020-05-19 23:53:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"strconv"
|
|
|
|
"encoding/base64"
|
|
|
|
"crypto/subtle"
|
|
|
|
)
|
|
|
|
|
|
|
|
const AUTH_REQUIRED_MSG = "Proxy authentication required."
|
|
|
|
|
|
|
|
type Auth interface {
|
|
|
|
Validate(wr http.ResponseWriter, req *http.Request) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuth(paramstr string) (Auth, error) {
|
|
|
|
url, err := url.Parse(paramstr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(url.Scheme) {
|
|
|
|
case "static":
|
|
|
|
auth, err := NewStaticAuth(url)
|
|
|
|
return auth, err
|
|
|
|
case "none":
|
|
|
|
return NoAuth{}, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown auth scheme")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:26:23 +02:00
|
|
|
type StaticAuth struct {
|
|
|
|
token string
|
|
|
|
hiddenDomain string
|
|
|
|
}
|
2020-05-19 23:53:32 +02:00
|
|
|
|
2020-05-24 17:26:23 +02:00
|
|
|
func NewStaticAuth(param_url *url.URL) (*StaticAuth, error) {
|
2020-05-19 23:53:32 +02:00
|
|
|
values, err := url.ParseQuery(param_url.RawQuery)
|
|
|
|
if err != nil {
|
2020-05-24 17:26:23 +02:00
|
|
|
return nil, err
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
username := values.Get("username")
|
|
|
|
if username == "" {
|
2020-05-24 17:26:23 +02:00
|
|
|
return nil, errors.New("\"username\" parameter is missing from auth config URI")
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
|
|
|
password := values.Get("password")
|
|
|
|
if password == "" {
|
2020-05-24 17:26:23 +02:00
|
|
|
return nil, errors.New("\"password\" parameter is missing from auth config URI")
|
2020-05-19 23:53:32 +02:00
|
|
|
}
|
2020-05-24 17:26:23 +02:00
|
|
|
return &StaticAuth{
|
|
|
|
token: base64.StdEncoding.EncodeToString([]byte(username + ":" + password)),
|
|
|
|
hiddenDomain: strings.ToLower(values.Get("hidden_domain")),
|
|
|
|
}, 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) {
|
|
|
|
if hidden_domain != "" && req.URL.Host != hidden_domain && req.Host != hidden_domain {
|
|
|
|
http.Error(wr, "Bad Request", 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 17:26:23 +02:00
|
|
|
func (auth *StaticAuth) Validate(wr http.ResponseWriter, req *http.Request) bool {
|
2020-05-19 23:53:32 +02:00
|
|
|
hdr := req.Header.Get("Proxy-Authorization")
|
|
|
|
if hdr == "" {
|
2020-05-24 17:26:23 +02:00
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2020-05-19 23:53:32 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
hdr_parts := strings.SplitN(hdr, " ", 2)
|
|
|
|
if len(hdr_parts) != 2 || strings.ToLower(hdr_parts[0]) != "basic" {
|
2020-05-24 17:26:23 +02:00
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2020-05-19 23:53:32 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
token := hdr_parts[1]
|
2020-05-24 17:26:23 +02:00
|
|
|
ok := (subtle.ConstantTimeCompare([]byte(token), []byte(auth.token)) == 1)
|
2020-05-19 23:53:32 +02:00
|
|
|
if ok {
|
|
|
|
return true
|
|
|
|
} else {
|
2020-05-24 17:26:23 +02:00
|
|
|
requireBasicAuth(wr, req, auth.hiddenDomain)
|
2020-05-19 23:53:32 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoAuth struct {}
|
|
|
|
|
|
|
|
func (_ NoAuth) Validate(wr http.ResponseWriter, req *http.Request) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|