forked from weaveworks/weave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpproxy.go
37 lines (32 loc) · 887 Bytes
/
httpproxy.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
package main
import (
"flag"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
var (
listen = flag.String("listen", "localhost:1080", "listen on address")
dest = flag.String("dest", "http://localhost:6784", "destination address")
)
func main() {
flag.Parse()
target, err := url.Parse(*dest)
if err != nil {
log.Fatal(err)
}
// Proxy, copying the incoming request exactly apart from the host
myProxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
}}
// Allow browser requests, no matter what their origin
proxyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
}
myProxy.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(*listen, proxyHandler))
}