middleware

package
v0.0.0-...-64e30f5 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const MaxTraceNum = 100

Variables

View Source
var CIDRWhiteList = &cidrWhitelist{
	m: &Middleware{withOptions: NewCIDRWhitelist},
}
View Source
var CloudflareRealIP = &realIP{
	m: &Middleware{withOptions: NewCloudflareRealIP},
}
View Source
var CustomErrorPage = &Middleware{
	before: func(next http.HandlerFunc, w ResponseWriter, r *Request) {
		if !ServeStaticErrorPageFile(w, r) {
			next(w, r)
		}
	},
	modifyResponse: func(resp *Response) error {

		contentType := gphttp.GetContentType(resp.Header)
		if !gphttp.IsSuccess(resp.StatusCode) && (contentType.IsHTML() || contentType.IsPlainText()) {
			errorPage, ok := errorpage.GetErrorPageByStatus(resp.StatusCode)
			if ok {
				errPageLogger.Debugf("error page for status %d loaded", resp.StatusCode)

				io.Copy(io.Discard, resp.Body)
				resp.Body.Close()
				resp.Body = io.NopCloser(bytes.NewReader(errorPage))
				resp.ContentLength = int64(len(errorPage))
				resp.Header.Set("Content-Length", strconv.Itoa(len(errorPage)))
				resp.Header.Set("Content-Type", "text/html; charset=utf-8")
			} else {
				errPageLogger.Errorf("unable to load error page for status %d", resp.StatusCode)
			}
			return nil
		}
		return nil
	},
}
View Source
var Deserialize = U.Deserialize
View Source
var ForwardAuth = &forwardAuth{
	m: &Middleware{withOptions: NewForwardAuthfunc},
}
View Source
var HideXForwarded = &Middleware{
	before: Rewrite(func(req *Request) {
		req.Header.Del("Forwarded")
		req.Header.Del(xForwardedFor)
		req.Header.Del(xForwardedHost)
		req.Header.Del(xForwardedProto)
	}),
}
View Source
var ModifyRequest = &modifyRequest{
	m: &Middleware{withOptions: NewModifyRequest},
}
View Source
var ModifyResponse = &modifyResponse{
	m: &Middleware{withOptions: NewModifyResponse},
}
View Source
var OAuth2 = &oAuth2{
	m: &Middleware{withOptions: NewAuthentikOAuth2},
}
View Source
var RealIP = &realIP{
	m: &Middleware{withOptions: NewRealIP},
}
View Source
var RedirectHTTP = &Middleware{
	before: func(next http.HandlerFunc, w ResponseWriter, r *Request) {
		if r.TLS == nil {
			r.URL.Scheme = "https"
			r.URL.Host = r.URL.Hostname() + ":" + common.ProxyHTTPSPort
			http.Redirect(w, r, r.URL.String(), http.StatusTemporaryRedirect)
			return
		}
		next(w, r)
	},
}
View Source
var SetXForwarded = &Middleware{
	before: Rewrite(func(req *Request) {
		req.Header.Del("Forwarded")
		req.Header.Del(xForwardedFor)
		req.Header.Del(xForwardedHost)
		req.Header.Del(xForwardedProto)
		clientIP, _, err := net.SplitHostPort(req.RemoteAddr)
		if err == nil {
			req.Header.Set(xForwardedFor, clientIP)
		} else {
			req.Header.Set(xForwardedFor, req.RemoteAddr)
		}
		req.Header.Set(xForwardedHost, req.Host)
		if req.TLS == nil {
			req.Header.Set(xForwardedProto, "http")
		} else {
			req.Header.Set(xForwardedProto, "https")
		}
	}),
}

Functions

func All

func All() map[string]*Middleware

func BuildMiddlewaresFromComposeFile

func BuildMiddlewaresFromComposeFile(filePath string) (map[string]*Middleware, E.NestedError)

func BuildMiddlewaresFromYAML

func BuildMiddlewaresFromYAML(data []byte) (middlewares map[string]*Middleware, outErr E.NestedError)

func LoadComposeFiles

func LoadComposeFiles()

func PatchReverseProxy

func PatchReverseProxy(rpName string, rp *ReverseProxy, middlewaresMap map[string]OptionsRaw) (res E.NestedError)

TODO: check conflict or duplicates

