rules

package
v0.11.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CacheKeyQueries   = "queries"
	CacheKeyCookies   = "cookies"
	CacheKeyRemoteIP  = "remote_ip"
	CacheKeyBasicAuth = "basic_auth"
)
View Source
const (
	CommandRewrite          = "rewrite"
	CommandServe            = "serve"
	CommandProxy            = "proxy"
	CommandRedirect         = "redirect"
	CommandError            = "error"
	CommandRequireBasicAuth = "require_basic_auth"
	CommandSet              = "set"
	CommandAdd              = "add"
	CommandRemove           = "remove"
	CommandPass             = "pass"
	CommandPassAlt          = "bypass"
)
View Source
const (
	FieldHeader = "header"
	FieldQuery  = "query"
	FieldCookie = "cookie"
)
View Source
const (
	OnHeader    = "header"
	OnQuery     = "query"
	OnCookie    = "cookie"
	OnForm      = "form"
	OnPostForm  = "postform"
	OnMethod    = "method"
	OnPath      = "path"
	OnRemote    = "remote"
	OnBasicAuth = "basic_auth"
)

Variables

View Source
var (
	ErrUnterminatedQuotes     = gperr.New("unterminated quotes")
	ErrUnsupportedEscapeChar  = gperr.New("unsupported escape char")
	ErrUnknownDirective       = gperr.New("unknown directive")
	ErrInvalidArguments       = gperr.New("invalid arguments")
	ErrInvalidOnTarget        = gperr.New("invalid `rule.on` target")
	ErrInvalidCommandSequence = gperr.New("invalid command sequence")
	ErrInvalidSetTarget       = gperr.New("invalid `rule.set` target")

	ErrExpectNoArg       = gperr.Wrap(ErrInvalidArguments, "expect no arg")
	ErrExpectOneArg      = gperr.Wrap(ErrInvalidArguments, "expect 1 arg")
	ErrExpectTwoArgs     = gperr.Wrap(ErrInvalidArguments, "expect 2 args")
	ErrExpectKVOptionalV = gperr.Wrap(ErrInvalidArguments, "expect 'key' or 'key value'")
)

Functions

This section is empty.

Types

type BypassCommand

type BypassCommand struct{}

BypassCommand will skip all the following commands and directly return to reverse proxy.

func (BypassCommand) Handle

func (c BypassCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

type Cache

type Cache map[string]any

Cache is a map of cached values for a request. It prevents the same value from being parsed multiple times.

func NewCache

func NewCache() Cache

NewCache returns a new Cached.

func (Cache) GetBasicAuth

func (c Cache) GetBasicAuth(r *http.Request) *Credentials

GetBasicAuth returns *Credentials the basic auth username and password. If r does not have basic auth, nil is returned.

func (Cache) GetCookies

func (c Cache) GetCookies(r *http.Request) []*http.Cookie

GetCookies returns the cookies. If r does not have cookies, an empty slice is returned.

func (Cache) GetQueries

func (c Cache) GetQueries(r *http.Request) url.Values

GetQueries returns the queries. If r does not have queries, an empty map is returned.

func (Cache) GetRemoteIP

func (c Cache) GetRemoteIP(r *http.Request) net.IP

GetRemoteIP returns the remote ip address. If r.RemoteAddr is not a valid ip address, nil is returned.

func (Cache) Release

func (c Cache) Release()

Release clear the contents of the Cached and returns it to the pool.

func (Cache) UpdateCookies

func (c Cache) UpdateCookies(r *http.Request, update UpdateFunc[[]*http.Cookie])

func (Cache) UpdateQueries

func (c Cache) UpdateQueries(r *http.Request, update func(url.Values))

type CheckFunc

type CheckFunc func(cached Cache, r *http.Request) bool

func (CheckFunc) Check

func (checker CheckFunc) Check(cached Cache, r *http.Request) bool

type CheckMatchAll

type CheckMatchAll []Checker

func (CheckMatchAll) Check

func (checkers CheckMatchAll) Check(cached Cache, r *http.Request) bool

type CheckMatchSingle

type CheckMatchSingle []Checker

func (CheckMatchSingle) Check

func (checkers CheckMatchSingle) Check(cached Cache, r *http.Request) bool

type Checker

type Checker interface {
	Check(cached Cache, r *http.Request) bool
}

type Command

type Command struct {
	// contains filtered or unexported fields
}

func (*Command) MarshalText

func (cmd *Command) MarshalText() ([]byte, error)

func (*Command) Parse

func (cmd *Command) Parse(v string) error

Parse implements strutils.Parser.

func (*Command) String

func (cmd *Command) String() string

type CommandHandler

type CommandHandler interface {
	// CommandHandler can read and modify the values
	// then handle the request
	// finally proceed to next command (or return) base on situation
	Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)
}

