Skip to content

Commit

Permalink
demo
Browse files Browse the repository at this point in the history
  • Loading branch information
orkunkaraduman committed Feb 4, 2018
1 parent 8f04654 commit 63addc4
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,61 @@ import (
"github.com/go-httpproxy/httpproxy"
)

func OnError(ctx *httpproxy.Context, when string, err *httpproxy.Error, opErr error) {
log.Printf("%s %s %s", when, err, opErr)
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 {
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) {
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.Printf("Request %s", req.URL.String())
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) {
resp.Header.Add("Via", "test")
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
prx.MitmChunked = true

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

0 comments on commit 63addc4

Please sign in to comment.