vhost

package
v0.35.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NotFound = `` /* 489-byte string literal not displayed */

)

Variables

View Source
var (
	ErrNoDomain = errors.New("no such domain")
)
View Source
var (
	ErrRouterConfigConflict = errors.New("router config conflict")
)
View Source
var (
	NotFoundPagePath = ""
)

Functions

func GetHTTPSHostname

func GetHTTPSHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error)

Types

type BufferPool

type BufferPool interface {
	Get() []byte
	Put([]byte)
}

A BufferPool is an interface for getting and returning temporary byte slices for use by io.CopyBuffer.

type ByLocation

type ByLocation []*Router

sort by location

func (ByLocation) Len

func (a ByLocation) Len() int

func (ByLocation) Less

func (a ByLocation) Less(i, j int) bool

func (ByLocation) Swap

func (a ByLocation) Swap(i, j int)

type CreateConnFunc

type CreateConnFunc func(remoteAddr string) (net.Conn, error)

type HTTPReverseProxy

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

func NewHTTPReverseProxy

func NewHTTPReverseProxy(option HTTPReverseProxyOptions, vhostRouter *Routers) *HTTPReverseProxy

func (*HTTPReverseProxy) CheckAuth

func (rp *HTTPReverseProxy) CheckAuth(domain, location, user, passwd string) bool

func (*HTTPReverseProxy) CreateConnection

func (rp *HTTPReverseProxy) CreateConnection(domain string, location string, remoteAddr string) (net.Conn, error)

CreateConnection create a new connection by route config

func (*HTTPReverseProxy) GetHeaders

func (rp *HTTPReverseProxy) GetHeaders(domain string, location string) (headers map[string]string)

func (*HTTPReverseProxy) GetRealHost

func (rp *HTTPReverseProxy) GetRealHost(domain string, location string) (host string)

func (*HTTPReverseProxy) GetRouteConfig

func (rp *HTTPReverseProxy) GetRouteConfig(domain string, location string) *RouteConfig

func (*HTTPReverseProxy) Register

func (rp *HTTPReverseProxy) Register(routeCfg RouteConfig) error

Register register the route config to reverse proxy reverse proxy will use CreateConnFn from routeCfg to create a connection to the remote service

func (*HTTPReverseProxy) ServeHTTP

func (rp *HTTPReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

func (*HTTPReverseProxy) UnRegister

func (rp *HTTPReverseProxy) UnRegister(domain string, location string)

UnRegister unregister route config by domain and location

type HTTPReverseProxyOptions

type HTTPReverseProxyOptions struct {
	ResponseHeaderTimeoutS int64
}

type HTTPSMuxer

type HTTPSMuxer struct {
	*Muxer
}

func NewHTTPSMuxer

func NewHTTPSMuxer(listener net.Listener, timeout time.Duration) (*HTTPSMuxer, error)

type Listener

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

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

func (*Listener) Close

func (l *Listener) Close() error

func (*Listener) Name

func (l *Listener) Name() string

type Muxer

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

func NewMuxer

func NewMuxer(listener net.Listener, vhostFunc muxFunc, authFunc httpAuthFunc, successFunc successFunc, rewriteFunc hostRewriteFunc, timeout time.Duration) (mux *Muxer, err error)

func (*Muxer) Listen

func (v *Muxer) Listen(ctx context.Context, cfg *RouteConfig) (l *Listener, err error)

listen for a new domain name, if rewriteHost is not empty and rewriteFunc is not nil then rewrite the host header to rewriteHost

type ReverseProxy

type ReverseProxy struct {
	// Director must be a function which modifies
	// the request into a new request to be sent
	// using Transport. Its response is then copied
	// back to the original client unmodified.
	// Director must not access the provided Request
	// after returning.
	Director func(*http.Request)

	// The transport used to perform proxy requests.
	// If nil, http.DefaultTransport is used.
	Transport http.RoundTripper

	// FlushInterval specifies the flush interval
	// to flush to the client while copying the
	// response body.
	// If zero, no periodic flushing is done.
	// A negative value means to flush immediately
	// after each write to the client.
	// The FlushInterval is ignored when ReverseProxy
	// recognizes a response as a streaming response, or
	// if its ContentLength is -1; for such responses, writes
	// are flushed to the client immediately.
	FlushInterval time.Duration

	// ErrorLog specifies an optional logger for errors
	// that occur when attempting to proxy the request.
	// If nil, logging is done via the log package's standard logger.
	ErrorLog *log.Logger

	// BufferPool optionally specifies a buffer pool to
	// get byte slices for use by io.CopyBuffer when
	// copying HTTP response bodies.
	BufferPool BufferPool

	// ModifyResponse is an optional function that modifies the
	// Response from the backend. It is called if the backend
	// returns a response at all, with any HTTP status code.
	// If the backend is unreachable, the optional ErrorHandler is
	// called without any call to ModifyResponse.
	//
	// If ModifyResponse returns an error, ErrorHandler is called
	// with its error value. If ErrorHandler is nil, its default
	// implementation is used.
	ModifyResponse func(*http.Response) error

	// ErrorHandler is an optional function that handles errors
	// reaching the backend or errors from ModifyResponse.
	//
	// If nil, the default is to log the provided error and return
	// a 502 Status Bad Gateway response.
	ErrorHandler func(http.ResponseWriter, *http.Request, error)
}

ReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client.

ReverseProxy by default sets the client IP as the value of the X-Forwarded-For header.

If an X-Forwarded-For header already exists, the client IP is appended to the existing values. As a special case, if the header exists in the Request.Header map but has a nil value (such as when set by the Director func), the X-Forwarded-For header is not modified.

To prevent IP spoofing, be sure to delete any pre-existing X-Forwarded-For header coming from the client or an untrusted proxy.

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy

NewSingleHostReverseProxy returns a new ReverseProxy that routes URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir. NewSingleHostReverseProxy does not rewrite the Host header. To rewrite Host headers, use ReverseProxy directly with a custom Director policy.

func (*ReverseProxy) ServeHTTP

func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type RouteConfig

type RouteConfig struct {
	Domain      string
	Location    string
	RewriteHost string
	Username    string
	Password    string
	Headers     map[string]string

	CreateConnFn CreateConnFunc
}

RouteConfig is the params used to match HTTP requests

type RouteInfo

type RouteInfo string
const (
	RouteInfoURL    RouteInfo = "url"
	RouteInfoHost   RouteInfo = "host"
	RouteInfoRemote RouteInfo = "remote"
)

type Router

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

type Routers

type Routers struct {
	RouterByDomain map[string][]*Router
	// contains filtered or unexported fields
}

func NewRouters

func NewRouters() *Routers

func (*Routers) Add

func (r *Routers) Add(domain, location string, payload interface{}) error

func (*Routers) Del

func (r *Routers) Del(domain, location string)

func (*Routers) Get

func (r *Routers) Get(host, path string) (vr *Router, exist bool)

Jump to

Keyboard shortcuts

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