common

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// B64StdWithPadding is a base64 encoding with standard padding.
	B64StdWithPadding = base64.StdEncoding.WithPadding(base64.StdPadding)

	// B64URLWithPadding is a base64 URL safe encoding without padding.
	B64URLNoPadding = base64.URLEncoding.WithPadding(base64.NoPadding)

	// B32StdNoPadding is a base32 encoding without padding.
	B32StdNoPadding = base32.StdEncoding.WithPadding(base32.NoPadding)
)

Functions

func CTEqual added in v0.0.9

func CTEqual[S interface{ ~string | ~[]byte }](a, b S) bool

CTEqual is a constant-time comparison function.

func Closer

func Closer(o any) io.ReadWriteCloser

Closer builds a ReadWriteCloser interface around the given object.

func CloserWrap

func CloserWrap(o any, err error) (io.ReadWriteCloser, error)

CloserWrap is a convenience function that wraps the given object and error into a ReadWriteCloser interface. Useful to catch return values from functions that return an error.

func ConfigIP2LocationApiKey added in v0.0.15

func ConfigIP2LocationApiKey() string

ConfigIP2LocationApiKey returns the IP2Location API key.

func ConfigProxyOTPSecret

func ConfigProxyOTPSecret() string

ConfigProxyOTPSecret returns the proxy OTP secret.

func ConfigProxyPass

func ConfigProxyPass() string

ConfigProxyPass returns the proxy password.

func ConfigProxyRedirectLoginURL

func ConfigProxyRedirectLoginURL() string

ConfigProxyRedirectLoginURL returns the proxy redirect login URL.

func ConfigProxySessionDuration added in v0.0.14

func ConfigProxySessionDuration() time.Duration

ConfigProxySessionDuration returns the proxy session duration.

func ConfigProxySessionSecret added in v0.0.11

func ConfigProxySessionSecret() string

ConfigProxySessionSecret returns the CSRF secret.

func ConfigProxyUser

func ConfigProxyUser() string

ConfigProxyUser returns the proxy user.

func ConfigSessionToken

func ConfigSessionToken() string

ConfigSessionToken returns the session token.

func DecodeFromQuery added in v0.0.8

func DecodeFromQuery(in string, secret []byte, session sessions.Session) (map[string]any, error)

DecodeFromQuery decodes a URL query parameter into a map. It uses the HMAC checksum stored in the session to verify the integrity of the data. The session has to be saved after calling this function.

func EncodeForQuery added in v0.0.8

func EncodeForQuery(in map[string]any, secret []byte, session sessions.Session) (string, error)

EncodeForQuery encodes the input map into a URL query parameter. It uses JSON encoding and base64 URL safe encoding without padding. It also calculates the HMAC checksum of the JSON data using the provided secret and stores it in the session. Checksum is used to verify the integrity of the data. The session has to be saved after calling this function.

func FatalOnError added in v0.0.15

func FatalOnError(msg string, err error)

FatalOnError logs the error and exits the program if the error is not nil.

func GetNonce added in v0.0.7

func GetNonce() (string, error)

GetNonce generates a random nonce. It returns a base64 encoded string. If the random number generator fails, it uses the current time for randomness.

func Getenv

func Getenv[T any](key string, fallback T) (out T)

Getenv returns the value of the environment variable with the given key.

func Logger

func Logger() *zap.Logger

Logger returns the logger. It uses the development logger by default.

func MakeKeyPair added in v0.0.7

func MakeKeyPair(in []byte) ([]byte, []byte)

MakeKeyPair generates a pair of 32-byte keys. If the input is empty, it generates a random key. If the input is 64 bytes, it splits it into two keys. If the input is less than 64 bytes, it fills just the first key and leaves the second key empty.

func NewUUID added in v0.0.11

func NewUUID() string

func QuickGet added in v0.0.8

func QuickGet[T, M any](m M, key string) (val T)

QuickGet is a type assertion helper for getting values from a map or an interface. It supports the following types: - map[string]T - map[string]any - interface{ Get(string) T } - interface{ Get(string) any } - interface{ Get(string) (T, bool) } - interface{ Get(string) (any, bool) } - interface{ Get(any) T } - interface{ Get(any) any } - interface{ Get(any) (T, bool) } - interface{ Get(any) (any, bool) }

func SetIP2LocationApiKey added in v0.0.15

func SetIP2LocationApiKey(apiKey string)

SetIP2LocationApiKey sets the IP2Location API key.

func SetProxyGuardPolicy added in v0.0.14

func SetProxyGuardPolicy(policy Policy)

SetProxyGuardPolicy sets the proxy guard rules.

func SetProxyOTPSecret added in v0.0.14

func SetProxyOTPSecret(secret string)

SetProxyOTPSecret sets the proxy OTP secret.

func SetProxyPass added in v0.0.14

func SetProxyPass(pass string)

SetProxyPass sets the proxy password.

func SetProxyPublicDomains added in v0.0.14

func SetProxyPublicDomains(domains Domains)

SetProxyPublicDomains sets the public domains.

func SetProxyRedirectLoginURL added in v0.0.14

func SetProxyRedirectLoginURL(url string)

SetProxyRedirectLoginURL sets the proxy redirect login URL.

func SetProxySessionDuration added in v0.0.14

func SetProxySessionDuration(duration time.Duration)

SetProxySessionDuration sets the proxy session duration.

func SetProxySessionSecret added in v0.0.14

func SetProxySessionSecret(secret string)

SetProxySessionSecret sets the CSRF secret.

func SetProxyTargetHosts added in v0.0.14

