requestcontext

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetClientIP

func GetClientIP(ctx context.Context) string

GetClientIP get clientIP from context. If not found, return empty string

Warning: Request context should be setup before using this function

func GetRequestId

func GetRequestId(ctx context.Context) string

GetRequestId get requestId from context. If not found, return empty string

Warning: Request context should be setup before using this function

func New

func New(opts ...Option) fiber.Handler

Types

type Option

type Option func(ctx context.Context, c *fiber.Ctx) (context.Context, error)

func WithClientIP

func WithClientIP(config WithClientIPConfig) Option

WithClientIP setup client IP context with XFF Spoofing prevention support.

If request is from proxies, it will use first IP from `X-Forwarded-For` header by default.

func WithRequestId

func WithRequestId() Option

type Response

type Response struct {
	Result any    `json:"result"`
	Error  string `json:"error,omitempty"`
}

type WithClientIPConfig

type WithClientIPConfig struct {
	// [Optional] TrustedProxiesIP is a list of all proxies IP ranges that's between the server and the client.
	//
	// If it's provided, it will walk backwards from the last IP in `X-Forwarded-For` header
	// and use first IP that's not trusted proxy(not in the given IP ranges.)
	//
	// **If you want to use this option, you should provide all of probable proxies IP ranges.**
	//
	// This is lowest priority.
	TrustedProxiesIP []string `env:"TRUSTED_PROXIES_IP" mapstructure:"trusted_proxies_ip"`

	// [Optional] TrustedHeader is a header name for getting client IP. (e.g. X-Real-IP, CF-Connecting-IP, etc.)
	//
	// This is highest priority, it will ignore rest of the options if it's provided.
	TrustedHeader string `env:"TRUSTED_HEADER" mapstructure:"trusted_proxies_header"`

	// EnableRejectMalformedRequest return 403 Forbidden if the request is from proxies, but can't extract client IP
	EnableRejectMalformedRequest bool `env:"ENABLE_REJECT_MALFORMED_REQUEST" envDefault:"false" mapstructure:"enable_reject_malformed_request"`
}

Jump to

Keyboard shortcuts

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