Giter Site home page Giter Site logo

proxy's Introduction

Proxy

GoDoc Go Report Card Build Status Coverage Status

HTTP/HTTPS proxy library with custom dialer, which receives in the context key proxy.ReqParamsK a *proxy.ReqParams instance with the IP that made the request, it's URL and method. It can be served using standard library server or fasthttp server, and has two builtin dialers for dialing with a specific network interface or parent proxy.

Install example server

With Go 1.13 or superior:

git clone [email protected]:lamg/proxy.git
cd proxy/cmd/proxy && go install

Example

This is a proxy that denies all the connections coming from IP addresses different from 127.0.0.1.

package main

import (
	"crypto/tls"
	"fmt"
	"net"
	h "net/http"
	"time"

	alg "github.com/lamg/algorithms"
	"github.com/lamg/proxy"
)

func main() {
	// localhost clients only
	ar, e := newAllowedRanges("127.0.0.1/32")
	if e == nil {
		p := proxy.NewProxy(ar.DialContext)
		server := &h.Server{
			Addr:         ":8080",
			Handler:      p,
			ReadTimeout:  5 * time.Second,
			WriteTimeout: 10 * time.Second,
			// Disable HTTP/2.
			TLSNextProto: make(map[string]func(*h.Server,
				*tls.Conn, h.Handler)),
		}
		e = server.ListenAndServe()
	}
	if e != nil {
		log.Fatal(e)
	}
}

type allowedRanges struct {
	ranges      []*net.IPNet
	timeout     time.Duration
}

func newAllowedRanges(cidrs ...string) (a *allowedRanges, e error) {
	a = &allowedRanges{
		ranges:      make([]*net.IPNet, len(cidrs)),
		timeout:     90 * time.Second,
	}
	ib := func(i int) (b bool) {
		_, a.ranges[i], e = net.ParseCIDR(cidrs[i])
		b = e != nil
		return
	}
	alg.BLnSrch(ib, len(cidrs))
	return
}

func (r *allowedRanges) DialContext(ctx context.Context, network,
	addr string) (c net.Conn, e error) {
	rqp := ctx.Value(proxy.ReqParamsK).(*proxy.ReqParams)
	ip := net.ParseIP(rqp.IP)
	ok, _ := alg.BLnSrch(
		func(i int) bool { return r.ranges[i].Contains(ip) },
		len(r.ranges),
	)
	if !ok {
		e = fmt.Errorf("Client IP '%s' out of range", rqp.IP)
	}
	if e == nil {
		ifd := &proxy.IfaceDialer{Timeout: r.timeout}
		c, e = ifd.Dial(network, addr)
	}
	return
}

proxy's People

Contributors

lamg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.