type Commands

type Commands []CommandHandler

Commands is a slice of CommandHandler.

func (Commands) Handle

func (c Commands) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

type Credentials

type Credentials struct {
	Username string
	Password []byte
}

type DynamicCommand

type DynamicCommand func(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

DynamicCommand will return base on the request and can raed or modify the values.

func (DynamicCommand) Handle

func (c DynamicCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

type FieldHandler

type FieldHandler struct {
	// contains filtered or unexported fields
}

type FieldModifier

type FieldModifier string
const (
	ModFieldSet    FieldModifier = "set"
	ModFieldAdd    FieldModifier = "add"
	ModFieldRemove FieldModifier = "remove"
)

type HashedCrendentials

type HashedCrendentials struct {
	Username   string
	CheckMatch func(inputPwd []byte) bool
}

func BCryptCrendentials

func BCryptCrendentials(username string, hashedPassword []byte) *HashedCrendentials

func (*HashedCrendentials) Match

func (hc *HashedCrendentials) Match(cred *Credentials) bool

type Help

type Help struct {
	// contains filtered or unexported fields
}

func (*Help) String

func (h *Help) String() string

Generate help string, e.g.

 rewrite <from> <to>
	from: the path to rewrite, must start with /
	to: the path to rewrite to, must start with /

type ReturningCommand

type ReturningCommand http.HandlerFunc

ReturningCommand will run then return immediately.

func (ReturningCommand) Handle

func (c ReturningCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

type Rule

type Rule struct {
	Name string  `json:"name"`
	On   RuleOn  `json:"on"`
	Do   Command `json:"do"`
}

Rule is a rule for a reverse proxy. It do `Do` when `On` matches.

A rule can have multiple lines of on.

All lines of on must match, but each line can have multiple checks that one match means this line is matched.

func (*Rule) Check

func (rule *Rule) Check(cached Cache, r *http.Request) bool

func (*Rule) Handle

func (rule *Rule) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

func (*Rule) String

func (rule *Rule) String() string

type RuleOn

type RuleOn struct {
	// contains filtered or unexported fields
}

func (*RuleOn) MarshalText

func (on *RuleOn) MarshalText() ([]byte, error)

func (*RuleOn) Parse

func (on *RuleOn) Parse(v string) error

Parse implements strutils.Parser.

func (*RuleOn) String

func (on *RuleOn) String() string

type Rules

type Rules []*Rule

Example:

proxy.app1.rules: |
	- name: default
		do: |
			rewrite / /index.html
			serve /var/www/goaccess
	- name: ws
		on: |
			header Connection Upgrade
			header Upgrade websocket
		do: bypass

proxy.app2.rules: |
	- name: default
		do: bypass
	- name: block POST and PUT
		on: method POST | method PUT
		do: error 403 Forbidden

func (Rules) BuildHandler

func (rules Rules) BuildHandler(caller string, up http.Handler) http.HandlerFunc

BuildHandler returns a http.HandlerFunc that implements the rules.

if a bypass rule matches,
the request is passed to the upstream and no more rules are executed.

if no rule matches, the default rule is executed
if no rule matches and default rule is not set,
the request is passed to the upstream.

func (Rules) MarshalJSON

func (rules Rules) MarshalJSON() ([]byte, error)

type StaticCommand

type StaticCommand http.HandlerFunc

StaticCommand will run then proceed to next command or reverse proxy.

func (StaticCommand) Handle

func (c StaticCommand) Handle(cached Cache, w http.ResponseWriter, r *http.Request) (proceed bool)

type StrTuple

type StrTuple = Tuple[string, string]

type Tuple

type Tuple[T1, T2 any] struct {
	First  T1
	Second T2
}

func (*Tuple[T1, T2]) String

func (t *Tuple[T1, T2]) String() string

func (*Tuple[T1, T2]) Unpack

func (t *Tuple[T1, T2]) Unpack() (T1, T2)

type UpdateFunc

type UpdateFunc[T any] func(T) T

Cache is a map of cached values for a request. It prevents the same value from being parsed multiple times.

type ValidateFunc

type ValidateFunc func(args []string) (any, gperr.Error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL