proxy

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: MIT Imports: 16 Imported by: 13

README

Proxy - Make Reverse Proxy easier to use

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get -u github.com/go-zoox/proxy

Quick Start

package main

import (
	"fmt"
	"net/http"

	"github.com/go-zoox/proxy"
)

func main() {
	fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")

	http.ListenAndServe(":9999", proxy.New(&proxy.Config{
		OnRequest: func(req *http.Request) error {
			req.URL.Host = "127.0.0.1:8080"
			return nil
		},
	}))
}

// visit http://127.0.0.1:9999/ip => http://127.0.0.1:8080/ip
// curl -v http://127.0.0.1:9999/ip

Inspiration

  • Go httputil.ReverseProxy

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

View Source
const HeaderXForwardedFor = "X-Forwarded-For"

HeaderXForwardedFor is the header name for X-Forwarded-For

View Source
const HeaderXForwardedHost = "X-Forwarded-Host"

HeaderXForwardedHost is the header name for X-Forwarded-Host

View Source
const HeaderXForwardedPort = "X-Forwarded-Port"

HeaderXForwardedPort is the header name for X-Forwarded-Port

View Source
const HeaderXForwardedProto = "X-Forwarded-Proto"

HeaderXForwardedProto is the header name for X-Forwarded-Proto

Variables

View Source
var Version = "1.1.0"

Version is the version of this package.

Functions

func NewHTTPError

func NewHTTPError(status int, message string) error

NewHTTPError creates a new HTTPError.

func ParseHostPort

func ParseHostPort(rawHost string) (string, string)

ParseHostPort parses host and port from a string in the form host[:port].

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 Config

type Config struct {
	// IsAnonymouse is a flag to indicate whether the proxy is anonymouse.
	//	which means the proxy will not add headers:
	//		X-Forwarded-For
	//		X-Forwarded-Proto
	//		X-Forwarded-Host
	//		X-Forwarded-Port
	// Default is false.
	IsAnonymouse bool

	// OnRequest is a function that will be called before the request is sent.
	OnRequest func(req *http.Request) error

	// OnResponse is a function that will be called after the response is received.
	OnResponse func(res *http.Response) error

	// OnError is a function that will be called when an error occurs.
	OnError func(err error, rw http.ResponseWriter, req *http.Request)
}

Config is the configuration for the Proxy.

type HTTPError

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

HTTPError is an error that wraps an HTTP status code.

func (*HTTPError) Error

func (h *HTTPError) Error() string

Error returns the error message.

func (*HTTPError) Status

func (h *HTTPError) Status() int

Status returns the HTTP status code.

type Proxy

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

Proxy is a Powerful HTTP Proxy, inspired by Go Reverse Proxy.

func New

func New(cfg *Config) *Proxy

New creates a new Proxy.

func NewSingleTarget

func NewSingleTarget(target string, cfg ...*SingleTargetConfig) *Proxy

NewSingleTarget creates a new SingleTarget Proxy.

func (*Proxy) ServeHTTP

func (r *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP is the entry point for the proxy.

type SingleTargetConfig

type SingleTargetConfig struct {
	Rewrites        map[string]string
	Scheme          string
	Query           url.Values
	RequestHeaders  http.Header
	ResponseHeaders http.Header
	OnRequest       func(req *http.Request) error
	OnResponse      func(res *http.Response) error
	//
	IsAnonymouse bool
}

SingleTargetConfig is the configuration for SingleTarget.

Directories

Path Synopsis
example
https command
simple command
single command
utils

Jump to

Keyboard shortcuts

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