-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoxy.go
51 lines (41 loc) · 931 Bytes
/
goxy.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
package main
import (
"flag"
"log"
"net/http"
"net/http/httputil"
"strconv"
)
var (
port = flag.Int("port", 8080, "Port number to start proxy on")
)
func init() {
flag.Parse()
}
func newDirector(r *http.Request) func(*http.Request) {
return func(req *http.Request) {
schemeOveride := r.Header.Get("goxy-scheme-override")
if schemeOveride != "" {
req.URL.Scheme = schemeOveride
} else {
req.URL.Scheme = "http"
}
req.URL.Host = r.Host
reqLog, err := httputil.DumpRequestOut(req, false)
if err != nil {
log.Printf("Got error %s\n %+v\n", err.Error(), req)
}
log.Println(string(reqLog))
}
}
func proxyHandler(w http.ResponseWriter, r *http.Request) {
proxy := &httputil.ReverseProxy{
Transport: &http.Transport{},
Director: newDirector(r),
}
proxy.ServeHTTP(w, r)
}
func main() {
http.HandleFunc("/", proxyHandler)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(*port), nil))
}