Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
orkunkaraduman committed Feb 4, 2018
1 parent 63addc4 commit 9455f5e
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,78 @@ type Context struct {
}
```

### Demo code

```go
package main

import (
"log"
"net/http"

"github.com/go-httpproxy/httpproxy"
)

func OnError(ctx *httpproxy.Context, when string,
err *httpproxy.Error, opErr error) {
// Log errors.
log.Printf("ERR: %s: %s [%s]", when, err, opErr)
}

func OnAccept(ctx *httpproxy.Context, w http.ResponseWriter,
r *http.Request) bool {
// Handle local request has path "/info"
if r.Method == "GET" && !r.URL.IsAbs() && r.URL.Path == "/info" {
w.Write([]byte("This is go-httpproxy."))
return true
}
return false
}

func OnAuth(ctx *httpproxy.Context, user string, pass string) bool {
// Auth test user.
if user == "test" && pass == "1234" {
return true
}
return false
}

func OnConnect(ctx *httpproxy.Context, host string) (
ConnectAction httpproxy.ConnectAction, newHost string) {
// Apply "Man in the Middle" to all ssl connections. Never change host.
return httpproxy.ConnectMitm, host
}

func OnRequest(ctx *httpproxy.Context, req *http.Request) (
resp *http.Response) {
// Log proxying requests.
log.Printf("INFO: Proxy: %s %s", req.Method, req.URL.String())
return
}

func OnResponse(ctx *httpproxy.Context, req *http.Request,
resp *http.Response) {
// Add header "Via: go-httpproxy".
resp.Header.Add("Via", "go-httpproxy")
}

func main() {
// Create a new proxy with default certificate pair.
prx, _ := httpproxy.NewProxy()

// Set handlers.
prx.OnError = OnError
prx.OnAccept = OnAccept
prx.OnAuth = OnAuth
prx.OnConnect = OnConnect
prx.OnRequest = OnRequest
prx.OnResponse = OnResponse

// Listen...
http.ListenAndServe(":8080", prx)
}
```

## GoDoc

[https://godoc.org/github.com/go-httpproxy/httpproxy](https://godoc.org/github.com/go-httpproxy/httpproxy)
Expand Down

0 comments on commit 9455f5e

Please sign in to comment.