-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
62 lines (55 loc) · 1.27 KB
/
http.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
package main
import (
"encoding/base64"
"io/ioutil"
"net"
"net/http"
"strings"
)
const (
headerProxyAuth = "Proxy-Authorization"
headerXFF = "X-Forwarded-For"
statusProxyAuthReq = "407 Proxy Authentication Required"
)
func newUnauthorizedResponse(req *http.Request, realm string) *http.Response {
return &http.Response{
StatusCode: 407,
ProtoMajor: 1,
ProtoMinor: 1,
Request: req,
Header: http.Header{
"Proxy-Authenticate": []string{"Basic realm=" + realm},
"Proxy-Connection": []string{"close"},
},
Body: ioutil.NopCloser(strings.NewReader(statusProxyAuthReq)),
ContentLength: int64(len(statusProxyAuthReq)),
}
}
func getProxyAuth(req *http.Request) (username, password string, ok bool) {
auth := req.Header.Get(headerProxyAuth)
if auth == "" {
return
}
if strings.HasPrefix(strings.ToLower(auth), "basic ") {
c, err := base64.StdEncoding.DecodeString(auth[6:])
if err == nil {
cs := string(c)
s := strings.IndexByte(cs, ':')
if s >= 0 {
return cs[:s], cs[s+1:], true
}
}
}
return
}
func getClientIP(req *http.Request) string {
field := strings.Split(req.Header.Get(headerXFF), ",")
if field[0] != "" {
return field[0]
}
x, _, z := net.SplitHostPort(req.RemoteAddr)
if z == nil {
return x
}
return ""
}