-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
45 lines (36 loc) · 802 Bytes
/
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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
var (
backend string
bind string
)
func init() {
flag.StringVar(&backend, "backend", "http://127.0.0.1:80", "The backend to proxy to")
flag.StringVar(&bind, "bind", ":8888", "Address to bind")
}
func main() {
flag.Parse()
initAuth()
u, err := url.Parse(backend)
if err != nil {
fmt.Printf("Error parsing backend URL (%v) - %v\n", backend, err)
return
}
proxy := httputil.NewSingleHostReverseProxy(u)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if checkAuth(w, r) {
proxy.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm="MY REALM"`)
w.WriteHeader(401)
w.Write([]byte("401 Unauthorized\n"))
})
http.ListenAndServe(bind, nil)
}