Add nested rules Add backend action, allow wildcard in backends Remove poison from tree, update README with action table Allow defining pass/fail actions on challenge, Remove redirect/referer parameters on backend pass Set challenge cookie tied to host Rewrite DNSBL condition into a challenge Allow passing an arbitrary path for assets to js challenges Optimize programs exhaustively on compilation Activation instead of map for CEL context, faster map access, new network override Return valid host on cookie setting in case Host is an IP address. bug: does not work with IPv6, see https://github.com/golang/go/issues/65521 Apply TLS fingerprinter on GetConfigForClient instead of GetCertificate Cleanup go-away cookies before passing to backend Code action for specifically replying with an HTTP code
150 lines
3.4 KiB
Go
150 lines
3.4 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"git.gammaspectra.live/git/go-away/lib/challenge"
|
|
"git.gammaspectra.live/git/go-away/utils"
|
|
"github.com/goccy/go-yaml"
|
|
"github.com/goccy/go-yaml/ast"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
challenge.Runtimes[Key] = FillRegistration
|
|
}
|
|
|
|
const Key = "dnsbl"
|
|
|
|
type Parameters struct {
|
|
VerifyProbability float64 `yaml:"verify-probability"`
|
|
Host string `yaml:"dnsbl-host"`
|
|
Timeout time.Duration `yaml:"dnsbl-timeout"`
|
|
Decay time.Duration `yaml:"dnsbl-decay"`
|
|
}
|
|
|
|
var DefaultParameters = Parameters{
|
|
VerifyProbability: 0.10,
|
|
Timeout: time.Second * 1,
|
|
Decay: time.Hour * 1,
|
|
Host: "dnsbl.dronebl.org",
|
|
}
|
|
|
|
func lookup(ctx context.Context, decay, timeout time.Duration, dnsbl *utils.DNSBL, decayMap *utils.DecayMap[[net.IPv6len]byte, utils.DNSBLResponse], ip net.IP) (utils.DNSBLResponse, error) {
|
|
var key [net.IPv6len]byte
|
|
copy(key[:], ip.To16())
|
|
|
|
result, ok := decayMap.Get(key)
|
|
if ok {
|
|
return result, nil
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
result, err := dnsbl.Lookup(ctx, ip)
|
|
if err != nil {
|
|
|
|
}
|
|
decayMap.Set(key, result, decay)
|
|
|
|
return result, err
|
|
}
|
|
|
|
type closer chan struct{}
|
|
|
|
func (c closer) Close() error {
|
|
select {
|
|
case <-c:
|
|
default:
|
|
close(c)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FillRegistration(state challenge.StateInterface, reg *challenge.Registration, parameters ast.Node) error {
|
|
params := DefaultParameters
|
|
|
|
if parameters != nil {
|
|
ymlData, err := parameters.MarshalYAML()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = yaml.Unmarshal(ymlData, ¶ms)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if params.Host == "" {
|
|
return errors.New("empty host")
|
|
}
|
|
|
|
reg.Class = challenge.ClassTransparent
|
|
|
|
if params.VerifyProbability <= 0 {
|
|
//20% default
|
|
params.VerifyProbability = 0.20
|
|
} else if params.VerifyProbability > 1.0 {
|
|
params.VerifyProbability = 1.0
|
|
}
|
|
reg.VerifyProbability = params.VerifyProbability
|
|
|
|
decayMap := utils.NewDecayMap[[net.IPv6len]byte, utils.DNSBLResponse]()
|
|
|
|
dnsbl := utils.NewDNSBL(params.Host, &net.Resolver{
|
|
PreferGo: true,
|
|
})
|
|
|
|
ob := make(closer)
|
|
|
|
go func() {
|
|
ticker := time.NewTicker(params.Timeout / 3)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
decayMap.Decay()
|
|
case <-ob:
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// allow freeing the ticker/decay map
|
|
reg.Object = ob
|
|
|
|
reg.IssueChallenge = func(w http.ResponseWriter, r *http.Request, key challenge.Key, expiry time.Time) challenge.VerifyResult {
|
|
|
|
data := challenge.RequestDataFromContext(r.Context())
|
|
|
|
result, err := lookup(r.Context(), params.Decay, params.Timeout, dnsbl, decayMap, data.RemoteAddress)
|
|
if err != nil {
|
|
data.State.Logger(r).Debug("dnsbl lookup failed", "address", data.RemoteAddress.String(), "result", result, "err", err)
|
|
}
|
|
|
|
if err != nil {
|
|
return challenge.VerifyResultFail
|
|
}
|
|
|
|
if result.Bad() {
|
|
token, err := reg.IssueChallengeToken(state.PrivateKey(), key, nil, expiry, false)
|
|
if err != nil {
|
|
return challenge.VerifyResultFail
|
|
}
|
|
utils.SetCookie(utils.CookiePrefix+reg.Name, token, expiry, w, r)
|
|
return challenge.VerifyResultNotOK
|
|
} else {
|
|
token, err := reg.IssueChallengeToken(state.PrivateKey(), key, nil, expiry, true)
|
|
if err != nil {
|
|
return challenge.VerifyResultFail
|
|
}
|
|
utils.SetCookie(utils.CookiePrefix+reg.Name, token, expiry, w, r)
|
|
return challenge.VerifyResultOK
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|