Skip to content

Commit 63addc4

Browse files
demo
1 parent 8f04654 commit 63addc4

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

demo/main.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,61 @@ import (
77
"github.com/go-httpproxy/httpproxy"
88
)
99

10-
func OnError(ctx *httpproxy.Context, when string, err *httpproxy.Error, opErr error) {
11-
log.Printf("%s %s %s", when, err, opErr)
10+
func OnError(ctx *httpproxy.Context, when string,
11+
err *httpproxy.Error, opErr error) {
12+
// Log errors.
13+
log.Printf("ERR: %s: %s [%s]", when, err, opErr)
1214
}
1315

14-
func OnAccept(ctx *httpproxy.Context, w http.ResponseWriter, r *http.Request) bool {
16+
func OnAccept(ctx *httpproxy.Context, w http.ResponseWriter,
17+
r *http.Request) bool {
18+
// Handle local request has path "/info"
19+
if r.Method == "GET" && !r.URL.IsAbs() && r.URL.Path == "/info" {
20+
w.Write([]byte("This is go-httpproxy."))
21+
return true
22+
}
1523
return false
1624
}
1725

1826
func OnAuth(ctx *httpproxy.Context, user string, pass string) bool {
27+
// Auth test user.
1928
if user == "test" && pass == "1234" {
2029
return true
2130
}
2231
return false
2332
}
2433

25-
func OnConnect(ctx *httpproxy.Context, host string) (ConnectAction httpproxy.ConnectAction, newHost string) {
34+
func OnConnect(ctx *httpproxy.Context, host string) (
35+
ConnectAction httpproxy.ConnectAction, newHost string) {
36+
// Apply "Man in the Middle" to all ssl connections. Never change host.
2637
return httpproxy.ConnectMitm, host
2738
}
2839

29-
func OnRequest(ctx *httpproxy.Context, req *http.Request) (resp *http.Response) {
30-
log.Printf("Request %s", req.URL.String())
40+
func OnRequest(ctx *httpproxy.Context, req *http.Request) (
41+
resp *http.Response) {
42+
// Log proxying requests.
43+
log.Printf("INFO: Proxy: %s %s", req.Method, req.URL.String())
3144
return
3245
}
3346

34-
func OnResponse(ctx *httpproxy.Context, req *http.Request, resp *http.Response) {
35-
resp.Header.Add("Via", "test")
47+
func OnResponse(ctx *httpproxy.Context, req *http.Request,
48+
resp *http.Response) {
49+
// Add header "Via: go-httpproxy".
50+
resp.Header.Add("Via", "go-httpproxy")
3651
}
3752

3853
func main() {
54+
// Create a new proxy with default certificate pair.
3955
prx, _ := httpproxy.NewProxy()
56+
57+
// Set handlers.
4058
prx.OnError = OnError
4159
prx.OnAccept = OnAccept
4260
prx.OnAuth = OnAuth
4361
prx.OnConnect = OnConnect
4462
prx.OnRequest = OnRequest
4563
prx.OnResponse = OnResponse
46-
prx.MitmChunked = true
4764

65+
// Listen...
4866
http.ListenAndServe(":8080", prx)
4967
}

0 commit comments

Comments
 (0)