httpproxy

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: MIT Imports: 8 Imported by: 0

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/http-proxy

Quick Start

package main

import (
	"fmt"
	"net/http"

	"github.com/go-zoox/http-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

Best Practice

1. Single Host => All traffic to a single target with path
package main

import (
	"fmt"
	"net/http"

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

func main() {
	target := "https://httpbin.org"

	fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
	http.ListenAndServe(":9999", proxy.NewSingleHost(target))
}
2. Single Host => All traffic to a single target with rewrite
package main

import (
	"fmt"
	"net/http"

	"github.com/go-zoox/http-proxy"
	"github.com/go-zoox/http-proxy/utils/rewriter"
)

func main() {
	target := "https://httpbin.org"

	fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
	http.ListenAndServe(":9999", proxy.NewSingleHost(target, &proxy.SingleHostConfig{
		Rewrites: rewriter.Rewriters{
			{
				From: "/api/ip",
				To:   "/ip",
			},
			{
				From: "/api/headers",
				To:   "/headers",
			},
			{
				From: "/api/v2/(.*)",
				To:   "/$1",
			},
		},
	}))
}
3. Multiple Hosts => All traffic to multiple targets
package main

import (
	"fmt"
	"net/http"

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

func main() {
	fmt.Println("Starting proxy at http://127.0.0.1:9999 ...")
	
	http.ListenAndServe(":9999", proxy.NewMultiHosts(&proxy.MultiHostsConfig{
		Routes: []proxy.MultiHostsRoute{
			{
				Host: "httpbin1.go-zoox.work",
				Backend: proxy.MultiHostsRouteBackend{
					ServiceProtocol: "https",
					ServiceName:     "httpbin.zcorky.com",
					ServicePort:     443,
				},
			},
			{
				Host: "httpbin2.go-zoox.work",
				Backend: proxy.MultiHostsRouteBackend{
					ServiceProtocol: "https",
					ServiceName:     "httpbin.org",
					ServicePort:     443,
				},
			},
		},
	}))
}

Inspiration

  • Go httputil.ReverseProxy

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "0.0.2"

Version is the version of this package.

Functions

func EqualFold

func EqualFold(s, t string) bool

EqualFold is strings.EqualFold, ASCII only. It reports whether s and t are equal, ASCII-case-insensitively.

Types

type Config

type Config struct {
	Port int
	//
	Username string
	Password string
}

type HTTPProxy

type HTTPProxy interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request)
	Run() error
}

func New

func New(opts ...func(cfg *Config)) HTTPProxy

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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