Skip to content

Commit 85b46aa

Browse files
added simple code
1 parent 0eac6b2 commit 85b46aa

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

demo/simple/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/simple

demo/simple/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/go-httpproxy/httpproxy"
8+
)
9+
10+
func OnError(ctx *httpproxy.Context, where string,
11+
err *httpproxy.Error, opErr error) {
12+
// Log errors.
13+
log.Printf("ERR: %s: %s [%s]", where, err, opErr)
14+
}
15+
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+
}
23+
return false
24+
}
25+
26+
func OnAuth(ctx *httpproxy.Context, authType string, user string, pass string) bool {
27+
// Auth test user.
28+
if user == "test" && pass == "1234" {
29+
return true
30+
}
31+
return false
32+
}
33+
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.
37+
return httpproxy.ConnectMitm, host
38+
}
39+
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())
44+
return
45+
}
46+
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")
51+
}
52+
53+
func main() {
54+
// Create a new proxy with default certificate pair.
55+
prx, _ := httpproxy.NewProxy()
56+
57+
// Set handlers.
58+
prx.OnError = OnError
59+
prx.OnAccept = OnAccept
60+
prx.OnAuth = OnAuth
61+
prx.OnConnect = OnConnect
62+
prx.OnRequest = OnRequest
63+
prx.OnResponse = OnResponse
64+
65+
// Listen...
66+
http.ListenAndServe(":8080", prx)
67+
}

0 commit comments

Comments
 (0)