Categorygithub.com/coreos/goproxy
modulepackage
0.0.0-20190513173959-f8dc2d7ba04e
Repository: https://github.com/coreos/goproxy.git
Documentation: pkg.go.dev

# README

Introduction

GoDoc Join the chat at https://gitter.im/elazarl/goproxy

Package goproxy provides a customizable HTTP proxy library for Go (golang),

It supports regular HTTP proxy, HTTPS through CONNECT, and "hijacking" HTTPS connection using "Man in the Middle" style attack.

The intent of the proxy, is to be usable with reasonable amount of traffic yet, customizable and programable.

The proxy itself is simply a net/http handler.

In order to use goproxy, one should set their browser to use goproxy as an HTTP proxy. Here is how you do that in Chrome and in Firefox.

For example, the URL you should use as proxy when running ./bin/basic is localhost:8080, as this is the default binding for the basic proxy.

Mailing List

New features would be discussed on the mailing list before their development.

Latest Stable Release

Get the latest goproxy from gopkg.in/elazarl/goproxy.v1.

Why not Fiddler2?

Fiddler is an excellent software with similar intent. However, Fiddler is not as customable as goproxy intend to be. The main difference is, Fiddler is not intended to be used as a real proxy.

A possible use case that suits goproxy but not Fiddler, is, gathering statisitics on page load times for a certain website over a week. With goproxy you could ask all your users to set their proxy to a dedicated machine running a goproxy server. Fiddler is a GUI app not designed to be ran like a server for multiple users.

A taste of goproxy

To get a taste of goproxy, a basic HTTP/HTTPS transparent proxy

package main

import (
    "github.com/elazarl/goproxy"
    "log"
    "net/http"
)

func main() {
    proxy := goproxy.NewProxyHttpServer()
    proxy.Verbose = true
    log.Fatal(http.ListenAndServe(":8080", proxy))
}

This line will add X-GoProxy: yxorPoG-X header to all requests sent through the proxy

proxy.OnRequest().DoFunc(
    func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) {
        r.Header.Set("X-GoProxy","yxorPoG-X")
        return r,nil
    })

DoFunc will process all incoming requests to the proxy. It will add a header to the request and return it. The proxy will send the modified request.

Note that we returned nil value as the response. Have we returned a response, goproxy would have discarded the request and sent the new response to the client.

In order to refuse connections to reddit at work time

proxy.OnRequest(goproxy.DstHostIs("www.reddit.com")).DoFunc(
    func(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) {
        if h,_,_ := time.Now().Clock(); h >= 8 && h <= 17 {
            return r,goproxy.NewResponse(r,
                    goproxy.ContentTypeText,http.StatusForbidden,
                    "Don't waste your time!")
        }
        return r,nil
})

DstHostIs returns a ReqCondition, that is a function receiving a Request and returning a boolean we will only process requests that matches the condition. DstHostIs("www.reddit.com") will return a ReqCondition accepting only requests directed to "www.reddit.com".

DoFunc will recieve a function that will preprocess the request. We can change the request, or return a response. If the time is between 8:00am and 17:00pm, we will neglect the request, and return a precanned text response saying "do not waste your time".

See additional examples in the examples directory.

What's New

  1. Ability to Hijack CONNECT requests. See the eavesdropper example
  2. Transparent proxy support for http/https including MITM certificate generation for TLS. See the transparent example.

License

I put the software temporarily under the Go-compatible BSD license, if this prevents someone from using the software, do let mee know and I'll consider changing it.

At any rate, user feedback is very important for me, so I'll be delighted to know if you're using this package.

Beta Software

I've received a positive feedback from a few people who use goproxy in production settings. I believe it is good enough for usage.

I'll try to keep reasonable backwards compatability. In case of a major API change, I'll change the import path.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

ContentTypeIs returns a RespCondition testing whether the HTTP response has Content-Type header equal to one of the given strings.
DstHostIs returns a ReqCondition testing wether the host in the request url is the given string.
HandleBytes will return a RespHandler that read the entire body of the request to a byte array in memory, would run the user supplied f function on the byte arra, and will replace the body of the original response with the resulting byte array.
No description provided by the author
New proxy server, logs to StdErr by default.
Will generate a valid http response to the given request the response will have the given contentType, and http status.
No description provided by the author
Not returns a ReqCondition negating the given ReqCondition.
ReqHostIs returns a ReqCondition, testing whether the host to which the request is directed to equal to one of the given strings.
ReqHostMatches returns a ReqCondition, testing whether the host to which the request was directed to matches any of the given regular expressions.
SrcIpIs returns a ReqCondition testing whether the source IP of the request is one of the given strings.
Alias for NewResponse(r,ContentTypeText,http.StatusAccepted,text).
No description provided by the author
UrlHasPrefix returns a ReqCondition checking wether the destination URL the proxy client has requested has the given prefix, with or without the host.
UrlIs returns a ReqCondition, testing whether or not the request URL is one of the given strings with or without the host prefix.
UrlMatches returns a ReqCondition testing whether the destination URL of the request matches the given regexp, with or without prefix.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

AlwaysMitm is a HttpsHandler that always eavesdrop https connections, for example to eavesdrop all https connections to www.google.com, we can use proxy.OnRequest(goproxy.ReqHostIs("www.google.com")).HandleConnect(goproxy.AlwaysMitm).
AlwaysReject is a HttpsHandler that drops any CONNECT request, for example, this code will disallow connections to hosts on any other port than 443 proxy.OnRequest(goproxy.Not(goproxy.ReqHostMatches(regexp.MustCompile(":443$"))).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IsLocalHost checks whether the destination host is explicitly local host (buggy, there can be IPv6 addresses it doesn't catch).
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
ProxyConds is used to aggregate RespConditions for a ProxyHttpServer.
ProxyCtx is the Proxy context, contains useful information about every request.
The basic proxy type.
ReqProxyConds aggregate ReqConditions for a ProxyHttpServer.

# Interfaces

When a client send a CONNECT request to a host, the request is filtered through all the HttpsHandlers the proxy has, and if one returns true, the connection is sniffed using Man in the Middle attack.
ReqCondition.HandleReq will decide whether or not to use the ReqHandler on an HTTP request before sending it to the remote server.
ReqHandler will "tamper" with the request coming to the proxy server If Handle returns req,nil the proxy will send the returned request to the destination server.
RespCondition.HandleReq will decide whether or not to use the RespHandler on an HTTP response before sending it to the proxy client.
after the proxy have sent the request to the destination server, it will "filter" the response through the RespHandlers it has.
No description provided by the author

# Type aliases

No description provided by the author
A wrapper that would convert a function to a HttpsHandler interface type.
A wrapper that would convert a function to a ReqHandler interface type.
A wrapper that would convert a function to a RespHandler interface type.
ReqConditionFunc.HandleReq(req,ctx) <=> ReqConditionFunc(req,ctx).
RespConditionFunc.HandleResp(resp,ctx) <=> RespConditionFunc(resp,ctx).
No description provided by the author