webproxy

package module
v0.0.0-...-dbd4f01 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2019 License: MIT Imports: 9 Imported by: 1

README

Documentation

Overview

Package webproxy is a simple http proxy tool.

Refer:

https://github.com/nodejitsu/node-http-proxy
https://github.com/chimurai/http-proxy-middleware
"golang.org/x/net/proxy" -> proxy.SOCKS5()

使用说明:

1. 作为中间件 - 匹配成功就代理请求,否则跳过当前请求。

proxy.New("/api", some options)
proxy.New([]string{"/api", "/v1/api"}, some options)

2. 直接作为路由path handler - 代理此路由下的所有请求(相当于作为中间件时,ctx为一个url path)

router.GET("/api", proxy.All(some options))
http.HandleFunc("/api", proxy.All(some options))

参数说明:

proxy.New() 接收两个参数:第一个是要匹配的信息,第二个是一些选项设置

关于参数 'ctx',它允许为:

empty:
	nil - matches any path, all requests will be proxied.
path matching(string - a URL path, support wildcard):
	"/", "**" - matches any path, all requests will be proxied.
	"/api" - matches paths starting with /api
	"**/*.html" - matches any path which ends with .html
	"/*.html" - matches paths directly under path-absolute
	"!**/bad.json" - exclusion
multiple path matching([]string - multi URL path, support wildcard).
	[]string{"/api", "/v1/api", "/some/**/special-api"}
custom validate func:
	FilterFunc - must be type of FilterFunc. return True, proxy current request

Index

Constants

View Source
const (
	CtxIsEmpty   = "empty"
	CtxIsString  = "string"
	CtxIsStrings = "strings"
	CtxIsFilter  = "func"
)

the Proxy ctx data type name

Variables

This section is empty.

Functions

func MultiHostReverseProxy

func MultiHostReverseProxy(targets ...*url.URL) *httputil.ReverseProxy

MultiHostReverseProxy create a global reverse proxy. usage:

rp := MultiHostReverseProxy(&url.URL{
	Scheme: "http",
	Host:   "localhost:9091",
}, &url.URL{
	Scheme: "http",
	Host:   "localhost:9092",
})
log.Fatal(http.ListenAndServe(":9090", rp))

Types

type FilterFunc

type FilterFunc func(path string, r *http.Request) bool

FilterFunc custom filter to check if it should be proxy or not

type Options

type Options struct {
	// open debug
	Debug bool
	// WS enable webSocket proxy
	WS bool
	// Target url string. eg. "http://www.example.org"
	// Notice:
	// 	Target and Forward cannot be both missing
	Target string
	// Forward url string.
	Forward string
	// IgnorePath specify whether you want to ignore the proxy path of the incoming request. Default: false
	IgnorePath bool
	// ChangeOrigin changes the origin of the host header to the target URL. Default: false
	// for vhosted sites, changes host header to match to target's host
	ChangeOrigin bool
	// Auth is basic authentication i.e. 'user:password' to compute an Authorization header.
	Auth string
	// WS bool
	// PathRewrite url path rewrite
	// 	{
	//      '^/api/old-path' : '/api/new-path',     // rewrite path
	//      '^/api/remove/path' : '/path'           // remove base path
	// 		'^/' : '/basePath/'  					// add base path
	//   },
	PathRewrite map[string]string
	LogLevel    int
	// LogOutput
	// Example:
	// 	LogOutput = os.Stdout
	// 	LogOutput = new(bytes.Buffer)
	// 	LogOutput, _ = os.OpenFile("proxy.log", os.O_RDWR|os.O_CREATE, os.ModePerm)
	LogOutput io.Writer
	//
	Events map[string]func(args ...interface{}) error
	// Routes table, if match success, will override Target.
	//
	// Example:
	// 	{
	// 		// when request.headers.host == 'dev.localhost:3000',
	// 		// override target 'http://www.example.org' to 'http://localhost:8000'
	// 		"dev.localhost:3000" : "http://localhost:8000"
	// 	}
	Routes map[string]string
}

Options for proxy.

type Proxy

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

Proxy definition

func All

func All(opts Options) *Proxy

All requests will be proxy

func New

func New(ctx interface{}, opts Options) *Proxy

New a proxy instance.

func Target

func Target(url string, opts ...Options) *Proxy

Target requests will be proxy to the target URL

func (*Proxy) HandlerFunc

func (p *Proxy) HandlerFunc() http.HandlerFunc

HandlerFunc return http.HandlerFunc

func (*Proxy) Middleware

func (p *Proxy) Middleware(h http.Handler) http.Handler

Middleware of the interface http.Handler

func (*Proxy) Options

func (p *Proxy) Options() Options

Options get

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handle request

func (*Proxy) WEB

func (p *Proxy) WEB()

WEB request proxy

func (*Proxy) WS

func (p *Proxy) WS()

WS request proxy

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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