reverseproxy

package module
v0.0.2-0...-266bba3 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2017 License: MIT Imports: 8 Imported by: 0

README

Introduction

A minimalist proxy library for go, inspired by net/http/httputil and add support for HTTPS using HTTP Tunnel

Support cancels an in-flight request by closing it's connection

Installation

go get github.com/cssivision/reverseproxy

Usage

A simple proxy

package main

import (
    "net/http"
    "net/url"
    "github.com/cssivision/reverseproxy"
)

func main() {
    http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        path, err := url.Parse("https://github.com")
        if err != nil {
            panic(err)
            return
        }
        proxy := reverseproxy.NewReverseProxy(path)
        proxy.ServeHTTP(w, r)
    }))
}

Use as a proxy server

To use proxy server, you should set browser to use the proxy server as an HTTP proxy.

package main

import (
    "net/http"
    "net/url"
    "github.com/cssivision/reverseproxy"
)

func main() {
    http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        path, err := url.Parse("http://" + r.Host)
        if err != nil {
            panic(err)
            return
        }

        proxy := reverseproxy.NewReverseProxy(path)
        proxy.ServeHTTP(w, r)

        // Specific for HTTP and HTTPS
        // if r.Method == "CONNECT" {
        //     proxy.ProxyHTTPS(w, r)
        // } else {
        //     proxy.ProxyHTTP(w, r)
        // }
    }))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReverseProxy

type ReverseProxy struct {
	// Set the timeout of the proxy server, default is 5 minutes
	Timeout time.Duration

	// 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.
	// default is http.DefaultTransport.
	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.
	FlushInterval time.Duration

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

	// ModifyResponse is an optional function that
	// modifies the Response from the backend.
	// If it returns an error, the proxy returns a StatusBadGateway error.
	ModifyResponse func(*http.Response) error
}

ReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client, support http, also support https tunnel using http.hijacker

func NewReverseProxy

func NewReverseProxy(target *url.URL) *ReverseProxy

NewReverseProxy 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. if the target's query is a=10 and the incoming request's query is b=100, the target's request's query will be a=10&b=100. NewReverseProxy does not rewrite the Host header. To rewrite Host headers, use ReverseProxy directly with a custom Director policy.

func (*ReverseProxy) ProxyHTTP

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

func (*ReverseProxy) ProxyHTTPS

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

func (*ReverseProxy) ServeHTTP

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

Jump to

Keyboard shortcuts

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