corsproxy

package module
v0.0.0-...-6bf4fcb Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: MIT Imports: 12 Imported by: 0

README

corsproxy

Bypass CORS problem in browser runtime with a proxy.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 CORSProxy

type CORSProxy struct {
	// 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 CORSProxy
	// 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)

	Config *Config

	CORSOptions *cors.Options
}

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

CORSProxy 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 NewProxy

func NewProxy(cfg *Config, corsOptions *cors.Options) *CORSProxy

NewProxy returns a new CORSProxy 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 CORSProxy directly with a custom Director policy. FIXME: change comment

func (*CORSProxy) CORSHandler

func (p *CORSProxy) CORSHandler() http.Handler

func (*CORSProxy) ServeHTTP

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

func (*CORSProxy) Start

func (p *CORSProxy) Start()

type Config

type Config struct {
	Port             int `default:"8080"`
	RealIP           string
	RealURLHeader    string `default:"X-Real-URL"`
	NeedTokenAuth    bool
	ProxyTokenHeader string `default:"X-Proxy-Token"`
	ProxyToken       string `secret:"cors_proxy_token"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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