/
newhook.go
166 lines (149 loc) · 3.78 KB
/
newhook.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package webhook
import (
"bytes"
"crypto/sha1"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"os"
"time"
"github.com/astaxie/beego"
"github.com/Qihoo360/wayne/src/backend/bus/message"
"github.com/Qihoo360/wayne/src/backend/bus/newbus"
"github.com/Qihoo360/wayne/src/backend/models"
"github.com/Qihoo360/wayne/src/backend/models/response"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
const EventName = "webhook"
var (
httpClient *http.Client
)
// init HTTPClient
func init() {
httpClient = createHTTPClient()
}
const (
MaxIdleConns int = 100
MaxIdleConnsPerHost int = 100
IdleConnTimeout int = 60
)
// createHTTPClient for connection re-use
func createHTTPClient() *http.Client {
sec, err := beego.AppConfig.Int("WebhookClientTimeout")
if err != nil || sec == 0 {
sec = 10
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: MaxIdleConns,
MaxIdleConnsPerHost: MaxIdleConnsPerHost,
IdleConnTimeout: time.Duration(IdleConnTimeout) * time.Second,
},
Timeout: time.Duration(sec) * time.Second,
}
return client
}
func RegisterHookHandler() error {
if newbus.UBus != nil {
if err := newbus.UBus.RegisterHandler(EventName, Process); err != nil {
logs.Error("register failed: %s ", err)
return err
}
return nil
}
logs.Error("EventBus 未初始化")
return errors.New("EventBus 未初始化")
}
func Process(msg *message.Message) error {
var data *message.HookMessageData
switch msg.Type {
case message.TypeHook:
if err := json.Unmarshal(msg.Data, &data); err != nil {
logs.Error(err)
return err
}
logs.Info(data)
default:
return nil // 不处理其他类型数据
}
webHooks, err := models.WebHookModel.GetTriggered(data.NamespaceId, data.AppId, data.EventKey)
if err != nil {
return err
}
for _, hook := range webHooks {
go exec(hook, data)
}
return nil
}
func exec(hook models.WebHook, data *message.HookMessageData) {
request := Request{
EventKey: data.EventKey,
User: data.User,
IP: data.IP,
Datetime: data.Datetime,
Payload: data.Payload,
}
if data.NamespaceId != 0 {
ns, err := models.NamespaceModel.GetById(data.NamespaceId)
if err != nil {
logs.Error(err)
} else {
request.Namespace = response.Namespace{
Id: ns.Id,
Name: ns.Name,
CreateTime: ns.CreateTime,
UpdateTime: ns.UpdateTime,
User: ns.User,
}
}
}
if data.AppId != 0 {
app, err := models.AppModel.GetById(data.AppId)
if err != nil {
logs.Error(err)
} else {
request.App = response.App{
Id: app.Id,
Name: app.Name,
Description: app.Description,
User: app.User,
CreateTime: app.CreateTime,
UpdateTime: app.UpdateTime,
}
}
}
jsonData, err := json.Marshal(request)
if err != nil {
logs.Error(err)
return
}
body := bytes.NewReader(jsonData)
req, err := http.NewRequest(http.MethodPost, hook.Url, body)
if err != nil {
logs.Error(err)
return
}
ua, _ := os.Hostname()
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", fmt.Sprintf("Wayne-WebHook/%s", ua))
if len(hook.Secret) > 0 {
req.Header.Set("X-Wayne-Signature", fmt.Sprintf("sha1=%x", sha1.New().Sum([]byte(hook.Secret))))
}
var resp *http.Response
resp, err = httpClient.Do(req)
if err != nil {
logs.Error(err)
return
}
if resp.StatusCode >= http.StatusBadRequest {
logs.Warning("WebHook failed to call %s with code %d", hook.Url, resp.StatusCode)
}
defer resp.Body.Close()
}