Skip to content

Commit

Permalink
Merge pull request #5 from go-httpproxy/develop
Browse files Browse the repository at this point in the history
v1.4
  • Loading branch information
orkunkaraduman authored Mar 2, 2018
2 parents 56cce93 + ebffeab commit 981f79e
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.cache
*.swap
*.swp
*.temp
*.tmp

*.o
*.a
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# Please keep the list sorted.

Orkun Karaduman <[email protected]>
Yasin Özel <[email protected]>
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Go HTTP proxy library
# Go HTTP proxy server library

[![GoDoc](https://godoc.org/github.com/go-httpproxy/httpproxy?status.svg)](https://godoc.org/github.com/go-httpproxy/httpproxy)

Expand All @@ -9,6 +9,14 @@ attack.
It's easy to use. `httpproxy.Proxy` implements `Handler` interface of `net/http`
package to offer `http.ListenAndServe` function.

## Installing

```sh
go get -u github.com/go-httpproxy/httpproxy
# or
go get -u gopkg.in/httpproxy.v1
```

## Usage

Library has two significant structs: Proxy and Context.
Expand All @@ -33,29 +41,29 @@ type Proxy struct {
// User data to use free.
UserData interface{}

// Error handler.
// Error callback.
OnError func(ctx *Context, where string, err *Error, opErr error)

// Accept handler. It greets proxy request like ServeHTTP function of
// Accept callback. It greets proxy request like ServeHTTP function of
// http.Handler.
// If it returns true, stops processing proxy request.
OnAccept func(ctx *Context, w http.ResponseWriter, r *http.Request) bool

// Auth handler. If you need authentication, set this handler.
// Auth callback. If you need authentication, set this callback.
// If it returns true, authentication succeeded.
OnAuth func(ctx *Context, authType string, user string, pass string) bool

// Connect handler. It sets connect action and new host.
// Connect callback. It sets connect action and new host.
// If len(newhost) > 0, host changes.
OnConnect func(ctx *Context, host string) (ConnectAction ConnectAction,
newHost string)

// Request handler. It greets remote request.
// Request callback. It greets remote request.
// If it returns non-nil response, stops processing remote request.
OnRequest func(ctx *Context, req *http.Request) (resp *http.Response)

// Response handler. It greets remote response.
// Remote response sends after this handler.
// Response callback. It greets remote response.
// Remote response sends after this callback.
OnResponse func(ctx *Context, req *http.Request, resp *http.Response)

// If ConnectAction is ConnectMitm, it sets chunked to Transfer-Encoding.
Expand Down Expand Up @@ -104,7 +112,11 @@ type Context struct {
}
```

### Simple code
## Examples

For more examples, examples/

### examples/go-httpproxy-simple

```go
package main
Expand Down
1 change: 1 addition & 0 deletions casigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func SignHosts(ca tls.Certificate, hosts []string) (*tls.Certificate, error) {
BasicConstraintsValid: true,
}
for _, h := range hosts {
h = stripPort(h)
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
Expand Down
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func (ctx *Context) doConnect(w http.ResponseWriter, r *http.Request) (w2 http.R
ctx.ConnectReq = r
ctx.ConnectAction = ConnectProxy
host := r.URL.Host
if !hasPort.MatchString(host) {
host += ":80"
}
if ctx.Prx.OnConnect != nil {
var newHost string
ctx.ConnectAction, newHost = ctx.onConnect(host)
Expand Down Expand Up @@ -259,7 +262,7 @@ func (ctx *Context) doConnect(w http.ResponseWriter, r *http.Request) (w2 http.R
remoteConn.Close()
case ConnectMitm:
tlsConfig := &tls.Config{}
cert := ctx.Prx.signer.SignHost(stripPort(host))
cert := ctx.Prx.signer.SignHost(host)
if cert == nil {
hijConn.Close()
ctx.doError("Connect", ErrTLSSignHost, err)
Expand Down
1 change: 0 additions & 1 deletion demo/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion demo/simple/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions examples/go-httpproxy-demo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/go-httpproxy-demo
File renamed without changes.
1 change: 1 addition & 0 deletions examples/go-httpproxy-simple/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/go-httpproxy-simple
File renamed without changes.
14 changes: 7 additions & 7 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ type Proxy struct {
// User data to use free.
UserData interface{}

// Error handler.
// Error callback.
OnError func(ctx *Context, where string, err *Error, opErr error)

// Accept handler. It greets proxy request like ServeHTTP function of
// Accept callback. It greets proxy request like ServeHTTP function of
// http.Handler.
// If it returns true, stops processing proxy request.
OnAccept func(ctx *Context, w http.ResponseWriter, r *http.Request) bool

// Auth handler. If you need authentication, set this handler.
// Auth callback. If you need authentication, set this callback.
// If it returns true, authentication succeeded.
OnAuth func(ctx *Context, authType string, user string, pass string) bool

// Connect handler. It sets connect action and new host.
// Connect callback. It sets connect action and new host.
// If len(newhost) > 0, host changes.
OnConnect func(ctx *Context, host string) (ConnectAction ConnectAction,
newHost string)

// Request handler. It greets remote request.
// Request callback. It greets remote request.
// If it returns non-nil response, stops processing remote request.
OnRequest func(ctx *Context, req *http.Request) (resp *http.Response)

// Response handler. It greets remote response.
// Remote response sends after this handler.
// Response callback. It greets remote response.
// Remote response sends after this callback.
OnResponse func(ctx *Context, req *http.Request, resp *http.Response)

// If ConnectAction is ConnectMitm, it sets chunked to Transfer-Encoding.
Expand Down

0 comments on commit 981f79e

Please sign in to comment.