func ServeStaticErrorPageFile

func ServeStaticErrorPageFile(w http.ResponseWriter, r *http.Request) bool

Types

type BeforeFunc

type BeforeFunc func(next http.HandlerFunc, w ResponseWriter, r *Request)

func Rewrite

func Rewrite(r RewriteFunc) BeforeFunc

type CloneWithOptFunc

type CloneWithOptFunc func(opts OptionsRaw) (*Middleware, E.NestedError)
type Cookie = http.Cookie

type Error

type Error = E.NestedError
type Header = http.Header

type Middleware

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

func BuildMiddlewareFromChain

func BuildMiddlewareFromChain(name string, chain []*Middleware) *Middleware

TODO: check conflict or duplicates.

func Get

func Get(name string) (middleware *Middleware, ok bool)

func NewAuthentikOAuth2

func NewAuthentikOAuth2(opts OptionsRaw) (*Middleware, E.NestedError)

func NewCIDRWhitelist

func NewCIDRWhitelist(opts OptionsRaw) (*Middleware, E.NestedError)

func NewCloudflareRealIP

func NewCloudflareRealIP(_ OptionsRaw) (*Middleware, E.NestedError)

func NewForwardAuthfunc

func NewForwardAuthfunc(optsRaw OptionsRaw) (*Middleware, E.NestedError)

func NewModifyRequest

func NewModifyRequest(optsRaw OptionsRaw) (*Middleware, E.NestedError)

func NewModifyResponse

func NewModifyResponse(optsRaw OptionsRaw) (*Middleware, E.NestedError)

func NewRealIP

func NewRealIP(opts OptionsRaw) (*Middleware, E.NestedError)

func (*Middleware) AddTraceRequest

func (m *Middleware) AddTraceRequest(msg string, req *Request) *Trace

func (*Middleware) AddTraceResponse

func (m *Middleware) AddTraceResponse(msg string, resp *Response) *Trace

func (*Middleware) AddTracef

func (m *Middleware) AddTracef(msg string, args ...any) *Trace

func (*Middleware) EnableTrace

func (m *Middleware) EnableTrace()

func (*Middleware) Fullname

func (m *Middleware) Fullname() string

func (*Middleware) MarshalJSON

func (m *Middleware) MarshalJSON() ([]byte, error)

func (*Middleware) Name

func (m *Middleware) Name() string

func (*Middleware) String

func (m *Middleware) String() string

func (*Middleware) WithOptionsClone

func (m *Middleware) WithOptionsClone(optsRaw OptionsRaw) (*Middleware, E.NestedError)

type ModifyResponseFunc

type ModifyResponseFunc func(resp *Response) error

type Options

type Options any

type OptionsRaw

type OptionsRaw = map[string]any

type ProxyRequest

type ProxyRequest = gphttp.ProxyRequest

type Request

type Request = http.Request

type Response

type Response = http.Response

type ResponseWriter

type ResponseWriter = http.ResponseWriter

type ReverseProxy

type ReverseProxy = gphttp.ReverseProxy

type RewriteFunc

type RewriteFunc func(req *Request)

type TestResult

type TestResult struct {
	RequestHeaders  http.Header
	ResponseHeaders http.Header
	ResponseStatus  int
	RemoteAddr      string
	Data            []byte
}

type Trace

type Trace struct {
	Time        string            `json:"time,omitempty"`
	Caller      string            `json:"caller,omitempty"`
	URL         string            `json:"url,omitempty"`
	Message     string            `json:"msg"`
	ReqHeaders  map[string]string `json:"req_headers,omitempty"`
	RespHeaders map[string]string `json:"resp_headers,omitempty"`
	RespStatus  int               `json:"resp_status,omitempty"`
	Additional  map[string]any    `json:"additional,omitempty"`
}

func GetAllTrace

func GetAllTrace() []*Trace

func (*Trace) With

func (tr *Trace) With(what string, additional any) *Trace

func (*Trace) WithError

func (tr *Trace) WithError(err error) *Trace

func (*Trace) WithRequest

func (tr *Trace) WithRequest(req *Request) *Trace

func (*Trace) WithResponse

func (tr *Trace) WithResponse(resp *Response) *Trace

type Traces

type Traces []*Trace

Jump to

Keyboard shortcuts

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