-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.go
67 lines (56 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"log"
"net/http"
"github.com/go-httpproxy/httpproxy"
)
func OnError(ctx *httpproxy.Context, where string,
err *httpproxy.Error, opErr error) {
// Log errors.
log.Printf("ERR: %s: %s [%s]", where, 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, authType string, 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)
}