forked from nsqio/nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.go
71 lines (65 loc) · 1.69 KB
/
notify.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
63
64
65
66
67
68
69
70
71
package nsqadmin
import (
"encoding/base64"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type AdminAction struct {
Action string `json:"action"`
Topic string `json:"topic"`
Channel string `json:"channel,omitempty"`
Node string `json:"node,omitempty"`
Timestamp int64 `json:"timestamp"`
User string `json:"user,omitempty"`
RemoteIP string `json:"remote_ip"`
UserAgent string `json:"user_agent"`
URL string `json:"url"` // The URL of the HTTP request that triggered this action
Via string `json:"via"` // the Hostname of the nsqadmin performing this action
}
func basicAuthUser(req *http.Request) string {
s := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
return ""
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return ""
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return ""
}
return pair[0]
}
func (s *httpServer) notifyAdminAction(action, topic, channel, node string, req *http.Request) {
if s.ctx.nsqadmin.getOpts().NotificationHTTPEndpoint == "" {
return
}
via, _ := os.Hostname()
u := url.URL{
Scheme: "http",
Host: req.Host,
Path: req.URL.Path,
RawQuery: req.URL.RawQuery,
}
if req.TLS != nil || req.Header.Get("X-Scheme") == "https" {
u.Scheme = "https"
}
a := &AdminAction{
Action: action,
Topic: topic,
Channel: channel,
Node: node,
Timestamp: time.Now().Unix(),
User: basicAuthUser(req),
RemoteIP: req.RemoteAddr,
UserAgent: req.UserAgent(),
URL: u.String(),
Via: via,
}
// Perform all work in a new goroutine so this never blocks
go func() { s.ctx.nsqadmin.notifications <- a }()
}