Files
go-away/lib/action/backend.go
WeebDataHoarder ead41055ca Condition, rules, state and action refactor / rewrite
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
2025-04-23 20:35:20 +02:00

81 lines
1.7 KiB
Go

package action
import (
"fmt"
"git.gammaspectra.live/git/go-away/lib/challenge"
"git.gammaspectra.live/git/go-away/lib/policy"
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"log/slog"
"net/http"
"regexp"
)
func init() {
Register[policy.RuleActionPROXY] = func(state challenge.StateInterface, ruleName, ruleHash string, settings ast.Node) (Handler, error) {
params := ProxyDefaultSettings
if settings != nil {
ymlData, err := settings.MarshalYAML()
if err != nil {
return nil, err
}
err = yaml.Unmarshal(ymlData, &params)
if err != nil {
return nil, err
}
}
if params.Match != "" {
expr, err := regexp.Compile(params.Match)
if err != nil {
return nil, err
}
return Proxy{
Match: expr,
Rewrite: params.Rewrite,
Backend: params.Backend,
}, nil
}
return Proxy{
Backend: params.Backend,
}, nil
}
}
var ProxyDefaultSettings = ProxySettings{}
type ProxySettings struct {
Match string `yaml:"proxy-match"`
Rewrite string `yaml:"proxy-rewrite"`
Backend string `yaml:"proxy-backend"`
}
type Proxy struct {
Match *regexp.Regexp
Rewrite string
Backend string
}
func (a Proxy) Handle(logger *slog.Logger, w http.ResponseWriter, r *http.Request, done func() (backend http.Handler)) (next bool, err error) {
data := challenge.RequestDataFromContext(r.Context())
backend := data.State.GetBackend(a.Backend)
if backend == nil {
return false, fmt.Errorf("backend for %s not found", a.Backend)
}
if a.Match != nil {
// rewrite query
r.URL.Path = a.Match.ReplaceAllString(r.URL.Path, a.Rewrite)
}
// set headers, ignore reply
_ = done()
backend.ServeHTTP(w, r)
return false, nil
}