go_http_proxy

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2023 License: MIT Imports: 11 Imported by: 0

README

go-http-proxy

go-http-proxy

example


import (
	"fmt"
	go_http_proxy "github.com/muzin/go-http-proxy"
	"net/http"
	"strings"
)

type MyHttpHandler2 struct {
	proxyServer *go_http_proxy.ProxyServer
}
func (this *MyHttpHandler2) ServeHTTP(resp http.ResponseWriter, req *http.Request){
	this.proxyServer.Web(resp, req)
}

func main() {

	options := go_http_proxy.NewProxyServerOptions()
	options.Target = "http://localhost:19001"

	options.Headers["X-Clover-Proxy"] = "true"

	proxyServer := go_http_proxy.NewProxyServer(options)

	proxyServer.OnProxyError(func(resp http.ResponseWriter, req *http.Request, err error) {
		// 如果 连接被拒绝 , 跳转到
		if strings.HasSuffix(err.Error(), "connect: connection refused") {
			resp.Header().Add("Location", "http://baidu.com")
			resp.WriteHeader(http.StatusFound)
		}
	})

	proxyServer.Listen(19000, "0.0.0.0")

	fmt.Println("1")

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetupOutgoing

func SetupOutgoing()

Copies the right headers from `options` and `req` to `outgoing` which is then used to fire the proxied request.

Examples:

common.setupOutgoing(outgoing, options, req)
// => { host: ..., hostname: ...}

@param {Object} Outgoing Base object to be filled with required properties

@param {Object} Options Config object passed to the proxy

@param {ClientRequest} Req Request Object

@param {String} Forward String to select forward or target

@return {Object} Outgoing Object with all required properties set

@api private

Types

type DefaultHttpProxyHandler

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

func (*DefaultHttpProxyHandler) ServeHTTP

func (this *DefaultHttpProxyHandler) ServeHTTP(response http.ResponseWriter, request *http.Request)

func (*DefaultHttpProxyHandler) SetWebHandler

func (this *DefaultHttpProxyHandler) SetWebHandler(f func(resp http.ResponseWriter, request *http.Request))

type ProxyServer

type ProxyServer struct {
	events.EventEmitter
	// contains filtered or unexported fields
}

func NewProxyServer

func NewProxyServer(options *ProxyServerOptions) *ProxyServer

func (*ProxyServer) GetOptions

func (this *ProxyServer) GetOptions() *ProxyServerOptions

func (*ProxyServer) Listen

func (this *ProxyServer) Listen(port int, hostname string) error

func (*ProxyServer) OnClose

func (this *ProxyServer) OnClose(listener func(...interface{}))

OnClose

@param listener func(throwable try.Throwable)

func (*ProxyServer) OnError

func (this *ProxyServer) OnError(listener func(...interface{}))

OnError

@param listener func(throwable try.Throwable)

func (*ProxyServer) OnProxyError

func (this *ProxyServer) OnProxyError(listener func(resp http.ResponseWriter,
	req *http.Request,
	err error))

func (*ProxyServer) OnProxyReq

func (this *ProxyServer) OnProxyReq(listener func(proxyReq *http.Request,
	request *http.Request,
	response http.ResponseWriter,
	options *ProxyServerOptions,
))

OnProxyReq

@param listener func(listener func(proxyReq *http.Request, request *http.Request, response http.ResponseWriter, options *ProxyServerOptions)

func (*ProxyServer) OnProxyReqWs

func (this *ProxyServer) OnProxyReqWs(listener func(proxyReq *http.Request,
	request *http.Request,
	response http.ResponseWriter,
	options *ProxyServerOptions,
))

OnProxyReqWs

@param listener func(listener func(proxyReq *http.Request, request *http.Request, response http.ResponseWriter, options *ProxyServerOptions)

func (*ProxyServer) OnProxyResp

func (this *ProxyServer) OnProxyResp(listener func(proxyResp *http.Response))

OnProxyResp

@param listener func(listener func(proxyReq *http.Request, request *http.Request, response http.ResponseWriter, options *ProxyServerOptions)

func (*ProxyServer) RefreshOptions

func (this *ProxyServer) RefreshOptions()

func (*ProxyServer) Web

func (this *ProxyServer) Web(response http.ResponseWriter, request *http.Request, args ...interface{})

web proxy

@param response http.ResponseWriter

@param request *http.Request

@param extOptions *ProxyServerOptions

type ProxyServerOptions

type ProxyServerOptions struct {

	// url string to be parsed with the url module
	Target string

	// object to be passed to https.createServer()
	Ssl *ProxyServerOptions

	// true/false, Default: true adds x-forward headers
	Xfwd bool

	// true/false, Default: false  verify SSL certificate
	Secure bool

	// Basic authentication i.e. 'user:password' to compute an Authorization header
	Auth string

	// rewrites domain of `set-cookie` headers
	CookieDomainRewrite map[string]string

	// rewrites path of `set-cookie` headers
	CookiePathRewrite map[string]string

	// object with extra headers to be added to target requests
	Headers map[string]string

	// ProxyTimeout Default: 60s
	ProxyTimeout time.Duration

	// KeepAlive Default: 60s
	KeepAlive time.Duration

	// Maximum idle connections default: 100
	MaxIdleConns int

	// Idle Conn Timeout default: 90s
	IdleConnTimeout time.Duration

	// Idle Conn Timeout default: 10s
	TLSHandshakeTimeout time.Duration

	// Idle Conn Timeout default: 3s
	ExpectContinueTimeout time.Duration

	// Disable Compression default: true
	DisableCompression bool

	// true/false, Default: true - specify whether you want to follow redirects
	FollowRedirects bool
}

ProxyServerOptions

func NewProxyServerOptions

func NewProxyServerOptions() *ProxyServerOptions

type ProxyType

type ProxyType string
const (
	WS ProxyType = "ws"

	WEB ProxyType = "web"
)

func (ProxyType) String

func (this ProxyType) String() string

Jump to

Keyboard shortcuts

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