func SetProxyTargetHosts(hosts HostMap)

SetProxyTargetHosts sets the proxy target hosts.

func SetProxyUser added in v0.0.14

func SetProxyUser(user string)

SetProxyUser sets the proxy user.

func SetSessionToken added in v0.0.14

func SetSessionToken(token string)

SetSessionToken sets the session token.

Types

type Diff added in v0.0.16

type Diff struct {
	Added   Ruleset
	Removed Ruleset
}

type Domains added in v0.0.16

type Domains []string

Domains is a list of domains.

func ConfigProxyPublicDomains added in v0.0.13

func ConfigProxyPublicDomains() Domains

ConfigProxyPublicDomains returns the public domains.

func (Domains) Contains added in v0.0.16

func (d Domains) Contains(domain string) bool

Contains returns true if the domains contain the domain.

type Effect added in v0.0.16

type Effect bool
const (
	Allow Effect = true
	Deny  Effect = false
)

func (Effect) MarshalText added in v0.0.16

func (e Effect) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Effect) UnmarshalText added in v0.0.16

func (e *Effect) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type HostMap added in v0.0.14

type HostMap map[string]string

HostMap is a map of proxy host domains to target host domains.

func ConfigProxyTargetHosts

func ConfigProxyTargetHosts() HostMap

ConfigProxyTargetHosts returns the proxy target hosts.

func (HostMap) Base added in v0.0.14

func (t HostMap) Base() (base string)

Base returns the base host.

func (HostMap) Get added in v0.0.14

func (t HostMap) Get(host, def string) string

Get returns the target host for given proxy domain. If the target host is not found, it returns the default value. If the default value is not provided, it returns "NXDOMAIN".

func (HostMap) Reverse added in v0.0.14

func (t HostMap) Reverse() HostMap

Reverse returns the reversed hosts mapping.

type PathType added in v0.0.16

type PathType int
const (
	// Exact matches the path exactly.
	Exact PathType = iota
	// Prefix matches the path as a prefix.
	Prefix
	// Regex matches the path as an arbitrary regex.
	Regex
)

func (PathType) MarshalText added in v0.0.16

func (p PathType) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (*PathType) UnmarshalText added in v0.0.16

func (p *PathType) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Policy added in v0.0.12

type Policy struct {
	// Allow is the list of allow rules.
	// All allowed requests are publicly accessible without proxy authentication.
	Allow Ruleset `json:"allow,omitempty"`
	// Deny is the list of deny rules.
	// All denied requests are explicitly denied and not accessible via the proxy at all.
	Deny Ruleset `json:"deny,omitempty"`
	// Override is the list of form data override rules.
	// The override rules are applied to matching form data requests
	// to ensure the form data does not get altered.
	Override Ruleset `json:"override,omitempty"`
}

Policy is the access control policy. If effect is allow, the request is allowed publicly without proxy authentication. If effect is deny, the request is explicit denied and not accessible via the proxy.

func ConfigProxyGuardPolicy added in v0.0.12

func ConfigProxyGuardPolicy() Policy

ConfigProxyGuardPolicy returns the proxy guard rules.

func LoadPolicyFromFile added in v0.0.15

func LoadPolicyFromFile(path string) (*Policy, error)

LoadPolicyFromFile loads a policy from a file. If the path is empty, it returns an empty policy.

type Rule added in v0.0.10

type Rule struct {
	// FormData is the form data to patch the request with.
	FormData url.Values `json:"form_data,omitempty"`
	// JsSelectors is a list of JavaScript selectors.
	JsSelectors []string `json:"js_selectors,omitempty"`
	// Path is the URL path.
	Path string `json:"path"`
	// PathType is the type of the path. It can be exact, prefix, or regex.
	PathType PathType `json:"path_type"`
	// Query is the URL query parameters.
	Query url.Values `json:"query,omitempty"`
}

Rule is an access control rule.

func (Rule) Match added in v0.0.15

func (r Rule) Match(req *http.Request) bool

Match returns true if the rule matches the request.

func (Rule) PatchForm added in v0.0.15

func (r Rule) PatchForm(req *http.Request) (bool, error)

PatchForm patches the request form data with the rule query parameters. It returns true if the request form data has been patched.

func (Rule) Regex added in v0.0.10

func (r Rule) Regex() string

Regex returns the regex representation of the rule. The regex is used to match the request path and query.

type Ruleset added in v0.0.10

type Ruleset []Rule

Ruleset is a set of rules.

func (Ruleset) Compare added in v0.0.11

func (rules Ruleset) Compare(other Ruleset) Diff

Compare returns the difference between two rulesets.

func (Ruleset) Contains added in v0.0.11

func (rules Ruleset) Contains(other Rule) bool

Contains returns true if the ruleset contains the rule.

func (Ruleset) Evaluate added in v0.0.10

func (rules Ruleset) Evaluate(req *http.Request, noMatchEffect Effect) Effect

Evaluate returns the effect of the first matching rule. If no rule matches, it returns noMatchEffect.

func (Ruleset) JsSelectors added in v0.0.15

func (rules Ruleset) JsSelectors() []string

JsSelectors returns a list of JavaScript selectors for all rules.

func (Ruleset) Len added in v0.0.11

func (rules Ruleset) Len() int

Len returns the number of rules in the ruleset.

func (Ruleset) Merge added in v0.0.12

func (rules Ruleset) Merge(other Ruleset) Ruleset

Merge returns a new ruleset that contains all rules from both rulesets.

func (Ruleset) RegexList added in v0.0.10

func (rules Ruleset) RegexList() []string

RegexList returns a list of regex strings for each rule.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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